Showing posts with label Groovy. Show all posts
Showing posts with label Groovy. Show all posts

Friday, 10 July 2020

Dell Boomi API Management-Configure Rest Based API for form Based Jason Response

Dell Boomi API Management
Most of you are familiar with Dell Boomi API management feature. APIs are implemented as deployable API components. There are two types: API Service and API Proxy. Using API components enables you to consolidate API design into a single, explicit location.
In API Service, we can define an endpoint using a Rest or Soap.

Configure API:
First we have to configure an API.Give your API a title and then a base path.As Shown in screen shot below.


Then go on Rest Tab and it will automatically configure path for you then click on Add End Point button.Configure you end point. Configure your input type and output type and create a process that will handle your request.As shown in screen shot.



Create Listener Process
Now create a process that will listen all the request send to API. In my example i am configuring a process that will get two inputs as API parameter and on the bases of those inputs insert data into salesforce.
Starting connector will be WebService Server Connector with Listen action. Create its operation with 
Operation Type = Get
Object= will be your resource path you configure during end point creation
Expected Input Type = Single Data
Response Output Type = Single Data
Result Content Type = text/plain


Then you have to use this groovy script so parameter values can be assigned to your variables.Below groovy script will be used.

import java.util.Properties;
import java.io.InputStream;

import org.apache.http.NameValuePair;
import org.apache.http.client.utils.URLEncodedUtils;
import java.nio.charset.StandardCharsets;

for( int i = 0; i < dataContext.getDataCount(); i++ ) {
    InputStream is = dataContext.getStream(i);
    Properties props = dataContext.getProperties(i);

    def input = is.text;
    def data = URLEncodedUtils.parse(input, StandardCharsets.UTF_8) as List<NameValuePair>;
    
    for ( NameValuePair arg: data ) {
        props.setProperty("document.dynamic.userdefined." + arg.name, arg.value);
    }
    
    dataContext.storeStream(is, props);
}

Now we have to use dynamic properties to get values from endpoints. Will create two dynamic process properties to get Account Id and Account Key. Dynamic process property name = AccountId and AccountKey. It will get value from our endpoint. Our endpoint is 

Http:Demo//ws/rest/Test/Lim?AID=123456&Key=444 

Dynamic Process Property Name = AccountId
Parameter = Dynamic document property with name = AID

Dynamic Process Property Name = AccountKey
Parameter = Dynamic document property with name = Key

Note: One thing to note here is that my dynamic process property names are same as my url parameter name. This is the key point to set variables. The bold words in my url are my parameters placeholder i have given the same name to my dynamic process property. This is how you can set values.

In my case we will use another dynamic process property to check that the account id and key combination is in salesforce or not.In the decision box will check if salesforce do not have this combination we will send an email alert if this combination exists we will insert data in salesforce.

Salesforce Connector

Configuring salesforce connector or mapping data is easy tasks and can be found in below links.












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 !!!