How to access User Defined Document Properties using Groovy

Document created by gbockelmann Employee on Nov 8, 2011Last modified by chris_stevens on Jul 6, 2016
Version 2Show Document
  • View in full screen mode

Sometimes you may want to get or set a User Defined Document Property value from within a custom Groovy script to facilitate processing. The example script below shows you how to do so.

You cannot access User Defined Document Properties through scripting within a Map Function context. Instead simply use a Get/Set Properties function step in coordination with the scripting step.

  1. Add a Data Process step to your Process.
  2. Add a Custom Scripting step.
  3. Use the following script example to get and set User Defined Document Properties from within the "document 'for' loop".

 

Script:

import java.util.Properties;
import java.io.InputStream;
for ( int i = 0; i < dataContext.getDataCount(); i++ ) {
  InputStream is = dataContext.getStream(i);
  Properties props = dataContext.getProperties(i);
  // Retrieve the current property value
  propValue = props.getProperty("document.dynamic.userdefined.MY_PROPERTY_NAME");
  // Modify the value
  propValue = "some new value";
  // Store the new property value
  props.setProperty("document.dynamic.userdefined.MY_PROPERTY_NAME", propValue);
  dataContext.storeStream(is, props);
}

 

Notes:

  • User Defined Document Properties are prefixed with document.dynamic.userdefined.MY_PROPERTY_NAME.
  • Check for null values when getting and setting property values. Attempting to set a null value will result in a RuntimeException.
11 people found this helpful

Attachments

    Outcomes