UDF Basic Building Block - SAP Blogs
UDF Basic Building Block - SAP Blogs
UDF Basic Building Block - SAP Blogs
Community
Follow Update 29-Apr-2015: Added 2 examples for get values based on another field that matched condition string.
Update 27-Apr-2015: Added 7 examples for Handle leading and trailing zero and space trimming and padding, 2 example for Context handling.
Like Update 26-Apr-2015: Added 2 examples for Delimited string to context values and the reverse way.
RSS Feed
Introduction
UDF can be written for many different purpose, and with many different way of coding. Still, there should be some reusable coding/statement/patterns that keep on occurs again and
again. In this document, is my attempt to list down some common coding/statement and patterns that known. Below meant to be building blocks, mean not use it standalone, and
https://blogs.sap.com/2015/04/20/udf-basic-building-block/ 1/17
2/19/22, 10:40 AM UDF Basic Building Block | SAP Blogs
should mix and match with other building blocks to come out something useful. It just like LEGO
Condition Test if context values existed (not null). if (contextValues != null && contextValues.length > 0)
Normally this should be first checking for
contextValues, if empty context(null) then do
nothing.
Condition Test if has value (non-empty). Normally if if (value != null && value.trim().length() > 0)
false, sub-sequence coding will return result
with empty string only.
Looping Single loop at contextValues, get each value for (int i = 0; i < contextValues.length; i++){
https://blogs.sap.com/2015/04/20/udf-basic-building-block/ 2/17
2/19/22, 10:40 AM UDF Basic Building Block | SAP Blogs
Looping Create target node if some condition is met. for (int i = 0; i < contextValues.length; i++){
Pattern String value = contextValues[i];
Suggested function name:
Looping Pass value to target node if some condition is for (int i = 0; i < contextValues.length; i++){
Pattern met. String value = contextValues[i];
Looping Return true if some condition is met, for (int i = 0; i < contextValues.length; i++){
Pattern otherwise false. String value = contextValues[i];
https://blogs.sap.com/2015/04/20/udf-basic-building-block/ 3/17
2/19/22, 10:40 AM UDF Basic Building Block | SAP Blogs
Looping Return single true if some condition is met for String conditionMet = “false”;
Pattern values in context, otherwise single false.
for (int i = 0; i < contextValues.length; i++){
String value = contextValues[i];
contextIs<SomeConditions> }
result.addValue(conditionMet);
Looping Outer and inner loop at contextValues and for (int i = 0; i < contextValues.length; i++){
Pattern secondContext, get both outer and inner String value1 = contextValues[i];
value to compare and do something.
}
getLastValue
Context Split each value in contexts with context for (int i = 0; i < contextValues.length; i++){
change. Simulate splitByValue(ForEach).
https://blogs.sap.com/2015/04/20/udf-basic-building-block/ 4/17
2/19/22, 10:40 AM UDF Basic Building Block | SAP Blogs
result.addValue(value);
result.addContextChange();
}
Context Remove all context change in contextValues. for (int i = 0; i < contextValues.length; i++){
Simulate removeContext.
String value = contextValues[i];
if (!ResultList.CC.equals(value)){
result.addValue(value);
}
Context Return only unique values, remove duplicated List uniqueList = new ArrayList();
values.
if (!uniqueList.contains(value)) {
uniqueList.add(value);
result.addValue(value);
https://blogs.sap.com/2015/04/20/udf-basic-building-block/ 5/17
2/19/22, 10:40 AM UDF Basic Building Block | SAP Blogs
}
input2 = delimiter
result.addValue(splitString[j]);
}
https://blogs.sap.com/2015/04/20/udf-basic-building-block/ 6/17
2/19/22, 10:40 AM UDF Basic Building Block | SAP Blogs
if(i == 0){
}
else{
}
result.addValue(fullString);
if (!ResultList.SUPPRESS.equalsIgnoreCase(value)){
result.addValue(value);
}
https://blogs.sap.com/2015/04/20/udf-basic-building-block/ 7/17
2/19/22, 10:40 AM UDF Basic Building Block | SAP Blogs
input2 = q2
result.addValue(q1[i]);
result.addValue(q2[i]);
if(conditionValues[i].equalsIgnoreCase(condition)){
result.addValue(contextValues[i]);
}
https://blogs.sap.com/2015/04/20/udf-basic-building-block/ 8/17
2/19/22, 10:40 AM UDF Basic Building Block | SAP Blogs
else{
result.addSuppress();
}
if(conditionValues[i].equalsIgnoreCase(conditions[j])){
break;
https://blogs.sap.com/2015/04/20/udf-basic-building-block/ 9/17
2/19/22, 10:40 AM UDF Basic Building Block | SAP Blogs
}
}
if(found){
result.addValue(value);
}
else{
result.addSuppress();
}
Zero Trim leading and trailing zero. FormatNum can achieve this requirement. Below UDF remain as
learning purpose only, please use standard FormatNum instead.
Using UDF:
FormatNum:
https://blogs.sap.com/2015/04/20/udf-basic-building-block/ 10/17
2/19/22, 10:40 AM UDF Basic Building Block | SAP Blogs
if(idx != -1){
if (output.trim().length() == 0) {
output = “0”;
if(output.startsWith(“.”))
return output;
Space Trim leading space at left side. String output = value.replaceAll(“^\\s*”, “”);
return output;
https://blogs.sap.com/2015/04/20/udf-basic-building-block/ 11/17
2/19/22, 10:40 AM UDF Basic Building Block | SAP Blogs
Space Trim trailing space at right side. String output = value.replaceAll(“\\s*$”, “”);
return output;
Space Trim leading and trailing space. Just use standard text trim() function.
input2 = totalLength
long version:
https://blogs.sap.com/2015/04/20/udf-basic-building-block/ 12/17
2/19/22, 10:40 AM UDF Basic Building Block | SAP Blogs
return output;
input2 = totalLength
long version:
return output;
Alert Moderator
Assigned Tags
https://blogs.sap.com/2015/04/20/udf-basic-building-block/ 13/17
2/19/22, 10:40 AM UDF Basic Building Block | SAP Blogs
Related Questions
is it mandatory to use udf in message mapping ?? Accessing CPI endpoint from mta application in SAP BAS
By
santa_clause vinto Jul 18, 2018 By
Uday Shankar Nov 29, 2021
https://blogs.sap.com/2015/04/20/udf-basic-building-block/ 14/17
2/19/22, 10:40 AM UDF Basic Building Block | SAP Blogs
Coffee Corner
Join the new Coffee Corner Discussion Group.
2 Comments
Yee Loon
Trimming leading or trailing zeroes can already be achieved via standard function FormatNum if you specify the pattern according the Java's DecimalFormat.
This was already mentioned in a comment to a recent UDF-related blog (which is already removed because I guess it is no longer relevant). IMHO, best to stick
with standard functions on this unless there is a specific pattern that cannot be achieved with them.
For padding spaces, it can also be achieved by providing appropriate formatting (with Formatter) when using static method String.format(). Also, you can
define the totalLength type as int so no parseInt() is not necessary. Refer sample below.
https://blogs.sap.com/2015/04/20/udf-basic-building-block/ 15/17
2/19/22, 10:40 AM UDF Basic Building Block | SAP Blogs
Rgds
Eng Swee
Like 0 | Share
Hi Eng Swee,
Thanks for feedback and the input. Agreed, use standard function whenever possible. Tested FormatNum and String.format() is working fine for both
zeroes trimming and space padding. UDF document updated too with your input. Thx!
Regards,
Yee Loon
Like 0 | Share
Find us on
https://blogs.sap.com/2015/04/20/udf-basic-building-block/ 16/17
2/19/22, 10:40 AM UDF Basic Building Block | SAP Blogs
Newsletter Support
https://blogs.sap.com/2015/04/20/udf-basic-building-block/ 17/17