Using Scripting

Objectives

After completing this lesson, you will be able to:

  • Use scripting

Scripting

In this Lesson, the Following Topics are Discussed:

  • Scripting overview
  • Developing Groovy Scripts with the inline editor.
  • Create Groovy Scripts with an external editor.

Scripting Overview

You can utilize Java or Groovy scripts for message processing, which can be useful in the following scenarios:

Add Information to the Message Processing Log
You can use the Script step to add information to the message processing log (MPL).
Read and Modify Message Header, Message Body, and Exchange Properties
You can use the Script step to address (get, add, modify, or delete) the message header, the message body, and exchange properties, using the interface Message object.
Read and Modify SOAP Headers
You can use the Script step to address SOAP headers.
Read and Modify Partner Directory Content
You can use the Script step to address Partner Directory content.
Handle Exceptions
You can use the Script step to identify exceptions that arise when sending messages using the HTTP or OData V2 receiver adapter.
Read Security-Related Artifacts
You can use the Script step to address security-related artifacts (for example, keystore entries).
Additional Use Cases

Java docs for com.sap.gateway.ip.core.customdev.util packages

The Java doc can be found here: com.sap.it.script.custom-development 2.7.1 API

Developing Groovy Scripts with the inline editor

You can utilize the inLine editor directly in the following manner.

Procedure

  • Position a Groovy Script component on the expiration path.
  • Select the Create button from the context menu.
  • After that, you are in the inline editor of the Groovy script. There is already a basic script created, on which you can build.
  • You will have code compilation and more.

Then proceed with the standard simulation and/or testing process by deploying and tracing the flow.

Create Groovy Scripts with an External Editor

You can build a local development environment with Eclipse, IntelliJ, or Visual Code. You can get the corresponding SDK here: SAP Development Tools at Using Script APIScript API.

There is also an online editor with which you can write and test directly. Use Groovy IDE for easy development and testing of your scripts. All necessary SDKs are already implemented.

After copying the code, paste it into the scripting component and proceed as usual.

Summary

Scripts in Java or Groovy can be created using a Script SDK, which allows for processing messages and their metadata in various ways. The SDK allows for setting and reading exchange parameters, writing logs, intercepting exceptions, and more. These scripts can be created using both the Inline Editor and an online IDE that includes all the necessary SDKs, making it possible to test the script directly.

Create a Groovy Script for Error Handling

Business Scenario

We aim to create a Groovy script that provides additional error information in case of an error occurrence, and then place it in the Exception Subprocess.

Task Flow

In this exercise, you will perform the following tasks:

  1. Log on to the integration flow DelayedDelivery_Process.
  2. Create a Groovy Script.
  3. Save as version, deploy, and debug your integration process.

Prerequisites

The last step of creating an Exception Process has been completed.

Outcome After This Exercise

You will have implemented a Groovy Script within the Exception Subprocess.

What do you Learn Within This Exercise

Learn the process of creating and utilizing a Groovy script.

Exercise Options

To carry out this exercise, you can choose from the following options:

  1. Live Environment: Using the instructions provided below, you can perform the steps in your SAP BTP account.
  2. Platform Simulation: Follow the step-by-step instructions within the simulation.
  3. Side-by-side: Follow the step-by-step instructions within the simulation and perform the steps in your SAP BTP account simultaneously.
Note
We strongly recommend to perform the steps in the live environment.

Task 1: Log on to the the Integration Flow DelayedDelivery_Process

Steps

  1. Log on to the integration flow DelayedDelivery_Process via Integration Suite.

    1. Navigate within the Integration Suite Welcome page to DesignIntegrationsDelayedDelivery_Package_randomNumberDelayedDelivery_Process.

    2. Following the status after the last exercise step.

    3. Let's assume that the integration flow has been further edited.

Task 2: Create a Groovy Script

Steps

  1. Create a Groovy Script.

    1. Set Transformation >Script > Groovy Script component within the Exception Subprocess.

    2. Rename it to Script_readExceptionMessagesToPayload.

  2. Write a Groovy Script.

    1. Use the Create icon from the context menu of Script_readExceptionMessagesToPayload component.

      An editor opens.

    2. Enter the following data:

      Code snippet
      import com.sap.gateway.ip.core.customdev.util.Message;
      import java.util.HashMap;
      import groovy.xml.MarkupBuilder;
      def Message processData(Message message) {
       //Body 
       def body     = message.getBody();
       def props    = message.getProperties(); 
       def exStacktrace = props.get("exStacktrace");
       def exMessage = props.get("exMessage"); 
       def stringWriter = new StringWriter();
       def exceptionBuiler = new MarkupBuilder(stringWriter); 
       exceptionBuiler.exception {
       exceptionMessage(exMessage)
       stacktrace(exStacktrace)
       } 
       message.setHeader("Content-Type", "application/xml")
       message.setHeader("CamelHttpResponseCode", 500) 
       message.setBody(stringWriter.toString());
       
       def messageLog = messageLogFactory.getMessageLog(message);
       if(messageLog != null) {
       messageLog.addAttachmentAsString("Exception Messages", message.getBody(), "text/plain")
       }
       
       return message;
      }
      Expand
    3. Choose the OK button to save your work.

    4. Navigate back to your Integration Process via bread crumb navigation.

Task 3: Save as Version, Deploy, and Debug your Integration Process

Steps

  1. Save as version, deploy, and debug your integration process

    1. Perform the following steps:

      1. Save as version.
      2. Deploy.
      3. Jump to OverviewManage Integration Content.
      4. Set log level to trace.
      5. Deploy again.
      6. Jump again to OverviewManage Integration Content.
    2. Navigate to your integration flow Model in Overview > Monitor Message Processing >Message Processing Run.

  2. Test without an Exception.

    1. We use the same URLs as before, that is, the connections work and we do not go through an error. Everything should work as before.

  3. Test with an exception.

    1. Navigate to the Call_fetchLineItems HTTP connectivity. At the tab Connection, change the URL so that it no longer works.

    2. Save and Deploy again, and check your Monitor.

    3. At Overview > Monitor Message Processing.

    4. The message status is Completed and an error message has been created. Check the messages.

    5. To see the integration flow Model in more detail, navigate to Trace.

    6. An Exception occurs and the Exception Subprocess is called.

    7. Everything works as expected.

    8. Remove the error in the URL again so that everything runs without errors again.

  4. Learn more about the Groovy Scripting operations.

    1. Navigate back to your Integration Process, open the configuration bar of the Script_readExceptionMessagesToPayload component and choose the question marksymbol.

Replace the Timer Event by a Message Start Event

Business Scenario

In this step, our objective is to replace the timer event with a message start event and initiate the process through an incoming SOAP message. Additionally, we will remove the initial content modifier, Modify_setPayload, as it was created for assistance purposes.

Task Flow

In this exercise, you will perform the following steps in one tasks:

  1. Log on to the integration flow DelayedDelivery_Process.
  2. Substitute the timer event with a message start event.
  3. Save as version, deploy, and debug your integration process.

Prerequisites

You have completed the final step of writing the Customer ID.

Outcome After This Exercise

A message start event has been added at the beginning and the Modify_setPayload component has been removed.

What Do You Learn Within This Exercise?

Learn how to replace a timer event with a message start event.

Exercise Options

To carry out this exercise, you can choose from the following options:

  1. Live Environment: Using the instructions provided below, you can perform the steps in your SAP BTP account.
  2. Platform Simulation: Follow the step-by-step instructions within the simulation.
  3. Side-by-side: Follow the step-by-step instructions within the simulation and perform the steps in your SAP BTP account simultaneously.
Note
We strongly recommend to perform the steps in the live environment.

Task 1: Replace the Timer Event by a Message Start Event

Steps

  1. Log on to the integration flow DelayedDelivery_Process via Integration Suite

    1. Navigate within the Integration Suite Welcome page to Design > Integrations > DelayedDelivery_Package_randomNumber > DelayedDelivery_Process.

    2. Following the status after the last exercise step.

    3. Imagine that the integration flow has been further modified.

  2. Substitute the timer event with a message start event.

    1. Move the timer event to the left to create space for the message start event.

    2. Choose EventStart Message.

    3. Place it immediately adjacent to the timer event.

    4. Delete the timer event.

    5. Rename the start event to Event_startMessage.

  3. Delete the Modify_setPayload component.

    1. Delete the Modify_setPayload component.

    2. This segment of your integration flow should look like this now.

    3. Choose the Save button.

Log in to track your progress & complete quizzes