on 07-15-2011 5:34 AM
Dear team,
I need UDF for leading zero's in my message mapping.
Source and Target both structures contains matnr field.But we have to maintain total 18 chars at target matnr , because RFC in R/3 needed.So I want to add leading zero's when mapping. Can anybody provide the UDF code.
Matnr[source]--->split by value(eachvalue) ->UDF->Matnr[Target]
Ex::if source matnr conatains value "9846538" ; I need matnr value at target side like "000000000009846538"[total 18 chars].
-Drumi
Hi,
You can use the standard function Format number for formating the number to 18 digit. it will add the zero's at start of number.
Regards,
Harish
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Harish,
I have a question... in my mapping, from source side I may get matnr as 700345 or 14AF3B where if matnr is 700345, then 12 leading zeros should be added, if 14AF3B comes then it should be same 14AF3B on target side... for this format number function is not possible..
can you suggest what needs to be done when matnr is numeric, there should be leading 12 zeros, alphanumeric then it should be same as source side?
Hi Drumi, Sanjay,
Here is the UDF which will suffice your requirement fully:
Parameters: input (material), fieldLength (18 in your case)
int len = Integer.parseInt(fieldLength);
int inputLength = input.length();
try {
Integer.parseInt(input);
// If it is an integer, add 0 (len - inputLength) times
for (int i=0; i< len-inputLength;i++)
input = "0" + input;
return input;
} catch (NumberFormatException numForEx) {
// return as it is, if alphanumeric
return input;
}
This will take care of alpha-numeric case as well
Hope it helps!
Ambrish
Hi Ambrish,
Thanks a lot for reply... guess it is not working..
I need single UDF which should satisfy both MATNR numeric and alphanumeric cases..
Like if MATNR is 107520 or 107 or 10789520 which means it could be more than 6 numeric numbers or less than 6 numeric numbers, then on target side it should be total 18 digits(MATNR value and zeros before MATNR) given below scenarios...
1) Source: 14AFB6 Target side: 14AFB6 (if alphanumeric, then same should populate on target side)
2) Source side: 107520 Target side : 000000000000107520 (total 18 digits = (12 zeros + 6 numbers)
3) Source side: 107 Target side : 000000000000000107 (total 18 digits = 15 zeros + 3 numbers)
4) Source side: 10789520 Target side: 000000000010789520 (total 18 digits = 10 zeros + 8 numbers)
All these two numeric and alphanumeric should be in single UDF..
The above one is throwing error message.. Will update error message later as I don't have access to server now... But TQ for trying.. however still waiting for your suggestions to get it done..
Thanks in advance..
Hi Sanjay,
This is a working piece of code.
Just need to make sure that you have provided parameter names as below:
input (material), fieldLength (constant 18 in your case).
Execution type is Single Values
Use standard trim function after material before the UDF, to make sure that spaces before and after the input value are flushed out.
use constant as 18 for fieldLength parameter to the UDF.
Hope it works else revert back with the error you are getting.
Ambrish
Hi Ambrish,
Good day...
I have enterd what you said and I got below error... and I gave "input" in place of var1, 18 in place of filedlenght in below program...
=================================
int len = Integer.parseInt(18);
int inputLength = input.length();
try {
Integer.parseInt(input);
// If it is an integer, add 0 (len - inputLength) times
for (int i=0; i< len-inputLength;i++)
input = "0" + input;
return input;
} catch (NumberFormatException numForEx) {
// return as it is, if alphanumeric
return input;
}
====================================
ERROR:
Source text of object Message Mapping: Lubrisur_SHPMNT_SHPMNT05_TO_WMMBXY_WMMBID02_TransferPosting | urn:bp:xi:dwn:lu:common:Logistics:100 has syntax errors:
Function LeadingZeros, Line 1:
cannot find symbol symbol : method parseInt(int) location: class java.lang.Integer int len = Integer.parseInt(18); ^ 1 error
=======================
Please suggest
Hi Ambrish,
the below UDF working perfectly... but really very very much thanks for reply..
input: var1
Execution type: single value
int count = 0;
for (int i = 0; i < var1.length(); i++)
{
if(!Character.isDigit (var1.charAt(i)))
{
count = count +1;
break;
}
}
if (count>0)
{
return var1;
}
else
{
String format = String.format("%018d",Integer.parseInt(var1));
return format;
}
Thnaks once again Ambrish............................
hi Sanjay,
Add one more parameter to the UDF. name it fieldLength. Pass constant 18 in your case.
then instead of code below:
int len = Integer.parseInt(18);
put
int len = Integer.parseInt(fieldlength);
-----------------------------------------------------------------------------------------------------
If you dont want to add a parameter, use code below
int inputLength = input.length();
try {
Integer.parseInt(input);
// If it is an integer, add 0 (18 - inputLength) times
for (int i=0; i< (18-inputLength);i++)
input = "0" + input;
return input;
} catch (NumberFormatException numForEx) {
// return as it is, if alphanumeric
return input;
}
Ambrish
Hi Drumi,
You can try the below piece of code and implement in ur mapping.
String matnr = "";
if(input.length()<18)
{
matnr = "0";
for (int i =1; i < 18-input.length();i++)
{
matnr = matnr + "0";
}
matnr = matnr + input;
}
return matnr;
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Drumi,
UDF:
public void PadWithZero(String[] InputString, int[] Len, ResultList result, Container container) throws StreamTransformationException{
try {
for(int i=0;i<InputString.length;i++)
{
//Check whether the Input string length is less than the requird length
while (InputString<i>.length()< Len[0])
{
//Add Zero
InputString<i>="0"+InputString<i>;
}
//Add Inputstring to result.
result.addValue(InputString<i>);
}
}
othewise :
you can use simple formatNo function with 18 zeros(0) like above post mentioned.
Thanks,
Enivass
Edited by: enivas on Jul 15, 2011 9:12 AM
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
HI,
User the Standard Function "Format Number".
Regards,
Naga.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi,
Use the standard function FormatNum to add the leading zeros
Matnrsource--->split by value(eachvalue) ->formatNum (18 zeros)->MatnrTarget
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
simple solution is using format number as alredy mentioned previous thread.
MATNR>FORMATNUMBER(ENTER 18 ZEORS in Number format)>MATNR
Want UDF TRY BELOW CODE
MATNR argument
if(MATNR!=null){
int I=MATNR.length();
int lengthOfZeroString=0;
if(I<18){
lengthOfZeroString=18-I;
while(lengthOfZeroString>0)
{
MATNR="0"+MATNR;
lengthOfZeroString--;
}
}
}return MATNR;
Regards,
Raj
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
87 | |
10 | |
10 | |
10 | |
7 | |
6 | |
6 | |
5 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.