Handling Free-Form Text in SAP CPI: Segmenting Long Text into 132-Character Chunks with Groovy

Estimated read time 5 min read

 

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:

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);

}

}

}

 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:

Message Mapping

Message Mapping Result:

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

 

​  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 OverviewComponentDescriptionSenderNon-SAP system (e.g., Coupa, STIBO)ReceiverSAP ECC or S/4HANAProblemFree-form text exceeds SAP’s 132-character limitSolutionUse a Groovy script in message mapping to split long text into 132-character segmentsWhy 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 MappingWe created a Groovy script that:Accepts any length of input textDynamically breaks it into lines no longer than 132 charactersAdds each line as a new segment in the target SAP structureThis approach avoids hard-coding or static loops and adapts to any input lengthimport 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-Step1. 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 LengthConverts the fieldlength string to an integer using Integer.parseInt.3. Iterates Over the Input TextIt loops through each character of the input text in increments of lineLength (e.g., 132 characters at a time).4. Splits the TextIn 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 OutputEach 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 MappingMessage Mapping Result:ResultsThis 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.ThanksArpit   Read More Technology Blog Posts by Members articles 

#SAP

#SAPTechnologyblog

You May Also Like

More From Author