cancel
Showing results for 
Search instead for 
Did you mean: 

Add multiple fields in to single target fields using UDF

former_member196519
Participant
0 Kudos

Hi Team,

I have to add fields’ value in to target single field. I cannot use “add” because it does not work for empty value and I can also not add”mapwithdefault”. I am using SAP PI 7.1. Could you please suggest if there is any UDF for this.

Source Structure:

Source1

          Field1

         Field2

         Field3

         Field4

Source2

          Field12

         Field22

         Field32

         Field42

There are two source nodes, two Idoc will be generated in my scenario. I have to add Field1, Field2 & Field3 in to target single Idoc field. And same for Source2 for another Idoc.

Thanks in advance for your response!

Kumar

Accepted Solutions (1)

Accepted Solutions (1)

anupam_ghosh2
Active Contributor
0 Kudos

Hi Kumar,

                    You have not mentioned the data type of the input fields (floating point,integer etc). In any case the following UDF is sufficient to meet your requirement.

public static String addValues(String v1,String v2,String v3)

{

         double sum=0;

         String s="";

         if(v1!=null  && !v1.equals(""))

         {

          sum=sum+new Double(v1).doubleValue();

    

         }

         if(v2!=null  && !v2.equals(""))

         {

          sum=sum+new Double(v2).doubleValue();

         }

         if(v3!=null  && !v3.equals(""))

         {

          sum=sum+new Double(v3).doubleValue();

         }

        

         if(sum-(int)sum<=0.0)

         {

          s=""+(int)sum;

         }

         else

         {

          s=""+sum;

         }

         return s;

}

Another problem is that UDF will not get called, if fields are not present in source XML.Thus I would further suggest to use the UDF like this

f1----->mapwithdefault(0)------\

f2----->mapwithdefault(0)----------UDF--------->target

f3----->mapwithdefault(0)------/

you mentioned you cannot use mapwithdefault or add , could you please explain any reasons (as why you cannot use  standard functions).

If you use standard functions no UDF is required.

Regards

Anupam

former_member196519
Participant
0 Kudos

Thanks Anupam for sharing UDF!

It works correctly for my requirement.

Thanks again!

Kumar

Answers (5)

Answers (5)

0 Kudos

Duplicate the Idoc structure at receiver side and implement different add functions for the Target fields in IDocs.

Thanks,

former_member191435
Contributor
0 Kudos

Hi,

you can directly concat the 3 fields and assign to third field...

Cheers,

Cnu

former_member184789
Active Contributor
0 Kudos

hi,

there is no need of a udf for this. you cud use the standard concat function(under text functions) if u simply want to concat fields.

former_member196519
Participant
0 Kudos

Hi Adarsh,

Actually I have 41 fields under Source node. I was taking example of 4 fields.

Thanks,

Kumar

former_member201264
Active Contributor
0 Kudos

Hi Kumar,

write a UDF with var1 var2 and var3 as inputs.

use code as belwo simple

return var1+var2+var3 ---->if you want concat them

return var1+'  '+var2+'  '+var3 --> if you want concat them with space delimitor

Regards,

Sreeni.

Former Member
0 Kudos

Hi Kumar,

You can write UDF as follows as you are not able to use standard functions:

Pass 3 fields as parameter to UDF and create UDF of type value.

You may check for empty field in UDF as field != "".

Regards,

Beena