cancel
Showing results for 
Search instead for 
Did you mean: 

UDF SPLIT single value to multriple structure

rivasch
Explorer
0 Kudos

Hello

I need to move from a field 0: 1

field1: "value1, value2, value3"

To a structure 0: *

<item>

<field1> value1 </field1>

</item>

<item>

<field1> value2 </field1>

</item>

<item>

<field1> value2 </field1>

</item>

I am new to UDF and need help, any suggestions.

groovy code:
import com.sap.it.api.mapping.*

def void extParam(String[] P1, Output output, MappingContext context) {

String[] list = P1[0].split(",");

if (list.length > 0) {

for (int j = 0; j < list.length; j++) {

output.addValue(list[j]);

}

}

}

Thank you,

Greetings.

Accepted Solutions (1)

Accepted Solutions (1)

MortenWittrock
Active Contributor
0 Kudos

Hi Miguel

You are very close. Your function is correct, you just need to wire it up. The output of the function should be mapped to the item element, to create three of those, and mapped to the field1 element, to add the actual values.

For the latter, you need to create context changes, so that the three values can be assigned correctly to the three field1 elements.

Here's the mapping:

You'll find the splitByValue function under Node Functions. Use its default configuration, which is to add a context change after each value.

I get the following output:

Don't worry if you don't immediately get the idea of context changes; nobody does when they encounter them for the first time 🙂

About your function: In my opinion, it needs a better name. Also, the code itself could be more Groovy-like (and at the same time shorter). Here's an updated version:

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

def void splitByComma(String[] input, Output output, MappingContext context) {
    input[0].split(",").each { v -> output.addValue(v) }
}

Have fun,

Morten

rivasch
Explorer

Hi Morte,

Thanks for the update of the function and name, I did not have in mind the context changes.

This is really helpful.

Regards.

Answers (0)