Thursday, 22 November 2018

How to route to different receiver based on the CSV file

In this blog, we discuss about how to read each line from CSV file and route it depends on the value from first field.

Sample Input data:

1158|Join
1159|Enhanced
1159|IN|980||92|193|GoTest|||20183|||||IND|India CC|5225||||||
1159|END_OF|BEART
1159|BEG|20181003
1158|EN
1159|EN


Based on 1158 and 1159 it needs to be routed.

IFLOW:

For testing purpose, we are passing the input data in content modifier












We are getting the data one by one in a separate mail. The requirement is to send a mail on the combined one. As there is a SAP standard, we can't use Gather after Router. Working on the  resolution.



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]   
        }
}
   }
}







Tuesday, 11 September 2018

Remove special characters - Groovy Script -SAP HCI/CPI

We had a requirement to remove the special characters in the field in mapping and also remove the prefix "0".

After this if the value starts with 63 or 61 then + should be added at prefix and send to target field

Note: You can find the usage for if condition, replaceAll, startsWith, replaceFirst function, concat, Boolean function

Input: 

"000ew63f@gAH-Je5^%7r/df&6!)78g"

Output:

6357678


Code:


import com.sap.it.api.mapping.*;
def String customFunc(String arg1){
def str5 = ''
def str4 = '+'
// Remove the special characters
       def str2 = arg1.replaceAll("[\\*\\?/()\\-\\^!@_+%.&#a-zA-Z]","")
// remove the leading zero's
def str3 = str2.replaceFirst ("^0*", "")
//condition to check the 63 or 61
if (str3.startsWith("63") || str3.startsWith("61"))
{
  
   str5 = str4.concat(str3)
}

return str5

}

or 

import com.sap.it.api.mapping.*;
def String customFunc(String arg1, String arg2, String arg3){
def len1
def len2
len1 = arg1.length()
len2 = arg2.length()
if( len1 > 0 )
{
def str2 = arg1.replaceAll("[\\*\\?/()\\-\\^!@_+%.&#a-zA-Z]","")
def str3 = str2.replaceFirst ("^0*", "")
def str5
def str6
def str7
if (str3.startsWith("63") || str3.startsWith("61"))
{
    def str4 = "+"
    str5 = str4.concat(str3)
return str5  
}
else 
{
    if( (len2 == 0) && (arg3 == "Melbourne" || arg3 == "Sydney"  )){
  str6 = "+61"
  str7 = str6.concat(str3)
  return str7
    }
}
}
else
{
                return arg1
}
}