Blog Post

Examples of Groovy Script in SAP Cloud Integration

September 4, 2023

Before we dive into practical examples, let's grasp the fundamentals of Groovy scripting. Groovy is a versatile, dynamically-typed language that's fully compatible with Java. It offers a simplified syntax and is particularly well-suited for scripting tasks in SAP CPI.

Here are the basic examples of groovy script used in in the integration flows:

  1. Access Header/Property in Message Mapping -

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

import com.sap.it.api.mapping.MappingContext

//Access Property Value

def String getProperty(String propertyName, MappingContext context) {

   def propertyValue = context.getProperty(propertyName);

   return propertyValue;

}

//Access Header Value

def String getheader(String header_name, MappingContext context) {

   def headervalue= context.getHeader(header_name);

   return headervalue;

}

  1. Fetch User Credentials -

import com.sap.it.api.ITApi

import com.sap.it.api.ITApiFactory

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

import com.sap.gateway.ip.core.customdev.util.Message;

import java.util.HashMap;

def Message processData(Message message) {

   //Body

      def body = message.getBody();

      String password;

       String _output="";

      def service = ITApiFactory.getApi(SecureStoreService.class, null);

       def credential = service.getUserCredential("Pega_SessionTicket");

       if (credential == null)

       { throw new IllegalStateException("No credential found for alias 'CredAlias'");

           

       }

       else{

           password= new String(credential.getPassword());

           username= new String(credential.getUsername());

           }

       message.setProperty("PASSWORD", password);

       message.setProperty("USERNAME",username);

      return message;

}

  1. Write log to capture all payload -

import com.sap.gateway.ip.core.customdev.util.Message;

import java.util.HashMap;

def Message processData(Message message)

{  

   def body = message.getBody(java.lang.String) as String;

   def messageLog = messageLogFactory.getMessageLog(message);

   if(messageLog != null)

   {

       messageLog.setStringProperty("log1","Printing Payload As Attachment")

       messageLog.addAttachmentAsString("log1",body,"text/plain");

   }

   return message;

}

  1. Make integration flow stop/pause or sleep -

import com.sap.gateway.ip.core.customdev.util.Message;

import java.util.HashMap;

def Message processData(Message message)

{

      def body = message.getBody();

      sleep(40000);

      message.setBody(body);

      return message;

}

  1. Remove something from incoming payload -

import com.sap.gateway.ip.core.customdev.util.Message;

import java.util.HashMap;

def Message processData(Message message) {

   def xmlPayload = message.getBody(String)

 

   def modifiedPayload = xmlPayload.replaceAll(/<asx:values>|<asx:abap version="1.0"|<\/asx:abap>|<\/asx:values>|xmlns:asx="http:\/\/www.sap.com\/abapxml">/,'')

   message.setBody(modifiedPayload)

   return message

}

  1. Access all Headers/Properties Values -

import com.sap.gateway.ip.core.customdev.util.Message;

import java.util.HashMap;

import java.lang.*;

def Message processData(Message message)

{

   map = message.getProperties();

   return message;

}

  1. Concatenate all values -

import com.sap.gateway.ip.core.customdev.util.Message;

import java.util.HashMap;

import java.lang.*;

def Message processData(Message message)

{

   map = message.getProperties();

   def ZF1    = map.get("F1");

   def ZF2    = map.get("F2");

   def ZF3    = map.get("F3");

   String ZConcat = ZF1.concat(ZF2).concat(ZF3);

   message.setProperty("ConcatenatedResult",ZConcat);

   return message;

}

Conclusion

n this article, we've scratched the surface of Groovy scripting's capabilities within SAP CPI. From data transformation to error handling and dynamic routing, Groovy scripts empower you to enhance your integration flows and solve complex integration challenges.

As you embark on your journey with SAP CPI, remember that Groovy scripting is a powerful tool in your toolkit. It allows you to craft custom solutions, streamline processes, and ensure the seamless flow of data between your applications.

So, the next time you're faced with a tricky integration task, don't forget to consider Groovy scripting as your secret weapon. With practice and creativity, you can unlock its full potential and elevate your SAP CPI integration projects to new heights. Happy scripting!

The basic Groovy script listed above is helpful for SAP CPI. In some situations, you can visit the link https://groovyide.com/cpi to view the results after running your groovy script if you want to get accurate results.

LinkedIn 
Forbes Technology Council, Official Member (2022)
LinkedIn
Forbes Technology Council, Official Member (2022)

About the Author

Jaspreet is an Executive Consultant with expertise in SAP, SaaS/Cloud Integrations, Cyber Security and Data Science. Jaspreet is hands-On Architect who does Pre-Sales, Solution Architecture, Development, Lead Delivery of Complex Integration programs, Manage disperse teams and Ensure successful Project Go-Live/Goals. He has made a lasting impact on global businesses IT projects including Aflac, Advanced Energy, Donnelley Financial Solutions(DFIN), Dell EMC and many more.

Do you want to
learn more about integration?

We are dedicated to make our knowledge accessible. You can either figure it out by yourself or you can let us give you a hand.

Let us take care of your integration.

We are SAP Certified and we can make your project happen. Explore our services and contact us. We will be happy to take on your project.

View Our Services