cancel
Showing results for 
Search instead for 
Did you mean: 

UDF in HCI

markbernabe
Active Participant
0 Kudos

Dear Experts,

What is the counterpart of 'result.addValue(value)' of PI UDF in HCI UDF?

i.e. There is a UDF in PI that dynamically maps the values of Source field var1 to a 0..unbounded target field. The same UDF needs to be converted into custom function in HCI.

i.e. var1 = "Apple,Mango,Orange"

String[] list = var1[0].split(",");

if (list.length > 0){
for (int j = 0; j < list.length; j++){
result.addValue(list[j]);
}
}

Appreciate your inputs. Thank you.

Accepted Solutions (1)

Accepted Solutions (1)

Sriprasadsbhat
Active Contributor

Hello Mark,

I am not sure in which step you are facing issues.I have tried its working fine without any issues .Refer the below.

Integration Flow:

Input Message:

<Root>    
    <Data>Apple,Mango,Orange</Data>
</Root>

Custom Function:

import com.sap.it.api.mapping.*
def void extParam(String[] P1, Output output, MappingContext context) 
{
    String[] list = P1[0].split(",");
    if (list.length > 0)
    {
        for (int j = 0; j < list.length; j++)
        {
         output.addValue(list[j]);
        }
    }
}

Output Message:

<?xml version="1.0" encoding="UTF-8"?>
<Items>
    <Fruit>Apple</Fruit>
    <Fruit>Mango</Fruit>
    <Fruit>Orange</Fruit>
</Items>

Regards,

Sriprasad Shivaram Bhat

markbernabe
Active Participant
0 Kudos

Thanks Sriprasad!

Answers (2)

Answers (2)

Sriprasadsbhat
Active Contributor

Hello Mark ,

Create a Custom Function [ Ex: Custom_Function.gsh ] with below code and use it in your mapping it will work.

import com.sap.it.api.mapping.*

def void extParam(String[] P1, Output output, MappingContext context) {

    String[] list = P1[0].split(",");

    if (list.length > 0)
    {
        for (int j = 0; j < list.length; j++)
        {
         output.addValue(list[j]);
        }
    }
}

Hope this time I have interpreted your query correctly :).

Regards,

Sriprasad Shivaram Bhat

markbernabe
Active Participant
0 Kudos

Thanks Sriprasad. I tried but it didn't work 😞

Sriprasadsbhat
Active Contributor
0 Kudos

Hello Mark,

If I have understood your requirement correctly counterpart is same[ addValue() works in SAP HCI too ).Below might help you with example of getting List of values and storing them in result list.

import com.sap.it.api.mapping.*
def void extParam(String[] P1, Output output, MappingContext context) {
    String op_str="";
    int p1Len=P1.length;
    for( int i=0;i<p1Len;i++)
    {
      op_str=op_str+P1[i];
      if (i != (p1Len-1))
        op_str=op_str+",";
    } 
    output.addValue(op_str);
}

Here is the link to Java Doc for Mapping step which might help you to understand the functionality better.

JAVA DOC SAP HCI

Regards,

Sriprasad Shivaram Bhat

markbernabe
Active Participant
0 Kudos

Thanks Sriprasad! So it's possible to use .java for custom functions in message mapping? I thought it's only limited to groovy

Sriprasadsbhat
Active Contributor
0 Kudos

Yes Mark its possible.

Regards,

Sriprasad Shivaram Bhat

markbernabe
Active Participant
0 Kudos

Hi Sriprasad,

I'm getting invalid file extension when trying to create a .java Custom Function. How do you create such script?

script.

Sriprasadsbhat
Active Contributor
0 Kudos

Hello Mark,

In your earlier question I missed .(mis interpret with whether we can use Java functions or not).

Very sorry for that,you can't use .Java extension to custom functions it has to be . Groovy.

Regards,

Sriprasad Shivaram Bhat

markbernabe
Active Participant
0 Kudos

Hi Sriprasad,

No worries. Are you aware of any similar function in Groovy?

So the sample scenario is, a source has a field that contains CSV

<Root>
	<Item>Apple,Mango,Orange</Item>
</Root>

And target has a field that depends on the cardinality of the source field value.

<Root>
	<Items>
		<Fruit>Apple</Fruit>
		<Fruit>Mango</Fruit>
		<Fruit>Orange</Fruit>
	</Items>
</Root>

This can be easily achieved by using UDF in PI. But we need to do the same in HCI and we're not sure how to achieve a similar UDF in HCI. I know it's possible to import a message mapping from ESR in PI and it comes together with the UDF. The UDF also works during runtime. However, there's no way to view or edit the UDF in HCI directly. It can be a workaround but it's not sustainable since you always need PI to maintain it.

Any ideas?