Dear SCN Users,

Collected some UDFs which will be used in most of the scenarios.

1.UDF to get the input filename and write the output file with input filename

Capture.JPG

DynamicConfiguration conf = (DynamicConfiguration) container.getTransformationParameters().get(StreamTransformationConstants.DYNAMIC_CONFIGURATION);


DynamicConfigurationKey key = DynamicConfigurationKey.create(“http://sap.com/xi/XI/System/File“, “FileName”);

DynamicConfigurationKey key1 = DynamicConfigurationKey.create( “http://sap.com/xi/XI/System/File”,”FileName“);

String filename=”output_”+conf.get(key);

conf.put(key1,filename);

return filename;

2.UDF to add two timestamps

import java.text.DateFormat;

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.Calendar;

import java.util.Date;

import java.util.GregorianCalendar;

int date1 = var1;// first argumnet in UDF

int date2 = var2;//Second Argumnet in UDF

Date date = null;

String date1String = “” + date1;

DateFormat df = new SimpleDateFormat(“yyyyMMdd”);

try {

date = df.parse(date1String);

} catch (ParseException pe) {

pe.printStackTrace();

}

GregorianCalendar cal = new GregorianCalendar();

cal.setTime(date);

cal.add(Calendar.DAY_OF_YEAR, date2);

Date Cal = cal.getTime();

String newstring = new SimpleDateFormat(“MM/dd/yyyy”).format(Cal);

 

return newstring;

3.Getting the last 3 digits(If there are only 2 then only last 2 and so on).

if(EmpId.length()>3)

{

EmpId=EmpId.substring(EmpId.length()-3,EmpId.length());

return EmpId;

}

else

return EmpId;

Capture.JPG

4.Making the String to particular number of digits(In this case I am making it as 10 digits adding ‘X’ to fill the spaces)

if(empName.length()<10)

{

for(int i=empName.length();i<10;i++)

{

empName=empName+”x”;

}

return empName;

}

else

return empName;

Capture.JPG

5.UDF to get the last value in the queue\Context

int i=EmpTime.length;

result.addValue(EmpTime[i-1]);

Capture.JPG

6.UDF to check the duplicate Entries in the queue(In this case if the entry is first then’ FirstEntry’ else ‘ Duplicate is populated)

if(EmpDetails.length==2)

{

if(EmpName[0]==EmpName[1])

{

result.addValue(“True”);

}

else

result.addValue(“False”);

}

Capture.JPG

7. UDF to add context change(In this case after every 2 values)

int count = 0;               

           int j =0;

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

               {
                 j++;

               
                  if(j == 2&& i <EmpGift.length-1)

                       {

                               result.addValue(EmpGift[i]);

                               result.addContextChange();                               

                               count++;

                               j =0;

                       }

               
                       else

                       {

                               result.addValue(EmpGift[i]);

                       }

                       
               }

Capture.JPG

8.UDF to Sort Elements in the queue

int var2=0;

Arrays.sort(var1);

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

{

var2 =var1[i];

result.addValue(var2);

}

Capture.JPG

9. Remove Decimal(This UDF can be used to remove special characters as well)

String Out= EmpAllowances.replace(“.”,””);

return Out;

Capture.JPG

10. Rounding off to 3 Decimal digits

java.math.BigDecimal bd  = new java.math.BigDecimal(var1).setScale(2,java.math.BigDecimal.ROUND_HALF_UP);   

String s=bd.toString();

return s;

Capture.JPG

To report this post you need to login first.

19 Comments

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

      1. Rajesh PS

        Hi Raghu,

        Thanks a lot for sharing. 🙂

        Please add description to each line so that we can clearly understand since I’m new to JAVA Mapping & UDF’S.

        Also Kindly share us some more UDF’S.

        (0) 
          1. Rajesh PS

            Hi Raghu,

            Need a help from you. This is regarding the UDF to adjust the material sizes.

            Below is the screen shot wherein for a specific division GP it is adding zero’s in front. I wanted to remove the zero’s it when it is getting stored in FTP target directory.(IDOC to file scenario).

            I tried your 3rd UDF but the material sizes vary like:

            USFWM2857BR -11

            FMJN67- 6

            NTK42140VR40F -13

            ARSH3466 – 8

            FMSH017 – 7

            FMTREXP005 -10

            FMSH00201 – 9

            USFW39776DBR – 12

            000000000414461636 – 18

            Should we need to maintain any condition. Is there is any solution for this????

            HRT.jpg

            (0) 
  1. Praveen Gandepalli

    Hi Rajesh,

    You need to put return statement after the for loop, use below code, in future better to open a new thread instead of reply to blogs and old threads.

    removeLeadingZeros.png

    Regards,

    Praveen.

    (0) 
      1. Rajesh PS

        Hi Raghu,

        Very helpful information.

        Could you please provide your inputs and suggestions on Removing only specific Special characters  ”  ,  \  # and replace with space

        Thanks much.

        (0) 

Leave a Reply