Using Scripting

Objective

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 use 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 use the inLine editor directly in the following manner.

Procedure

  • Position a Groovy Script component on the expiration path.
  • Choose 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.

Place a Groovy Script component on the expiration path.

Choose Create.
Screenshot of the Scripting Editor.

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.

Screenshot of Groovy Script in Groovy IDE.

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

A Groovy Script is required for error handling to work and take effect. It is used to record error information in the event of an error. To do this, the script must be stored within 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.

Screenshot of the Script Exception Subprocess in the integration flow process.

What do you Learn Within This Exercise

Learn the process of creating and using a Groovy Script.

Task 1: Log on to 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_CLD900_Date_NumberDelayedDelivery_Process_Number.

    2. Following the status after the last exercise step.

      Starting the exercise.
    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 TransformationScriptGroovy Script component within the Exception Subprocess.

    2. Or you can use the context menu by choosing the line between the Error Start and End event. Then, to add a flow step, choose the plus symbol.

      To add a flow step, choose the line between the Error Start and End event and then, choose the plus symbol.
    3. Rename it to Script_readExceptionMessagesToPayload.

      Rename the script.
  2. Write a Groovy Script.

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

      To write, add a Groovy Script.
    2. Enter the following data:

      Code Snippet
      1234567891011121314151617181920212223242526
      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; }
      Screenshot of the Groovy Script Editor.
    3. Choose the OK button to save your work.

    4. Navigate back to your Integration Process via the 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 the log level to trace.
      5. Deploy again.
      6. Jump again to OverviewManage Integration Content.
    2. Navigate to your integration flow Model in OverviewMonitor Message ProcessingMessage Processing Run.

  2. Deploy it without an Exception and check the Exception Subprocess in the monitoring, which has no messages working with.

    Screenshot of the Exception Subprocess without any messages.
  3. Deploy it with an implemented exception error.

    1. Navigate to the Call_checkProductID OData adapter. At the Connection tab, change the URL so that it no longer works by adding an x.

      Navigate to the Call_checkProductID OData adapter. At the Connection tab, change the URL so that it no longer works by adding an x.
    2. Save and Deploy again, and check your Monitor.

    3. At OverviewMonitor Message Processing.

      Screenshot of Exception Subprocess with messages.
    4. The message status is Completed and an error message has been created. Check the messages and the attachments under Monitor Message Processing.

      Check the message and attachments (highlighted in red) under Monitor Message Processing.
    5. An Exception occurs and the Exception Subprocess is called.

    6. Everything works as expected.

    7. 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 mark symbol.

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. Also, we 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 task:

  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.

The graphic shows the integration flow process with the Event Start Message element highlighted in red.

What Do You Learn Within This Exercise?

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

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

Steps

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

    1. Navigate within the SAP Integration Suite Welcome page toDesignIntegrationsDelayedDelivery_Package_CLD900_Date_NumberDelayedDelivery_Process_Number .

    2. Following the status after the last exercise step.

      Screenshot of the exercise.
    3. Imagine that the integration flow has been further modified.

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

    1. Choose EventStart Message or use the context menu as previously mentioned in the preceding exercises.

    2. Delete the Time Event.

      To add a start message event, choose Start Message.
      To add a flow step, choose Start 2.
    3. Rename the start event to Event_startMessage.

      Rename the start event.
  3. Delete the Modify_setPayload component.

    Delete the Modify_setPayload component.
    1. This segment of your integration flow looks like this now.

      Screenshot of the outcome.
    2. Choose the Save button.

Log in to track your progress & complete quizzes