Skip to Content Former Member

# UDF based on Field Length Change

Hi All,

'MATNR' field size it contains 18 characters length but when I trigger materials having more than 10 digits it throws an error.

Is there any UDF or Java code wherein if material size is '9' it should add 9 zeros in front followed by material(000000000+material).

if material size is '10' it should add 8 zeros in front followed by material(00000000+ numeric material).

if material size is '11' it should add 7 zeros in front followed by material(0000000+material).

if material size is '12' it should add 6 zeros in front followed by material(000000+material).

if material is alphanumeric it should not add any zeros in front.

Please help. Many Thanks.

##### Add comment
10|10000 characters needed characters exceeded

### 5 Answers

• Best Answer
Aug 18, 2015 at 02:58 AM

Hi Rajesh,

Here is a code: testing PS: I have not used for-loop to pad the zeroes. The reason is that you only have to pad for four inputs (9, 10, 11 and 12). Using an if-else is much faster in this case.

Regards,

Mark

test.png (16.9 kB)
test.png (6.8 kB)
##### Add comment
10|10000 characters needed characters exceeded
• Mark Dihiansan Former Member

Hi Rajesh,

.matches should be 0-9 only instead of 0-12. The digits are from 0-9 only 😊

Regards,

Mark

• Aug 17, 2015 at 11:04 AM

Hi,

Please try below UDF:

int len=input.length();
for(int i=0; i<18-len;i++)
{
if(!(Character.isDigit(input.charAt(i))))
return input;
else
{
input="0"+input;
}
}
return input;

##### Add comment
10|10000 characters needed characters exceeded
• Former Member

Hi Sahithi,

Thanks alot for the help.

But when material starting with numeric followed by alphanumeric it adds zeros in front.

If material starting with alphabet followed by numeric it is not adding.

That should not be the case.

If any material having alphabets it should not add and it should add zeros only for numeric materials.

Please help. MAPPP.jpg (38.3 kB)
• Former Member
Aug 17, 2015 at 11:26 AM

Hi Rajesh,

Can you try below udf and check the outcome: String main = "0123456789";

int flag=0;

String res="";

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

{

if(main.contains(String.valueOf(matnum.charAt(i))))

continue;

else

{

flag=1;

break;

}

}

if(flag==0)

{

int len =matnum.length();

for(int i=0;i<(18-len);i++)

res=res + "0";

res=res.concat(matnum);

}

else

res=matnum;

return res;

1.PNG (7.7 kB)
##### Add comment
10|10000 characters needed characters exceeded
• hi vishnu just an improvement as i also had to use this alphanumeric thing so i had a shorter way of finding if its alphanumeric

try{

int i = Integer.parseInt(var);

//Code for numeric

}

catch(Exception e)

{ //code for alphanumeric

}

• Former Member
Aug 18, 2015 at 07:02 AM

Hi Rajesh,

Can you try below udf and check the outcome: String main = "0123456789";

int flag=0;

String res="";

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

{

if(main.contains(String.valueOf(matnum.charAt(i))))

continue;

else

{

flag=1;

break;

}

}

if(flag==0)

{

int len =matnum.length();

for(int i=0;i<(18-len);i++)

res=res + "0";

res=res.concat(matnum);

}

else

res=matnum;

return res;

1.PNG (7.7 kB)
##### Add comment
10|10000 characters needed characters exceeded
• Former Member Former Member

Hi Rajesh,

You just need to pass the ITEMID as the input to the udf. result is a variable used in the udf.

From the udf according to the code snippet, the IDTNR contains the value as needed.

Regards

• Aug 18, 2015 at 11:42 AM

Hi Rajesh,

My suggestions:

So you need 2 steps..

1. check is numeric?

2. pad up to 18 leading zero if is numeric, otherwise remain.

Suggest to use one udf IsNumeric to do checking, then reuse standard available formatNumber to add leading zero.

Reuse whenever possible. If cannot reuse, create something that can be reuse.

If still want do 2 steps in one udf, probably need to name the udf some thing like "pad18ZeroIfNumeric", instead of "padZeroes" only to avoid misleading future person who maintain this mapping. IsNumeric function. Here have to use Double class because Integer class maximum support until 2147483647 which around 10 characters only, while you have 18 characters. Regards,

Yee Loon

##### Add comment
10|10000 characters needed characters exceeded