Skip to Content

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

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

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

3 Answers

  • Best Answer
    Mar 31, 2017 at 07:37 PM

    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

    Add comment
    10|10000 characters needed characters exceeded

  • Mar 30, 2017 at 07:24 AM

    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

    Add comment
    10|10000 characters needed characters exceeded

    • 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

  • Apr 03, 2017 at 06:13 PM

    Thanks Sriprasad :)

    I am also using same concept.

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

    Regards,

    Aditya

    Add comment
    10|10000 characters needed characters exceeded