Create sub group to a group using JAVA SDK

Product/components used and version/fix level:

10.18

Detailed explanation of the problem:

Hi Guys!
I want to create a sub group to a group using the Java SDK. I tried the following

subscriptionsService.runForTenant(subscriptionsService.getTenant(), () -> {
				ManagedObjectRepresentation response = inventoryApi.create(asset);
				LOG.info("Group created with id:" + response.getId().getValue());
				

				// make this group as child of unsorted
				GId gid = GId.asGId(unsortedGroupMoId.get(subscriptionsService.getTenant()));
				ManagedObject managedObjectApi = inventoryApi.getManagedObjectApi(gid);
				managedObjectApi.addChildAsset(ManagedObjects.asManagedObject(response.getId()));
			});

I get the error that the id is not updatable. Does anyone know how can I assign the newly created group as the child of the group whose Gid i extract in the code above.

Error messages / full error message screenshot / log file:

execution of task failed within tenant : XXXXX - Http status code: 422
{error=“undefined/validationError”,message=“Following fields are not updateable: id”,info=“https://www.cumulocity.com/guides/reference/rest-implementation//#a-name-error-reporting-a-error-reporting",details="null”}
tenant: XXXXX user: service_c2c-integration

Question related to a free trial, or to a production (customer) instance?



Arsalan Mahmood Siddiqi

You can skip the first step and just execute the second call with the GID of the source group where you want to attach the to-be-created sub-group to.
Haven’t tested it but this is how I understand it.

ManagedObject managedObjectApi = inventoryApi.getManagedObjectApi(gid); //GID of the source group 
managedObjectApi.addChildAsset(<<ManagedObjectReprensentationToBeCreated>>);

This worked! There was another function called addChildAssets

				// make this group as child of unsorted
				GId gid = GId.asGId(unsortedGroupMoId.get(subscriptionsService.getTenant()));

				ManagedObject managedObjectApi = inventoryApi.getManagedObjectApi(gid);
				managedObjectApi.addChildAssets(response.getId());


Arsalan Mahmood Siddiqi

1 Like

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