Download as pdf or txt
Download as pdf or txt
You are on page 1of 17

2/19/22, 10:40 AM UDF Basic Building Block | SAP Blogs

Community

Ask a Question Write a Blog Post Login

Yee Loon Khoo


April 20, 2015
| 7 minute read

UDF Basic Building Block


 2  23  8,210

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.

Update 25-Apr-2015: Added DisplayQueue screenshots.

 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

Categor Requirement UDF coding


y

Condition Test if input field existed (not null). if (value != null)


Normally this should be first checking for
value, if null then do nothing.

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.

Condition Test if value is suppress. if (ResultList.SUPPRESS.equalsIgnoreCase(value)))

Condition Test if value is context change. if (ResultList.CC.equals(value))

ResultLis Add suppress to result. result.addSuppress();


t

ResultLis Add context change to result. result.addContextChange();


t

ResultLis Add value to result. result.addValue(value);


t

ResultLis Add boolean true/false to result. Boolean result.addValue(“true”);

t value will be used in next function call.


result.addValue(“false”);

ResultLis Add empty string to result. Empty string will result.addValue(“”);


t create target node.

ArrayList Create new array list. List array = new ArrayList();

ArrayList Add value to array. array.add(value);

ArrayList Test if array contains value. if (array.contains(value))

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

Pattern to do something.   String value = contextValues[i];


  //Do something

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:
 

createIf<SomeConditions>   if(<SomeConditions is true>){


      result.addValue(“”);
 }
  else{
      result.addSuppress();
  }

Looping Pass value to target node if some condition is for (int i = 0; i < contextValues.length; i++){
Pattern met.   String value = contextValues[i];
 

Suggested function name:   if(<SomeConditions is true>){


      result.addValue(value);
passIf<SomeConditions>  }
  else{
      result.addSuppress();
  }

Looping Return true if some condition is met, for (int i = 0; i < contextValues.length; i++){
Pattern otherwise false.   String value = contextValues[i];
 

Suggested function name:


  if(<SomeConditions is true>){
has<SomeConditions>
      result.addValue(“true”);
is<SomeConditions>  }
  else{
      result.addValue(“false”);
  }

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];
 

Suggested function name:   if(<SomeConditions is true>){


      conditionMet = “true”;
contextHas<SomeConditions>
      break;

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.

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


      String value2 = secondContext[j];

      //Compare value1 and value2

      //Do something

  }

Position Get value from contextValues based on input1 = contextValues

index/position. input2 = index(optional, starting at 1)

Suggested function name: by index –> result.addValue(contextValues[index-1]);

first –> result.addValue(contextValues[0]);


getValueByIndex

getFirstValue last –> result.addValue(contextValues[contextValues.length-1]);

getLastValue

Context Split each value in contexts with context for (int i = 0; i < contextValues.length; i++){
change. Simulate splitByValue(ForEach).

  String value = contextValues[i];

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);

  if(i != contextValues.length – 1){

      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.

for (int i = 0; i < contextValues.length; i++){


  String value = contextValues[i];

  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

  }

Context Split input1 full string to context values by input1 = fullString


delimiter specified by input2.

input2 = delimiter

String dchar = delimiter[0];

for (int i = 0; i < fullString.length; i++){

  String[] splitString = fullString[i].split(dchar);

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

      result.addValue(splitString[j]);

 }

Context Concatenate input1 context values to input1 =contextValues


delimited string by delimiter specified by
input2. input2 = delimiter

String fullString = “”;

String dchar = delimiter[0];

for (int i = 0; i < contextValues.length; i++){

  String value = contextValues[i];

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){

      fullString = value;

 }

  else{

      fullString = fullString + dchar + value;

 }

result.addValue(fullString);

Context Remove suppress, return only values


for (int i = 0; i < contextValues.length; i++) {

  String value = contextValues[i];

  if (!ResultList.SUPPRESS.equalsIgnoreCase(value)){

      result.addValue(value);

 }

Context Generate single suppress. (useful for unit result.addSuppress();


testing single function)

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

Context Combine 2 queues into 1 queue. input1 = q1

input2 = q2

for (int i = 0; i < q1.length; i++) {

  result.addValue(q1[i]);

for (int i = 0; i < q2.length; i++) {

  result.addValue(q2[i]);

Context If input1 condition values equal to input2 input1 = conditionValues

single condition string, then get input2 = conditionString

corresponding value from input3 context input3 = contextValues


values.
String condition = conditionString[0];

for (int i = 0; i < conditionValues.length; 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();

 }

Context If input1 condition values equal to any input2 input1 = conditionValues

multiple condition string separated by input2 = conditionString

semicolon, then get corresponding value input3 = contextValues


from input3 context values.
String conditions[] = conditionString[0].split(“;”);

for (int i = 0; i < conditionValues.length; i++){

  boolean found = false;

  String value = “”;

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

      if(conditionValues[i].equalsIgnoreCase(conditions[j])){

        value = contextValues[i];

        found = true;

        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:

String output = “”;

output = value.replaceAll(“^0*”, “”);

Integer idx = value.indexOf(“.”);

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){

  output = output.replaceAll(“0*$”, “”).replaceAll(“\\.$”, “”);

if (output.trim().length() == 0) {

  output = “0”;

if(output.startsWith(“.”))

  output = “0” + output;

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.

Space Pad leading space up to total length.


input1 = value

input2 = totalLength

good short version:

return String.format(“%1$” + totalLength + “s”, value);

long version:

String output = value;

int toLength = Integer.parseInt(totalLength);

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

while (output.length() < toLength) {

            output = ” ” + output;

return output;

Space Pad trailing space up to total length. input1 = value

input2 = totalLength

good short version:

return String.format(“%1$-” + totalLength + “s”, value);

long version:

String output = value;

int toLength = Integer.parseInt(totalLength);

while (output.length() < toLength) {

            output = output + ” “;

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

Cloud Integration | context | udf

Similar Blog Posts 


Difference between IF and IFS Integration Advisor - Integration game changer!
By
Muniyappan Marasamy Feb 17, 2014 By
Sandeep Bheema Oct 11, 2019

Working with Images/PDFs in PI using UDF


By
Former Member May 30, 2013

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

Tenant not driven by this application , Deployment failed


By
BASKAR R Nov 13, 2021

Join the Conversation 


SAP TechEd
Tune in for tech talk. Stay for inspiration. Upskill your future.

SAP BTP Learning Group


SAP Business Technology Platform Learning Journeys.

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

You must be Logged on to comment or reply to a post.

Eng Swee Yeoh


April 28, 2015 at 1:52 am

Yee Loon

Some feedback on your latest additions.

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.

private String padRight(String input, int width) {

return String.format("%1$-" + width + "s", input);

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

Yee Loon Khoo | Blog Post Author


April 28, 2015 at 12:07 pm

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

Privacy Terms of Use

Legal Disclosure Copyright

Trademark Cookie Preferences

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

You might also like