Monday 5 June 2017

Dell Boomi: Read all repeating tags text and put in one text field using groovy

Read data of all repeating tags and put in one text field

If you are working with Dell Boomi in order to integrate SAP data you must find a need for this, as this is SAP standard behavior that when it creates an XML or IDoc of text data it start reapeating tags till the number of enter in that field.

E.g:
In SAP you enter following text:
This is my first blog
I am writing this blog about Dell Boomi
and SAP general behaviors

When an IDoc/XML file will be created it will look like this:
<IDOC BEGIN="1">
<Notes>This is my first blog</Notes>
<Notes> I am writing this blog about Dell Boomi </Notes>
<Notes> and SAP general behaviors </Notes>
</IDOC BEGIN="1">

Problem
Now issue is that your destination system need this complete text in one field along with enters. If you directly map this tag in your map component only Dell Boomi will only read first ever line and all the other text will be gone.

Solution
In order to solve this issue you can use groovy in data process to read all these kind of repeating tags and then create a new node in your process and map that node with your destination text field.

Groovy Code: 
import java.util.Properties;
import java.io.InputStream;
import org.jdom.input.SAXBuilder;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.xpath.XPath;
import org.jdom.output.XMLOutputter;
import com.boomi.execution.ExecutionUtil;

Integer Count = 0;
for( int i = 0; i < dataContext.getDataCount(); i++ ) {

 InputStream is = dataContext.getStream(i);
 Properties props = dataContext.getProperties(i);
   
 // Build XML Document
 SAXBuilder builder = new SAXBuilder();
 Document doc = builder.build(is);

 XPath Approved_Pricing;
 Approved_Pricing = XPath.newInstance("//ZDEBMAS/IDOC/E1KNVVH[TDID='Z002']/E1KNVVL/TDLINE");
 
 MSGFN = Approved_Pricing.selectNodes(doc);
 
 Desc='';
 result = new StringBuffer();
for(int ak=0;ak<MSGFN.size();ak++){
if (Count < 10){
result.append(MSGFN[ak].getText()+System.getProperty('line.separator'));
Count = Count + 1;
}
}
    Desc = result.toString();
Notes = new Element("Pricing_Notes_Description").addContent(Desc);
x = XPath.newInstance("//ZDEBMAS/IDOC");
    eleParentElement = x.selectSingleNode(doc);
    if(eleParentElement !=null){
      eleParentElement.addContent(Notes);
    }
     XMLOutputter outputter = new XMLOutputter();
     is = new ByteArrayInputStream(outputter.outputString(doc).getBytes("UTF-8"));

     dataContext.storeStream(is, props);
}

You are adding a new element Notes in the root IDoc which you can then map to your destination field.you also have to provide complete and accurate path of that repeating tag.


Hope this helps you. Happy Coding !!!