cancel
Showing results for 
Search instead for 
Did you mean: 

Converting String To Integer in sap cpi

04-21-2022 10:07 AM
vineetha12 Participant
5659 views 2 comments
0 Likes
SAP Managed Tags
Subscribe

Hi EveryOne,

I have a requirement to convert the string data type to integer data type if the field has an integer number, else if the field has a string value pass it as it is.

for example, I have the payload

for the field "PurReqnSSPCatalogForEdit" I have an integer value. I need to convert it from string to integer.

I have written the below groovy script for this

import com.sap.gateway.ip.core.customdev.util.Message;

import java.util.HashMap;

import groovy.json.*

def Message processData(Message message) {

//Body

def jsonOP = message.getBody(String.class);

jsonOP=jsonOP.toString()

def json_to_str="{\"Element\":["+jsonOP+"]}"

def jsonSlurper = new JsonSlurper();

def list = jsonSlurper.parseText(json_to_str);

list.Element.each{

it.EmployeeForEdit.toString()

it.BusinessObjectTypeForEdit.toString()

it.PurReqnSSPCatalogForEdit=Integer.parseInt(it.get("PurReqnSSPCatalogForEdit").toString());

}

def jsonlist = JsonOutput.toJson(list);

def jsonObject = jsonSlurper.parseText(jsonlist);

def removerootnode = JsonOutput.toJson(jsonObject["Element"]);

def removebraket = removerootnode.replace("[","");

def removeendbraket = removebraket.replace("]","");

message.setBody(removeendbraket);

return message;

}

But if the field "PurReqnSSPCatalogForEdit" has a string value parse it as it is. What is the condition I need to add to the above groovy script to achieve this? Please provide a solution for it

Thank You,

Vineetha.

0 Likes

Accepted Solutions (0)

Answers (2)

Answers (2)

PriyankaChak
SAP Champion
SAP Champion
0 Likes

Hi Vineetha,

Modify the below code as per your requirement.

try{

if (val.isInteger() == true)

{

def modifedVal = val.toInteger()

}

}

catch(Exception ex) {

}

Here val is the field name.

Regards,

Priyanka

Sriprasadsbhat
Active Contributor
0 Likes