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
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;
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;
5.UDF to get the last value in the queue\Context
int i=EmpTime.length;
result.addValue(EmpTime[i-1]);
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”);
}
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]);
}
}
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);
}
9. Remove Decimal(This UDF can be used to remove special characters as well)
String Out= EmpAllowances.replace(“.”,””);
return Out;
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;
Hi,
Thanks for sharing.
BR,
Dipen.
Hello Dipen,
Welcome.
Will add more UDFs soon.
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.
Sure Rajesh 🙂
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????
Hi Rajesh,
You can check below Wki.
Remove Leading and Trailing Zeros from a String - Java Development - SCN Wiki
Regards,
Praveen.
Hi Raghu,
Thanks for sharing this information.
Regards,
Abhi
welcome Abhinav
Useful information Abhinav..
Thank you for information !
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.
Regards,
Praveen.
Hi Raghu,
Thanks a lot for sharing 🙂 . Very helpful information.
Regards,
Raja
Welcome Rajasekhar. 🙂
Hi Raghu,
Helpful information.
Regards
Krishna
Thanks Krishna. 🙂
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.
Hello Rajesh,
You can try with ReplaceAll function.
If you get doubts tell me,I will try to give exact code.
Hi Raghu,
Yes please, your valuable inputs and suggestion highly required on below thread.
Remove following characters " \ , # from field and Replace with Space
Done Rajesh..:)
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
Manohar if you using FCC can you try with document offset?
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