Skip to Content
0

Can we Pass root node in custom Function for validation in Graphical Mapping

Mar 30, 2017 at 06:52 AM

80

avatar image
Former Member

I want to validate the incoming data through XML sluper in Mapping. If i pass root note of xsd it will work or not?

10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

3 Answers

Best Answer
Sriprasad Shivaram Bhat Mar 31, 2017 at 07:37 PM
0

Hello Aditya,

Its possible to use XMLSlurper inside custom function also.Below code will do the needful I guess :)

Message Mapping:

Script:

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

//Add MappingContext as an additional argument to read or set Headers and properties.

def String customFunc(String property_name,MappingContext context){
    
    //Read property Value and store it in String
    String propVal = context.getProperty(property_name);
    propVal_Str = propVal.toString();
    
    def list = new XmlSlurper().parseText(propVal_Str)
    def str=""
    
    //Parse the data coming from Property.In my case I am just performing concatenation.
    list.Record.each
    {
      str=str+it.Field1.text()
    }

//Set the target field value.
  return str;
    
}

Property Data:

<Root>
    <Record>
        <Field1>Rec1_F1</Field1>
        <Field2>Rec1_F2</Field2>
    </Record>
    <Record>
        <Field1>Rec2_F11</Field1>
        <Field2>Rec2_F22</Field2>
    </Record>
</Root>

Actual Data:

<EmployeeData>
    <Record>
        <PersonID>P11</PersonID>
        <UserID>U31</UserID>
        <EmployementID>E221</EmployementID>
    </Record>
    <Record>
        <PersonID>P12</PersonID>
        <UserID>U32</UserID>
        <EmployementID>E222</EmployementID>
    </Record>
    <Record>
        <PersonID>P13</PersonID>
        <UserID>U33</UserID>
        <EmployementID>E223</EmployementID>
    </Record>
</EmployeeData>

Output Data:

<?xml version="1.0" encoding="UTF-8"?>
<EmployeeData>
    <Record>
        <PersonID>Rec1_F1Rec2_F11</PersonID>
        <UserID>U31</UserID>
        <EmployementID>E221</EmployementID>
    </Record>
    <Record>
        <PersonID>Rec1_F1Rec2_F11</PersonID>
        <UserID>U32</UserID>
        <EmployementID>E222</EmployementID>
    </Record>
    <Record>
        <PersonID>Rec1_F1Rec2_F11</PersonID>
        <UserID>U33</UserID>
        <EmployementID>E223</EmployementID>
    </Record>
</EmployeeData>

Regards,

Sriprasad Shivaram Bhat


Share
10 |10000 characters needed characters left characters exceeded
Sriprasad Shivaram Bhat Mar 30, 2017 at 07:24 AM
0

Hello Aditya,

It would be great if you can explain your requirement clearly.

I have not tried below firstoption and second option will work for sure.

1) You can map with root node and select return as xml,then try to pass it target field.

2) Set incoming XML to property and call it inside the Custom Function.

Regards,

Sriprasad Shivaram Bhat

Show 2 Share
10 |10000 characters needed characters left characters exceeded
Former Member

Hi Sriprasad.

Thanks for reply..

How I can use XML data in custom function if I will store input XML to property .

Regards,

Aditya

0

Hello Aditya,

Below code will help you.Have a constant and send the Property name to custom function.

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

def String getProperty(String property_name,MappingContext context)
 {
     String propVal = context.getProperty(property_name);
      propVal = propVal.toString();
      return propVal;
 }

Regards,

Sriprasad Shivaram Bhat

0
avatar image
Former Member Apr 03, 2017 at 06:13 PM
0

Thanks Sriprasad :)

I am also using same concept.

Thanks so much for your time and help, much appreciated!!!

Regards,

Aditya

Share
10 |10000 characters needed characters left characters exceeded