Tuesday, 23 October 2018

Add one day to the input date in SAP HCI/CPI

Increment the date with day by 1.

Received a requirement to add one day to the input date. Script is implemented in the mapping level to achieve this functionality.

Groovy Script:


import com.sap.it.api.mapping.*;

def String IncrementDateByOne1(String dateInput){

def dateFormat = 'yyyy-MM-dd'
try{
//For any date
def dateString = dateInput
def date = Date.parse(dateFormat, dateString)
//log.info date.nextDay()

            return date.nextDay()
}
catch(Exception ex){
    return ""
}
}

Input: 2012-01-03 19:10:10
Output: 2012-01-04



Monday, 15 October 2018

Trim Functionality using the Groovy Script in SAP CPI



Trim:
String str = " dsfdsf ";

String trimmedStr = str.trim();


println trimmedStr;



Pick the Iflow Name in Camel Expression in SAP CPI

For Picking up the Iflow name in the iflow and pass to fields, we can use this and achieve it.

   ${camelId}



Camel Expressions available in SAP CPI

Parameter Description
${in.body} or ${body} Message Payload
${header.var1} Message Header Variable (var1)
${property.prop1} Exchange Property (prop1)
${date:now:yyyy-MM-dd} Current Date/Time with format
${property. SAP_MessageProcessingLogID} Message Guid
${CamelFileName} File Name
${CamelHttpUri} HTTP URI (URL with QueryString)
${CamelDestinationOverrideUrl} SOAP Adapter URL
${exception.message} Error Message
${exception.stacktrace} Error Stacktrace
${camelId} Iflow Name
${SAP_ApplicationID} ID created in Message Processing Log for searching in monitoring
Relative Paths
Expression Returns
file:absolute FALSE
file:absolute.path \workspace\camel\camel-core\target\filelanguage\test\hello.txt
file:ext txt
file:name test\hello.txt
file:name.ext txt
file:name.noext test\hello
file:onlyname hello.txt
file:onlyname.noext hello
file:parent filelanguage\test
file:path filelanguage\test\hello.txt
Absolute Paths
Expression Returns
file:absolute TRUE
file:absolute.path \workspace\camel\camel-core\target\filelanguage\test\hello.txt
file:ext txt
file:name test\hello.txt
file:name.ext txt
file:name.noext test\hello
file:onlyname hello.txt
file:onlyname.noext hello
file:parent \workspace\camel\camel-core\target\filelanguage\test
file:path \workspace\camel\camel-core\target\filelanguage\test\hello.txt



Friday, 5 October 2018

Picklist/ Lookup comparison in SAP CPI/HCI

Lookup and compare the two payload from the input and try to filter out the <label> field and return it.


Test data:

<?xml version="1.0" encoding="UTF-8"?>
<multimap:Messages
xmlns:multimap="http://sap.com/xi/XI/SplitAndMerge">
<multimap:Message1>
<Picklist>
<Picklist>
<optionId>301</optionId>
<locale>en_GB</locale>
<label>Permanent</label>
</Picklist>
<Picklist>
<optionId>346</optionId>
<locale>en_GB</locale>
<label>Associate</label>
</Picklist>
<Picklist>
<optionId>304</optionId>
<locale>en_GB</locale>
<label>Active</label>
</Picklist>
<Picklist>
<optionId>352</optionId>
<locale>en_GB</locale>
<label>Permanent</label>
</Picklist>
</Picklist>
<Employee>
<EmpEmployment>
<EmpEmployment>
<startDate>2018-04-01T00:00:00.000</startDate>
<personIdExternal>50000006</personIdExternal>
<userId>50000006</userId>
<endDate></endDate>
</EmpEmployment>
<EmpEmployment>
<startDate>2018-05-09T00:00:00.000</startDate>
<personIdExternal>50000006</personIdExternal>
<userId>3</userId>
<endDate></endDate>
</EmpEmployment>
</EmpEmployment>
<EmpJob>
<EmpJob>
<userId>50000006</userId>
<employeeClass>352</employeeClass>
</EmpJob>
<EmpJob>
<userId>3</userId>
<employeeClass>346</employeeClass>
</EmpJob>
</EmpJob>
</Employee>
</multimap:Message1>
</multimap:Messages>


Output:

Permanent
Associate

GroovyScript:

def Message processData(Message message) {
    def body = message.getBody(java.lang.String) as String
    def root = new XmlSlurper().parseText(body)
    def xmldata1 = []


    def xmldata2 = []
    def xmldata3 = []
    def i=0;
    def j=0;
       root.'**'.findAll { it.name() == 'optionId'}.each { a ->
    xmldata1 << a.text()}
    root.'**'.findAll { it.name() == 'label'}.each { a ->
    xmldata2 << a.text()}
    root.'**'.findAll { it.name() == 'employeeClass'}.each { a ->
    xmldata3 << a.text()}
  
    for(j = 0; j < xmldata3.size(); j++)
    {
    for(i = 0; i < xmldata1.size(); i++){
   
        if( xmldata1[i] == xmldata3[j])
        {
       return xmldata2[i]   
        }
}
   }
}