Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
former_member186851
Active Contributor
4,071

Dear SCN Users,

Adding some more UDFs in addition to the mentioned UDFs in the below blog which can be reused.

USEFUL UDFs


1.UDF For Mapping Trace:

This UDF is to get the Values executed in the UDF . Very useful for debugging purpose.

Code:


MappingTrace trace=container.getTrace();

trace.addInfo("Input Value ---->"+strVal);

return strVal;


2.UDF For removing duplicate/repeated values in Queue/Context:


Below UDF can be used to remove the duplicate or repeated values in queue/context. If execution type is selected as Queue, duplicate values in queue are removed. Same applies for context.


Code:


int k=0;

String b[]= new String[100];

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

{

char flag=0;

if(inputStr[i].equals(ResultList.CC))

{

result.addValue(ResultList.CC); 

  flag=1;

}

else

{

  for(int j=i+1;j<inputStr.length;j++)

   if(inputStr[i].equals(inputStr[j]))

      flag= 1;

}

if (flag==0)

{

b[k]=inputStr[i];

    result.addValue(b[k]);

k++;

}



3.UDF For Generating Unique/Random Value(UUID):


Below UDF can be used to generate random unique value. This can be also used in Soap header in which particular fields might required UUID to be generated.No input is required for this UDF.


Code:


public String uniqueValueGeneration(Container container) throws StreamTransformationException{

  UUID uuid = UUID.randomUUID();                           // In Java UUID is a keyword.

  String randomUUIDString = uuid.toString();

   return randomUUIDString;

}



4.UDF To convert Decimal to Integer Values:


Below UDF converts Decimal value to Integer value.


Code:


if(!decVal.equals("") && (decVal.length()>0))

{

    float floatVal = Float.parseFloat(decVal);

    int res = (int)floatVal;

    return String.valueOf(res);

}

else return decVal;




5.Java Code to Add Time:


The below code was written to add a delay of 4 hours to the time field. We had this requirement to add a time difference in Soap header for authentication purpose. The same logic can be used  in UDF for time based functionalities.


Code:


import java.util.*;

import java.lang.*;

import java.text.*;

public class AddingTime {

public static Object AddTime(String TimeSent)
{
  SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");             
  Calendar cal = Calendar.getInstance();
  try
  {
    Date requiredDate = (Date)df.parse(TimeSent);
    cal.setTime(requiredDate);
    cal.add(Calendar.HOUR, Integer.parseInt("4"));
  }
  catch(Exception e)
  {
  }
  return (df.format(cal.getTime()));

 
 
}

}

Input Example:2016-09-18 T 06:04:20'56         

Output Example:2016-09-18 T 10:04:20'56         


2 Comments
Labels in this area