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

SAP CPI - Message Mapping - Split Function Output

0 Likes
4,212

Hello together,

I have a problem with my message mapping.

I try to split the following split. As the output i get both parts of the splitted string, but i just want to have the first part.

Format of the String:

008E7S54|XD56

Output:

<messbeleg><id>008E7S54</id><id>XD56</id></messbeleg>


Expected Output:

<messbeleg><id>008E7S54</id></messbeleg>

This is the Split-Function:

https://answers.sap.com/questions/13121983/sap-cpi-split-a-string-at-delimiter-and-add-contex.html

def void addContextChange(String[]input,Outputoutput, MappingContext context){for(item ininput[0].split('|')){output.addValue(item);}}

This is my message mapping.

How is it possible to get the first part of the splitted string as the output?

I hope someone can help me. 🙂

Regards

Henrik

Accepted Solutions (1)

Accepted Solutions (1)

Sriprasadsbhat
Active Contributor

Hello Enrik,

Below should work,

def void addContextChange(String[] input, Output output, MappingContext context) { 
    def item=input[0].split(';')[0]	
    output.addValue(item);   
}

Regards,

Sriprasad Shivaram Bhat

0 Likes

Thank you Sriprasad,

this works fine.

Regards,

Henrik 🙂

Answers (2)

Answers (2)

0 Likes

Hi henrikeffenberger,

Could you try below code as well.

def void addContextChange(String[] input, Output output, MappingContext context) { 
   def item=str[0]?.tokenize(';')[0]
   output.addValue(item);   
}

hope this helps !

thanks and regards,

Praveen T

0 Likes

Hey,

i try the code, but it throws the following exception:

Error Details com.sap.xi.mapping.camel.XiMappingException: Runtime exception during processing target field mapping /messbelege/messbeleg/id. The root message is: Exception:[com.sap.aii.mappingtool.tf7.rt.BehaviorInvocationException: groovy.lang.MissingPropertyException: No such property: str for class: Script7] in class com.sap.aii.mappingtool.tf7.ScriptHandler method addContextChange[[Ljava.lang.String;@524e2080, com.sap.aii.mappingtool.tf7.rt.ResultListImpl@2150a7e2, com.sap.xi.mapping.camel.impl.MappingContextImpl@72b91563, com.sap.aii.mappingtool.tf7.rt.Context@1b78aca6]
RegardsHenrik

Apologies typo error could you try below code.

def void addContextChange(String[] input, Output output, MappingContext context) { 
   def item=input[0]?.tokenize(';')[0]
   output.addValue(item);   
}
daviddasilva
Active Contributor
0 Likes

Hi Henrik,

I think the split function returns an array and this bit of your code:

output.addValue(item)

Suggests to me that maybe it is adding each value in the array in to the output?

I think there should be some way to only add the first item value - maybe something like:

output.addValue(item[0]) - which I think will add the first value in the array only to the output.

Just a suggestion and I could be way off but basically I think you need to find a way to manage the array after it has been split.

Kind regards,

David