cancel
Showing results for 
Search instead for 
Did you mean: 

example of simple User Defined Function : gives last part of string

Former Member
0 Kudos
62

I made this function because we needed a modulo operation over a sum of values.

this makes the string containing sum being of variable length. then the substring

operator cannot reliably give me a fixed amount of positions on the end of the string. also, a Modulo operator is missing from XI 3.0 SPS 18.

Thus I needed a UDF. I could not find a decent simple example here (my bad, I'm sure...)

Stilll here ?

Then, open you graphical mapping editor and create a new UDF (see lower left corner),

specify a name and description,

set cache:value,

create two input parameters (input string and string containing the number of position desired).

no further imports of java archives are required.

then enter your code

automatically generated header :

<b>public String LastPos(String instring,String sublength,Container container){</b>

You code in the edit window :

<b> //write your code here

int subintvalue = 0 ;

String myresult = "" ;

subintvalue = Integer.parseInt(sublength,10); // extract number from string

if (subintvalue < 1)

{subintvalue = 0; // do some sanity checking

} ;

if (instring.length() > subintvalue)

{ myresult = instring.substring((instring.length() - subintvalue), instring.length());

} // if the input is large enough, get the last part of the string

else

{ myresult = instring ; // else just return the whole input string

};

return myresult ;// put out your truncated string </b>

last generated item :

<b>}</b>

that's all. now learn java

One small question : I would like to document all created UDF's

how do I find these in a smarter way than reviewing all interfaces and all mappings ?

And is there a way to print/export the UDF including it's settings ?

XI is BAD when it comes to printing any content...

(otherwise I could have shown the usage of the

LastPos udf in a mapping here....)

Hope this helps you when the day comes you will need a UDF.

Accepted Solutions (0)

Answers (1)

Answers (1)

Former Member
0 Kudos

pity the forum software mauls the layout of the java code..

Former Member
0 Kudos

Ronald,

I have a simular problem i think.

for a mapping i need the 4th position till 7th or 8 position of a idoc field, depends on value of idocfield. This I then have to map together with a another field with a concat.

Working with substring gives me no good results.

Is this also possible with your UDF?

I receive a error at your line:

"subintvalue = Integer.parseInt(sublength,10); // extract number from string"

What is the purpose exactly of this line?

Former Member
0 Kudos

in the graphical mapping, you define (for instance) a constant, containing the

string "3" (as an example).

you connect this constant to the lower input of lastpos (with an arrow, in the graph. mapp. editor).

the line of code you mention converts the string"3" to the number 3.

then lastpos gives you the last 3 characters of the input string.

if you want to use my function, you will need to calculate the length of the

string you want instead of using a constant.

what will happen in your case :

you have string 123456789 as input

using my function with "6" gives string 456789, starting at pos. 4 of the input

but if you have 12345678

then it gives 345678.

so, it does not start at the same position in the input string.

I think substring should work with parameter 1 set to 4 and paramater 2 set to 0

this should give everything following position 3.

beware of concat when contexts play a part !

Former Member
0 Kudos

Thanks for the explanation.

I am dealing with contexts.

Former Member
0 Kudos

geen probleem, groeten van Luc en Kamran