Skip to Content
Author's profile photo srinu peram

Helpful UDFs – 1

Dear Users,

Here are some UDFs that might me helpful in your business scenarios.

1. UDF for Date Conversion :

          This UDF is used to convert the given date into required time zone and date format, it takes actual date as first input, required time zone as second input and format as third input which is validated based upon the second input. it validates the format of the both inputs and if true, returns actual date otherwise throws an exception. To use this, we need to import the UTIL and TEXT pages.

        Untitled.png

Code :

                       DateFormat df1 =new SimpleDateFormat(DateFormat);

           

                       df1.setTimeZone(TimeZone.getDefault());

           

                        DateFormat df2 = new SimpleDateFormat(DateFormat);

                       df2.setTimeZone(TimeZone.getTimeZone(ReqTimeZone));

                       String target = ” “;

                       try

                      {

 

                       Date d = df1.parse(CurrentDate);

   

                      target = df2.format(d);

                      }

                     catch( Exception e)

                    {}

                    Return target;

  Input :


                    /wp-content/uploads/2016/09/1_1038061.png

Output :


                       /wp-content/uploads/2016/09/2_1038062.png




2. Combining Array Elements:


             This is Queue type UDF.It takes a string and delimiter value as arrays and combines all the array elements with a delimiter and sends as single value.


                      /wp-content/uploads/2016/09/6_1038122.png


Code :

                  String res=””,delimiter=””;

                  res = StrArray[0]

                  delimiter = Separator[0];

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

                  res = res+delimiter+StrArray[i];

                  result.addValue(res);


Input and Output :


                  /wp-content/uploads/2016/09/7_1038123.png


3 . Checking Mandatory Field

                      This is a simple type UDF. It takes a single value and checks if the node exists or not. If the node exists, it returns same value, Otherwise it throws an exception.


                 /wp-content/uploads/2016/09/8_1038153.png


Code :

                  if (!var1.equals(“”)&&(var1.length()>0))

                  return var1;

                  else

                  throw new RuntimeException(“This field is mandatory : value is missing”);


                 

Input   :


                 /wp-content/uploads/2016/09/9_1038154.png


Output     :



                  /wp-content/uploads/2016/09/10_1038164.png


4. Remove Domain Name


               This is simple type UDF. It takes a mail id as input and removes the domain name.


                /wp-content/uploads/2016/09/11_1038165.png


Code :


                  String Pattern = “@xyz.com”;

                  String StrippedString = str.replaceAll(pattern,””);

                  return strippedstring;

             

                                    Input :                                                                            Output                                                                                                        


                  /wp-content/uploads/2016/09/1_1038061.png/wp-content/uploads/2016/09/2_1038062.png

5. Checks for the end of the List

                      This is Queue type UDF. It takes a set of values as input and sends all the values to output until the end of the list field appears for the first time.

                   /wp-content/uploads/2016/09/3_1038191.png

Code :

                 int i = 0;

           

                 int j = 0;

                 // check for the presence of EOD

                 while (!var1[i].equals(“EOD”))

           

                  {

           

                  // add the values till EOD is not present

                  result.addvalue(var1[i]);

                  i=i+1;

                  }

                                         Input :                                                             Output

                    /wp-content/uploads/2016/09/5_1038192.png/wp-content/uploads/2016/09/6_1038122.png

Assigned Tags

      7 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Raghuraman S
      Raghuraman S

      Srinu,

      Already posted some of these UDFS in

      USEFUL UDFs

      Useful UDFS-Part 2

      Author's profile photo srinu peram
      srinu peram
      Blog Post Author

      Hi Raghu, I have removed the existing UDF.

      Author's profile photo Raghuraman S
      Raghuraman S

      Great Srinu..:)

      Author's profile photo suman sourabh
      suman sourabh

      Thanks Srinu for sharing these logics.

      For the 4th UDF to remove the Domain name from the input, your code is working only for fixed domain.

      e.g. for input Name@asdf.com your code wont work,

      So for generic solution we can use some different logic like:

      ........

      ......

      ......

      int i = str.indexOf("@");

      String o = str.substring(0,i);

      return o;

      ....

      .....

      Thanks,
      Suman

      Author's profile photo srinu peram
      srinu peram
      Blog Post Author

      Hi Suman,

      That is also a good idea. As most of the requirements are on fixed domain, I shared that logic.

      Author's profile photo Former Member
      Former Member

      Hi Srinu,

      Thanks for spending time on sharing useful UDFs.

      Can you lease check the below mentioned code in last UDF:

      •    while (!Var1[i].equals("EOD"))
      •     result.addvalue(var1[i]);


      it should be :


      •    while (!var1[i].equals("EOD"))
      •   result.addValue(var1[i]);

      Regards,

      Yeshwanth

      Author's profile photo srinu peram
      srinu peram
      Blog Post Author

      Hi Yeshwanth,

      Thanks for correcting. Updated 🙂