# Update managed object fragement

**URL:** <https://community.cumulocity.com/t/update-managed-object-fragement/7512>\
**Category:** Forum\
**Tags:** cumulocity\
**Created:** [February 7, 2024, 9:52am UTC](https://community.cumulocity.com/t/update-managed-object-fragement/7512 "2024-02-07T09:52:14Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![vaibhavi\_ghumare](https://avatars.discourse-cdn.com/v4/letter/v/a587f6/32.png) [@vaibhavi\_ghumare](https://community.cumulocity.com/u/vaibhavi_ghumare)\
**Post date:** [February 7, 2024, 9:52am UTC](https://community.cumulocity.com/t/update-managed-object-fragement/7512/1 "2024-02-07T09:52:14Z")

</div>

````auto
    "id": "43348402",
    "type": "abc",
    "name": "TestNameTesting20",
    "c8y_Rules": [
        {
            "name": "da",
            "Date": "2024-01-26T10:36:43.000Z",
            "isActive": false
        },
        {
            "name": "TestRule",
            "isActive": true
        }
    ]}```

Need to update the isActive flag to true where Date property is present in the above fragement.
````

---

<div class="post-metadata">

**Author:** ![Nicholas\_Adams](https://avatars.discourse-cdn.com/v4/letter/n/bbce88/32.png) [@Nicholas\_Adams](https://community.cumulocity.com/u/Nicholas_Adams)\
**Post date:** [February 7, 2024, 10:56am UTC](https://community.cumulocity.com/t/update-managed-object-fragement/7512/2 "2024-02-07T10:56:29Z")

</div>

Something along the lines of:

```auto
ManagedObject mo := <your MO>
AnyExtractor rulesAE :=
    AnyExtractor(mo.params.getOrDefault("c8y_Rules"));
sequence<any> rulesSeq := rulesAE.getSequenceOr("", new sequence<any>);
any rule;
for rule in rulesSeq {
    if rule.hasEntry("date") {
        rule.setEntry("isActive", true);
    } else { // If you also looking to set false when date not present.
        rule.setEntry("isActive", false);
    }
}
send mo to ManagedObject.SEND_CHANNEL;

```

You could also be a bit more explicit if you care about logging errors:

```auto
if mo.params.hasKey("c8y_Rules") {
    AnyExtractor rulesAE :=
        AnyExtractor(mo.params["c8y_Rules"]);
    ...
} else {
    log "c8y_Rules fragment missing from ManagedObject " + mo.id at ERROR;
}

```

---

<div class="post-metadata">

**Author:** ![Nicholas\_Adams](https://avatars.discourse-cdn.com/v4/letter/n/bbce88/32.png) [@Nicholas\_Adams](https://community.cumulocity.com/u/Nicholas_Adams)\
**Post date:** [February 7, 2024, 11:12am UTC](https://community.cumulocity.com/t/update-managed-object-fragement/7512/3 "2024-02-07T11:12:17Z")

</div>

As a further refinement you may also want to track if any items in c8y\_Rules were updated so that you don’t send spurious ManagedObject updates to the REST API.

---

<div class="post-metadata">

**Author:** ![vaibhavi\_ghumare](https://avatars.discourse-cdn.com/v4/letter/v/a587f6/32.png) [@vaibhavi\_ghumare](https://community.cumulocity.com/u/vaibhavi_ghumare)\
**Post date:** [February 7, 2024, 12:27pm UTC](https://community.cumulocity.com/t/update-managed-object-fragement/7512/4 "2024-02-07T12:27:42Z")

</div>

I need to write the code for java microservice

---

<div class="post-metadata">

**Author:** ![Cyril\_Poder](https://dub1.discourse-cdn.com/flex005/user_avatar/community.cumulocity.com/cyril_poder/32/17142_2.png) [@Cyril\_Poder](https://community.cumulocity.com/u/Cyril_Poder)\
**Post date:** [February 7, 2024, 1:27pm UTC](https://community.cumulocity.com/t/update-managed-object-fragement/7512/5 "2024-02-07T13:27:32Z")

</div>

```java
var mor = inventoryApi.get("43348402");
if (mor.hasProperty("c8y_Rules") {
  var rules = mor.get(c8y.Rules.class); // assuming you have a c8y.Rules class, but for some reason I doubt you have
  // or
  var rules = (List<Map<String, Object>>)mor.getProperty("c8y_Rules");
  rules.forEach(rule -> {
    if (rule.hasKey("Date")) {
      rule.put("isActive", true);
    }
  });
}
updatedMor = new ManagedObjectRepresentation();
updatedMor.setId(mor.getId());
updatedMor.setProperty("c8y_Rules", rules); // You just need to update that property
inventoryApi.update(updatedMor);

```

Just writing this directly in here, not from my IDE, so there might be some typo, but you get the idea.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/flex005/user_avatar/community.cumulocity.com/system/32/31990_2.png) [@system](https://community.cumulocity.com/u/system)\
**Post date:** [August 5, 2024, 1:28pm UTC](https://community.cumulocity.com/t/update-managed-object-fragement/7512/6 "2024-08-05T13:28:05Z")

</div>

Closed on August 5, 2024, 1:28pm UTC

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