on 08-16-2019 4:20 PM
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.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
User | Count |
---|---|
87 | |
10 | |
10 | |
9 | |
7 | |
6 | |
6 | |
5 | |
4 | |
3 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.