cancel
Showing results for 
Search instead for 
Did you mean: 

UDF to count occurance of a particular field of a segment

Former Member
0 Kudos

Hi Friends

Please help me in writing this UDF .

I have a scenario iDOC(WPDBBY01) to file in which i have a requrment like this :

Sender field is E1WPBB03(segment)

the target field is Alt1_Qty to Alt9_Qty

My problem is this segment E1WPBB03 is repetive means can occur more than one time so can the field

so what i want to achieve is if it occurs only 1 time it should go to Alt1_Qty

if it occurs second time then it should be mapped to ALt2_Qty

if 3rd occurance than should map to ALt3_qty

and so on till the 9th occurance .

Please help me in this in the easiest way out as i am a beginner in XI so dont want to go for some UDF if its not necessary.

Points will surely be rewarded

Thanks

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

HI,

Please refer below code for UDF.

Create 2 UDFs "TestCount1 and TestCount2" with Cache parameter queue.

Now in UDf use below code

iDOC(WPDBBY01) --> testCount1 -->Alt1_Qty

iDOC(WPDBBY01) --> testCount2 -->Alt9_Qty

Input parameter as A i.e. iDOC(WPDBBY01)

TestCount1

if(A.length>1)

for(i=0; i<A.length;i++)

result.addValue(A<i>);

TestCount2

if(A.length == 1)

result.addValue(A[0]);

Thanks

Swarup

Former Member
0 Kudos

Swarup

do i have to import anything in the UDF and do i have to define any variables and all that.

I dont know anything on UDF so please be very clear.

Thanks

Former Member
0 Kudos

Hi,

Please use below link to create the UDF in graphical mapping.

1. In Message mapping at left most bottom corner, click on new Icon to create the UDF.

2. Give some name as TestCount1 etc and Select the check box Parameter as "Queue".

3. By default there is one input parameter will be there as "A"

4. The java editor will be open as shown in below snap-shots. (We should thanks to Raj for sharing these snap-shots of UDF)

http://www.flickr.com/photos/8764045@N06/2264412329/sizes/o/

5. Copy paste the above mentioned code in udf and then save and close it.

6. In graphical mapping API list you will get option as User Defined similar to Text, Arithmatic, Date etc.

7. Once the User Defined is select then the TestCount1 will be appear at bottom.

8. Drag and drop in Graphical editor. Give Source field as input paramter as

iDOC(WPDBBY01) --> testCount1 -->Alt1_Qty

9. Similarly do the all procedure to create one more udf TestCount2 and map it as

iDOC(WPDBBY01) --> testCount2 -->Alt9_Qty

For TestCount1 use below code

if(A.length>1)

for(i=0; i<A.length;i++)

result.addValue(A);

For TestCount2 use below code

if(A.length == 1)

result.addValue(A[0]);

Let me know if you are able to successfully do this.

Thanks

Swarup

Former Member
0 Kudos

Shall i go with ur 2 UDF or will raj single UDF will work ?

Thanks

justin_santhanam
Active Contributor
0 Kudos

Sharma,

I've given perfect screenshots of mapping, with results too. But still u've doubt whether mine works, ok that's fine.Thnxs Buddy.

raj.

Former Member
0 Kudos

Hi,

I think you need to go with two UDFs, Raj had given the snap-shot of single UDF that way the two UDFs will be required.

On the target end we have two fields to map...and the condition that we are implying will lead to go for two seperate entities.

Better suggestion will be to try it out, if everything works fine with single UDF then its better to go for it.

But I think still there is doubt about it. so can go ahead with two UDFs if timeline is at peak and not able to do much R&D on it.

Thanks

Swarup

Edited by: Swarup Sawant on Feb 15, 2008 5:21 AM

Former Member
0 Kudos

Raj one more question can i give the segment as the argument also as i have to read the occurance of segment or is it like we cant give segment as an argument.

Thanks

justin_santhanam
Active Contributor
0 Kudos

Hello,

We can pass segment as argument too. But I framed the logic as per ur latest reqmt that if ABC segment is present then map subelement MAT to ALT. I did only for this logic, what u want to do after counting the no.of segments do u want to map to someother field.

raj.

Former Member
0 Kudos

Hi Raj

can i mail you on your personal id about the full req .

Thanks

Former Member
0 Kudos

Hi Raj

yes we have to count the occurance of the Segment and than map the fields.

Something of this sort:

Alt1_QTY --

Take the first occurrence value of MAT_QUAN from the iDOC

Alt2_QTY

if MAT_NR from E1WPBB02 is same as MAT_NR from E1WPBB04, then take the value is "0",

elseif E1WPBB03 has second occurrence value is MAT_QUAN from the second occurance of the segment.

else value is "1"

AlT3_QTY

if E1WPBB03 has third occurrence value is MAT_QUAN from the 3rd occured segment

elseif E1WPBB03 has two occurences then value is "1",

else value is "0"

so on till Alt 8

ALT9_QTY

if E1WPBB03 has Ninth occurrence value is "1"

else value is "0"

this is my exact requirment please help me

Thanks

Former Member
0 Kudos

Any clue how can i achieve this req please help

Thanks

Answers (1)

Answers (1)

Former Member
0 Kudos

Hi,

I suggest rather than creating 9 separate target field in target structure create only one target element with name Alt_Qty and set the occurance of target element to unbounded.

So as many times ur source repeats that many times ur target Alt_Qty will repeate and will be filled.

This is the best way to do it rather than writing UDF.

Thnx

Chirag

Former Member
0 Kudos

Chirag

i cant do it with your way as i have to put some other logic also in it so i have to go for UDF.

What i actually want to acieve is want to count the number of times this segment has occured if its count value is 1 than do this if its count value is 2 thyan do this if count value 3 than this and so on........

Please help

thanks

Former Member
0 Kudos

Here is some more detail for the same

let us say segment ABC occur only once than the field let us say MAT is mapped to ALT1

if Segment ABC occur second time than the field MAT which is there in the second occurance of the Segment ABC should be mapped to ALT2

if segment ABC occur 3rd time than the field MAT there in the 3rd occurance of the segment ABC should be mapped to ALT3

and so on...

hope i have given some more clearity.....

Thanks

justin_santhanam
Active Contributor