Technology Blog Posts by Members
cancel
Showing results for 
Search instead for 
Did you mean: 
Arpit_Verma1
Explorer
1,060

 

In Application-to-Application (A2A) integrations, it’s common to encounter scenarios where the sender system is non-SAP (like Coupa, Salesforce, etc.) and the receiver is an SAP ECC or S/4HANA system. One specific challenge that often arises is handling free-form text.

  • Non-SAP platforms may allow free-form text of unlimited length.
  • SAP systems, however, have strict character limits—for example, 132 characters per segment in IDocs or BAPI text fields.

This blog explains how to dynamically split long text strings into SAP-compatible segments using a Groovy UDF (User Defined Function) in SAP Cloud Integration (CPI).

Scenario Overview

Component

Description

Sender

Non-SAP system (e.g., Coupa, STIBO)

Receiver

SAP ECC or S/4HANA

Problem

Free-form text exceeds SAP's 132-character limit

Solution

Use a Groovy script in message mapping to split long text into 132-character segments

Why 132 Characters?

In SAP systems, particularly in IDocs like ORDERS05, fields like E1EDKT1/E1EDKT2-TDLINE are limited to 132 characters per line. Any text longer than that must be broken into multiple lines—each forming a new segment.

Solution:
Groovy UDF in Message Mapping

We created a Groovy script that:

  1. Accepts any length of input text

  2. Dynamically breaks it into lines no longer than 132 characters

  3. Adds each line as a new segment in the target SAP structure

This approach avoids hard-coding or static loops and adapts to any input length

import com.sap.it.api.mapping.*;
import com.sap.gateway.ip.core.customdev.util.Message
import java.util.HashMap

def void createItems(String[] nonsaptext, String[] fieldlength, Output output, MappingContext context) {

// Iterate over each text block
int lineLength= Integer.parseInt(fieldlength[0])
// int lineLength=textlength
// def valu1int=Integer.parseInt(linelength[0])
for (String text : nonsaptext) {

// Calculate the number of lines needed for the current text block

int length = text.length();

//int lineLength = 132;

// Split the text into lines of up to 132 characters

for (int i = 0; i < length; i += lineLength) {

// Ensure we don't go out of bounds

int end = Math.min(length, i + lineLength);

// Extract the substring for the current line
String line = text.substring(i, end);

output.addValue(line);

}

}

}

 Step-by-Step

1. Receives Two Inputs:

  • nonsaptext: A long text string (from the sender system).

  • fieldlength: The maximum allowed characters per segment (usually "132").


2. Parses the Max Length

  • Converts the fieldlength string to an integer using Integer.parseInt.


3. Iterates Over the Input Text

  • It loops through each character of the input text in increments of lineLength (e.g., 132 characters at a time).


4. Splits the Text

  • In each loop:

    • It extracts a substring from the current position to the next lineLength.

    • This ensures no segment is longer than allowed.


5. Adds Segments to Output

  • Each substring is added to the output object as a new value.

  • In CPI mapping, this translates into multiple entries for the target repeating structure (e.g., TDLINE in an IDoc).


Source Payload:

arpit123_0-1747559896978.png

Message Mapping

arpit123_1-1747559928920.png

arpit123_2-1747559937576.png

Message Mapping Result:

arpit123_3-1747559961267.png

Results

This approach ensures:

  • Data Integrity: No truncation or loss of message content.

  • SAP Compliance: Respects SAP's 132-character-per-line constraint.

  • Flexibility: Can process any length of input text without needing hard-coded splits.

Thanks
Arpit

2 Comments