Here I am just explaining the Importance of Groovy script in the Real time scenarios when we are dealing with Cloud Integration.

Groovy scripting is an integral and important feature of SAP Cloud Platform Integration (CPI). The goals of this repository are: Providing templates when you are implementing a new script. Easily finding Groovy functions related to the CPI topic at hand. Minimizing search engine time for common tasks.

Business Requirement:

The Target system is expecting Contact and Detail Records in to one Single Record for their Internal data validations. Hence, we are using Groovy script for Merging two Records into a single Record.

This requirement is even we can achieve using standard options Join & Gather but to create an Interest in beginners I have chosen this option to understand few basic concepts of Groovy.

 

Inputs:

Record 1: 

<Record>

    <Detail>
        <Key>1</Key>
        <Place>Ocean</Place>
        <City>Urban</City>
    </Detail>
    <Detail>
        <Key>2</Key>
        <Place>Road</Place>
        <City>Rural</City>
    </Detail>
    <Detail>
        <Key>3</Key>
        <Place>Plane</Place>
        <City>Semiurban</City>
    </Detail>
</Record>

 

Record 2: 

<Record>
    <Contact>
        <Key>1</Key>
        <Name>Jack</Name>
    </Contact>
    <Contact>
        <Key>2</Key>
        <Name>Ethan</Name>
    </Contact>
    <Contact>
        <Key>3</Key>
        <Name>Ron</Name>
    </Contact>
</Record>
Groovy Script:
import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
import org.xml.sax.InputSource;
def Message processData(Message message)
{
    def body = message.getBody(java.io.Reader)
    def mainDoc = new XmlParser().parse(body)
    def map = message.getProperties();
    String value = map.get(“Message_2”);
    def externalXmlDoc = new XmlParser().parse(new InputSource(new StringReader(value)))
    // Create “Extension1” element for clarity
    def nodeBuilder = new NodeBuilder();
    def node1 = nodeBuilder.Extension1{}
    // Merge
    node1.append(externalXmlDoc)
    mainDoc.append(node1)
    // Write document to body
    def sw = new StringWriter()
    def xmlNodePrinter = new XmlNodePrinter(new PrintWriter(sw))
    xmlNodePrinter.with {
        preserveWhitespace = true
    }
    xmlNodePrinter.print(mainDoc)
    String result = sw.toString()
    message.setBody(result)
    return message;
}
Test Execution:
a) Give your 1st Record as an Input
b) Add second Record as an Exchange Property
c) Execute the Code
Output:
<Record>
  <Detail>
    <Key>1</Key>
    <Place>Ocean</Place>
    <City>Urban</City>
  </Detail>
  <Detail>
    <Key>2</Key>
    <Place>Road</Place>
    <City>Rural</City>
  </Detail>
  <Detail>
    <Key>3</Key>
    <Place>Plane</Place>
    <City>Semiurban</City>
  </Detail>
  <Extension1>
    <Record>
      <Contact>
        <Key>1</Key>
        <Name>Jack</Name>
      </Contact>
      <Contact>
        <Key>2</Key>
        <Name>Ethan</Name>
      </Contact>
      <Contact>
        <Key>3</Key>
        <Name>Ron</Name>
      </Contact>
    </Record>
  </Extension1>
</Record>

Conclusion –

When you’re Implementing in IFLOW, you have to capture the payload of second record as an Exchange Property. Hope this document will help to beginners to understand CPI concept of Groovy Script.

Happy Learning 🙂
Sara Sampaio

Sara Sampaio

Author Since: March 10, 2022

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x