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.
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:
Accepts any length of input text
Dynamically breaks it into lines no longer than 132 characters
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);
}
}
}
nonsaptext: A long text string (from the sender system).
fieldlength: The maximum allowed characters per segment (usually "132").
Converts the fieldlength string to an integer using Integer.parseInt.
It loops through each character of the input text in increments of lineLength (e.g., 132 characters at a time).
In each loop:
It extracts a substring from the current position to the next lineLength.
This ensures no segment is longer than allowed.
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:
Message Mapping
Message Mapping Result:
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
9 | |
7 | |
7 | |
7 | |
5 | |
4 | |
4 | |
3 | |
3 | |
3 |