Add Device to Asset/Group via Java SDK

Hi there,
I want to add a new Device to a specific Asset/Group. Is that directly possible in the creation process of a new device?
I figured out, that I have to do a request to {{baseUrl}}/inventory/managedObjects/:id/childAssets .
That worked just fine, but as I’m using the java sdk for my microservice, I run into a few problems.
I came up with this code

List<ManagedObjectReferenceRepresentation> assetCollection = deviceObjectRepresentation.getAssetParents().getReferences();
ManagedObjectReferenceRepresentation groupAssetRepresentation = new ManagedObjectReferenceRepresentation();
groupAssetRepresentation.set(inventoryApi.get(new GId("117063791")));
assetCollection.add(groupAssetRepresentation);
deviceObjectRepresentation.getAssetParents().setReferences(assetCollection);
deviceObjectRepresentation.setLastUpdatedDateTime(null);
inventoryApi.update(deviceObjectRepresentation);

to update the parentAssets of the deviceObjectRepresentation.
If I print out the deviceObjectRepresentation it seemed to work, but when looking at the result on cumulocity, nothing has changed.
I don’t think it has something to do with the permissions, because that should result in a 403, as I suppose.
I didn’t get any errors by running the code.
Thanks for any possible help

1 Like

Child devices and assets are set with a dedicated endpoint, not by changing the children lists in the managed object.

In your case using java-client you can do it like this:

GId parentID = new GId(deviceId);
GId childId = new GId(childDeviceId);
ManagedObject managedObjectApi = inventoryApi.getManagedObjectApi(parentID);
managedObjectApi.addChildDevice(childId);

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.