cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

SAP CPI - Groovy script to split a string and make an array list

veenagayathri
Explorer
0 Likes
10,586

Hi guys,

I am writing the below code to split the string(separated by comma) into a list array.

I am giving multiple values to "source value" in content modifier.

mycontentmodifier.png

Code:

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

import java.util.HashMap;

import java.util.regex.*;

import groovy.xml.*;

def Message processData(Message message) {

def headers = message.getHeaders();

def rows;

def properties = message.getProperties();

def credentials = properties.get("UserList");

List<String> myList = Arrays.asList(credentials.split(","));

for(i=0;i < myList.size();i++)

{

rows = rows + (myList[i]+"\n");

}

// rows = rows.replace("null","");

message.setBody(rows)

return message;

}

But when I run this, I was able to get the below output.

Output:myoutput.png

I am getting a null at the beginning, I can simply replace it. I also tries to assign null value to my variable"rows". But still I am getting the same output. I want to know what is wrong with my groovy code.

Thank you!

View Entire Topic
asutoshmaharana2326
Active Participant
0 Likes

Dear Veena,

This issue can be fixed by various ways. I would say just define rows as "" while initializing.

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

import java.util.HashMap;

import java.util.regex.*;

import groovy.xml.*;

def Message processData(Message message) {

def headers = message.getHeaders();

def rows = "";

def properties = message.getProperties();

def credentials = properties.get("UserList");

List<String> myList = Arrays.asList(credentials.split(","));

for(i=0;i < myList.size();i++)

{

rows = rows + (myList[i]+"\n");

}

// rows = rows.replace("null","");

message.setBody(rows)

return message;

}
veenagayathri
Explorer
0 Likes

Hi,

Yes. It worked. I should define "" to my variable. It is just my mistake!

Thank you so much.

Regards,

Veena