Skip to Content
Author's profile photo Raghuraman S

USEFUL UDFs

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

Assigned Tags

      22 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Dipen Pandya
      Dipen Pandya

      Hi,

      Thanks for sharing.

      BR,

      Dipen.

      Author's profile photo Raghuraman S
      Raghuraman S
      Blog Post Author

      Hello Dipen,

      Welcome.

      Will add more UDFs soon.

      Author's profile photo Former Member
      Former Member

      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.

      Author's profile photo Raghuraman S
      Raghuraman S
      Blog Post Author

      Sure Rajesh  🙂

      Author's profile photo Former Member
      Former Member

      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

      Author's profile photo Praveen Gandepalli
      Praveen Gandepalli

      Hi Rajesh,

      You can check below Wki.

      Remove Leading and Trailing Zeros from a String - Java Development - SCN Wiki

      Regards,

      Praveen.

      Author's profile photo Former Member
      Former Member

      Hi Raghu,

      Thanks for sharing this information.

      Regards,

      Abhi

      Author's profile photo Raghuraman S
      Raghuraman S
      Blog Post Author

      welcome Abhinav

      Author's profile photo vijayprasad talari
      vijayprasad talari

      Useful information Abhinav..

      Author's profile photo Jean-Philippe Pain
      Jean-Philippe Pain

      Thank you for information !

      Author's profile photo Praveen Gandepalli
      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.

      Author's profile photo Rajasekhar reddy
      Rajasekhar reddy

      Hi Raghu,

      Thanks a lot for sharing 🙂 . Very helpful information.

      Regards,

      Raja

      Author's profile photo Raghuraman S
      Raghuraman S
      Blog Post Author

      Welcome Rajasekhar. 🙂

      Author's profile photo Former Member
      Former Member

      Hi Raghu,

      Helpful information.

      Regards

      Krishna

      Author's profile photo Raghuraman S
      Raghuraman S
      Blog Post Author

      Thanks Krishna. 🙂

      Author's profile photo Former Member
      Former Member

      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.

      Author's profile photo Raghuraman S
      Raghuraman S
      Blog Post Author

      Hello Rajesh,

      You can try with ReplaceAll function.

      If you get doubts tell me,I will try to give exact code.

      Author's profile photo Former Member
      Former Member

      Hi Raghu,

      Yes please, your valuable inputs and suggestion highly required on below thread.

      Remove following characters " \ , #  from field and Replace with Space

      Author's profile photo Raghuraman S
      Raghuraman S
      Blog Post Author

      Done Rajesh..:)

      Author's profile photo Former Member
      Former Member

      Hi Raghuraman

      you have any code that skips 1st 6 lines in the text file

      if you know please give me the code it is important to me

      thanks and regards

      manohar

       

      Author's profile photo Raghuraman S
      Raghuraman S
      Blog Post Author

      Manohar if you using FCC can you try with document offset?

       

      Author's profile photo IRL BASIS
      IRL BASIS

      Hello,

       

      In 7th Udf for context change, constant value is given as 2. But, I need dynamic value which is coming from source and according that context will be created. Is it possible via this UDF?

       

      regards,

      Amar