cancel
Showing results for 
Search instead for 
Did you mean: 

SAP PI UDF to compare values in context if equal or different

former_member434498
Participant
0 Kudos

Hi Team,

Good day!

I need to create a mapping to compare values in a context if they are the same or different.

If the values in a particular context are the same, the output should be true.

Otherwise, if the values in a particular context are different, it should result as false.

e.g.

Input are

-------------------------------

1300

1300

1300

---- context change ---
1322

1322

1300

---- context change ---

1311

1311

1300

--------------------------------

the output should be

-------------------------------

true

---- context change ---
false

---- context change ---

false

--------------------------------

Is there a way to implement this logic using SAP PI standard functions in message mapping?

or do I need to create a user defined function for such?

If user defined function is required, could help me with the java code (just the start of the code)?

Thanks,

Carlo

Accepted Solutions (1)

Accepted Solutions (1)

former_member191435
Contributor
0 Kudos

Hi Carlo,

You can achieve this using Node Functions of Message Mapping. Please find below.

Explanation:

Filed1 --------> Sort ( Ascending)

                                                                  equalsS(Text)      -----------> Target.

Field1 ---------> Sort ( Descending)

Use FIeld1 in Both .......

make Context adjustment for field1.... I have maintain record level..

Thanks,

Sreenivas

former_member184681
Active Contributor
0 Kudos

Hi Sreenivas,

That's a tricky one! However, in my opinion it also requires a slight improvement to work as expected. In his example, Carlo mentioned that he only needs one value for each context. So you would have to add CollapseContext function before, or after equalsS, to achieve the expected result.

Regards,

Greg

former_member191435
Contributor
0 Kudos

Yes...Greg...

That is the reason at last I mention make context adjustment.... for his requirement...

Thanks,

former_member434498
Participant
0 Kudos

Hi All,

Thanks for providing assistance.

Both the Java and standard function actually worked.

But as advised by Sreenivas, I used the most simple way by using the PI standard function "sort" to implement the logic.

Thanks everyone

Carlo

Answers (1)

Answers (1)

Former Member
0 Kudos

Hello,

Check this UDF:

Execution type: all  values of a context

Input: var1


boolean flag = true;

String value=var1[0];

for(int i = 1; i < var1.length; i++)

{

if (var1[i]!= value)

{

flag = false;

break;

}

}

if (flag)

{

result.addValue("true");

}

else

result.addValue("false");

Thanks

Amit Srivastava

former_member184681
Active Contributor
0 Kudos

Hi Carlo, Amit,

Personally I would make a small improvement to the code provided by Amit. I would replace line 5 with the following code:

if (! var1[i].equals( value ))

The comparison using "=", "!=" etc. doesn't always work as expected with string variables in Java.

Regards,

Greg

Former Member
0 Kudos

Hello Greg,

Thanks for bringing that into notice.

But i would still persist with my code by using var1 as type int

int value=var1[0];

Thanks

Amit Srivastava

former_member184681
Active Contributor
0 Kudos

Sure, this can also work well. It all depends on the expected type of input values.