# Update field in compound type using groovy script

**URL:** <https://community.bloomreach.com/t/update-field-in-compound-type-using-groovy-script/1383>\
**Category:** Experience Manager (PaaS/OnPrem)\
**Created:** [September 5, 2019, 8:58pm UTC](https://community.bloomreach.com/t/update-field-in-compound-type-using-groovy-script/1383 "2019-09-05T20:58:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rakesh](https://avatars.discourse-cdn.com/v4/letter/r/258eb7/32.png) [@rakesh](https://community.bloomreach.com/u/rakesh)\
**Post date:** [September 5, 2019, 8:58pm UTC](https://community.bloomreach.com/t/update-field-in-compound-type-using-groovy-script/1383/1 "2019-09-05T20:58:04Z")

</div>

Hi,

How to update a field present in the compound type using groovy script?

I’am using something like this hbook:primaryTaxonomy/@hbook:firstLevel to access the compound field. Where firstLevel is a dropdown string field.

```
class ChangeTax extends BaseNodeUpdateVisitor {
 
  private static final NEW_TAG = 'hbook:primaryTaxonomy/@hbook:firstLevel'
 
  boolean doUpdate(Node node) {
    try{
        def taxvalue
        //log.debug "Node Name ${node.path}"
        //Fetch current value
        taxvalue = node.getProperty("hbook:primaryTaxonomy/@hbook:firstLevel").getValue()
        log.debug "Tag Value is ${taxvalue}"

        //Update value to ourstory
        node.setProperty(NEW_TAG, 'ourstory');
        return true;
    }catch(e) {
      return false;
    }
  }

boolean undoUpdate(Node node) {
throw new UnsupportedOperationException('Updater does not implement undoUpdate method')
}

}

```

Please suggest.

Thanks,  
RK

---

<div class="post-metadata">

**Author:** ![woonsanko](https://avatars.discourse-cdn.com/v4/letter/w/b5a626/32.png) [@woonsanko](https://community.bloomreach.com/u/woonsanko)\
**Post date:** [September 5, 2019, 9:55pm UTC](https://community.bloomreach.com/t/update-field-in-compound-type-using-groovy-script/1383/2 "2019-09-05T21:55:28Z")

</div>

Hi RK,

You can get the compound node first to get and set properties, using JCR API [1]:

```auto
  if (node.hasNode("hbook:primaryTaxonomy")) {
    def compoundNode = node.getNode("hbook:primaryTaxonomy")
    if (compoundNode.hasProperty("hbook:firstLevel")) {
      def value = compoundNode.getProperty("hbook:firstLevel").getString()
      //...
    }
    //...
    compoundNode.setProperty("hbook:firstLevel", "ourstory")
  }

```

You can also use the utility, `org.hippoecm.repository.util.JcrUtils`, to avoid exceptional cases such as non-existing node or illegal access, etc. For example,

```auto
import org.hippoecm.repository.util.JcrUtils
//...
  def compoundNode = JcrUtils.getNodeIfExists(node, "hbook:primaryTaxonomy")
  if (compoundNode != null) {
    def value = JcrUtils.getStringProperty(compoundNode, "hbook:firstLevel", null)
    //...
    compoundNode.setProperty("hbook:firstLevel", "ourstory")
  }

```

Regards,

Woonsan

[1] [https://docs.adobe.com/docs/en/spec/javax.jcr/javadocs/jcr-2.0/index.html](https://docs.adobe.com/docs/en/spec/javax.jcr/javadocs/jcr-2.0/index.html)

---

<div class="post-metadata">

**Author:** ![rakesh](https://avatars.discourse-cdn.com/v4/letter/r/258eb7/32.png) [@rakesh](https://community.bloomreach.com/u/rakesh)\
**Post date:** [September 6, 2019, 12:24pm UTC](https://community.bloomreach.com/t/update-field-in-compound-type-using-groovy-script/1383/3 "2019-09-06T12:24:09Z")

</div>

Awesome, thanks! Will give a try.
