on 02-11-2019 5:51 AM
Hi,
I have requirement to duplicate the field as many times as the source field is more than 200 .
If field_name (source field) is more than 200 character then the target field(Tag_name ,0..*) should be updated with 200 char . Now target field(Tag_name ,0..*) should be duplicated and update with next more than 200 char ( and so on.
please help me to achieve this logic
Hi Anurag
There are two things, you need to accomplish:
You can do both in a single script. Here's the code:
import com.sap.it.api.mapping.*
def void splitString(String[] fieldName, String[] fieldValue, String[] newLength, Output output, MappingContext context) {
// Find the FieldValue belonging to the FieldName value "MessageText".
def originalText = fieldValue[fieldName.findIndexOf { name -> name == "MessageText"}]
def len = newLength[0].toInteger()
def smallerStrings = originalText.toList().collate(len)
smallerStrings.each { l ->
output.addValue(l.join())
}
}
Here's how to wire the script up in your mapping:
Note that the FieldName and FieldValue elements both have their context set to the root element, to remove any context changes.
Also note that I pass the length, you want to split the string into, as a parameter. I used the value 10 for testing. Change this to 200, or update the script to hardcode that value.
Here's the result when I execute the mapping:
Splitting the string into smaller strings is achieved by that originalText.toList().collate(len) construct. There are more effective ways to do that, but none that are shorter 🙂
Regards,
Morten
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Morten,
This is my question.
https://answers.sap.com/questions/12845338/udf-split-single-value-to-multriple-structure.html
Regards.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hello Morten Wittrock,
I have a similar case, I have 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.
Thank you,
Greetings.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
88 | |
10 | |
10 | |
9 | |
7 | |
7 | |
6 | |
5 | |
4 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.