Modifier with Realtime examples
Modifier with Realtime examples
The Content Modifier in SAP CPI is a processing step used to modify the message content,
headers, and properties within an integration flow. It allows you to:
Scenario:
A third-party system requires an API call with a specific header (API-Key).
Integration Steps
Headers Tab
o Name: API-Key
o Type: Constant
o Value: 123456789-ABCDE
This adds the API-Key header to the message before sending it.
Scenario:
You need to store the order ID from an incoming XML payload and use it later in the flow.
Properties Tab
o Name: OrderID
o Type: XPath
o Value: /Order/ID
This extracts 56789 and stores it in the OrderID property. You can use this in later steps (e.g.,
logging or mapping).
Scenario:
A source system sends an XML, but the target system expects JSON.
json
CopyEdit
{
"OrderID": "${property.OrderID}",
"Status": "Processing"
}
This replaces the existing body with JSON, dynamically using the OrderID property.
Scenario:
1. A source system sends an XML payload with order details.
2. You extract OrderID using a Content Modifier (Property).
3. You set a custom header (Transaction-ID).
4. You modify the message body to match the API request format.
5. You send the request to an external REST API.
Conclusion
Content Modifier is essential in SAP CPI for message transformation and metadata handling.
Whether modifying headers for authentication, extracting properties for routing, or transforming
payloads, it plays a crucial role in real-world integration scenarios.
You said:
yes
ChatGPT said:
This exercise will guide you through modifying message headers, properties, and body using
Content Modifier in SAP CPI.
💡 Scenario: Order Processing API Integration
You need to process incoming order data, extract the Order ID, set a custom Transaction ID
header, and format the message to JSON before sending it to a REST API.
🛠️Step-by-Step Implementation
🔹 Step 1: Create an Integration Flow
Go to "Properties" tab
Click Add Property
o Name: OrderID
o Type: XPath
o Value: /Order/ID
Go to "Headers" tab
Click Add Header
o Name: Transaction-ID
o Type: Expression
o Value: ${date:now:yyyyMMddHHmmss}-${property.OrderID}
📝 This generates a unique transaction ID based on the timestamp and extracted Order ID.
(C) Modify the Message Body to JSON Format
Go to "Body" tab
Select Expression
Enter the following JSON structure:
json
CopyEdit
{
"OrderID": "${property.OrderID}",
"Status": "Processing"
}
Use Postman or any API testing tool to send a request to your CPI IFlow endpoint:
xml
CopyEdit
<Order>
<ID>12345</ID>
<Amount>200.00</Amount>
</Order>
pgsql
CopyEdit
Transaction-ID: 20250205123045-12345
Content-Type: application/json
🎯 Key Learnings
✅ Extracting values using XPath in Message Properties
✅ Creating custom headers using Expressions
✅ Transforming XML to JSON using Content Modifier
🚀 Bonus Task
Modify the integration to include Amount in the JSON body dynamically. Try using ${in.body}
or another property!
You said:
yes
ChatGPT said:
Instead of using a Content Modifier, we can achieve the same functionality using a Groovy
script in SAP CPI.
💡 Scenario Recap
1. Extract Order ID from the XML payload.
2. Generate a Transaction ID dynamically.
3. Convert the XML payload to JSON format.
4. Set the required headers before sending to the receiver.
groovy
CopyEdit
import com.sap.gateway.ip.core.customdev.util.Message
import java.text.SimpleDateFormat
// Set headers
def headers = message.getHeaders()
headers.put("Transaction-ID", transactionId)
headers.put("Content-Type", "application/json")
return message
}
🔹 How This Works
✔ Extracts Order ID and Amount from XML using regex
✔ Generates a Transaction-ID using a timestamp and Order ID
✔ Converts XML to JSON format
✔ Sets custom headers before sending the message
🔹 Expected Output
✅ JSON Message Sent to Receiver
json
CopyEdit
{
"OrderID": "12345",
"Amount": "200.00",
"Status": "Processing"
}
✅ Headers Sent
pgsql
CopyEdit
Transaction-ID: 20250205123045-12345
Content-Type: application/json
🚀 Next Steps
Try modifying the script to:
✅ Include an additional field like OrderDate
✅ Use XPath instead of regex for XML extraction
✅ Handle missing values gracefully
You said:
yes
ChatGPT said:
Groovy Script Alternative using XPath for XML Extraction 🚀
This version of the Groovy script extracts values using XPath, which is more reliable than regex
for structured XML data.
// Parse XML
def factory = DocumentBuilderFactory.newInstance()
factory.setNamespaceAware(true)
def builder = factory.newDocumentBuilder()
def inputStream = new ByteArrayInputStream(xmlBody.getBytes("UTF-8"))
def document = builder.parse(inputStream)
return message
}
🔹 Expected Output
✅ JSON Message Sent to Receiver
json
CopyEdit
{
"OrderID": "12345",
"Amount": "200.00",
"Status": "Processing"
}
✅ Headers Sent
pgsql
CopyEdit
Transaction-ID: 20250205123045-12345
Content-Type: application/json
🚀 Next Steps
✅ Modify XPath to extract more fields like CustomerName
✅ Handle missing XML elements gracefully
✅ Log extracted values for debugging
Would you like me to extend this with error handling and logging? 😊
You said:
1
ChatGPT said:
Could you please clarify what you mean by "1"? Are you asking for:
You said:
2
ChatGPT said:
The Content Modifier in SAP CPI is widely used for handling headers, properties, and body
transformations. Below are three real-time scenarios demonstrating its practical applications.
Steps in CPI:
Properties Tab
o Name: AuthToken
o Type: Expression
o Value: ${property.OAuthResponse.token}
Headers Tab
o Name: Authorization
o Type: Expression
o Value: Bearer ${property.AuthToken}
An order system sends raw XML without tracking information. The target system requires a
Tracking-ID.
Steps in CPI:
json
CopyEdit
{
"OrderID": "${property.OrderID}",
"Amount": "${property.Amount}",
"TrackingID": "${property.TrackingID}",
"Status": "Pending"
}
Expected Output
json
CopyEdit
{
"OrderID": "56789",
"Amount": "150.00",
"TrackingID": "20250205143030-56789",
"Status": "Pending"
}
A company routes invoices to different ERP systems based on region (US, EU, Asia).
Steps in CPI:
Properties Tab
o Name: Region
o Type: XPath
o Value: /Invoice/Country
Router Configuration:
📌 Why? This ensures invoices are sent to the correct backend ERP.
🚀 Summary of Learnings
Would you like a Groovy script alternative for any of these scenarios? 😊
You said:
🚀 Deploying and Testing an SAP CPI Integration Flow with Content Modifier
This guide will help you deploy and test an SAP CPI integration flow that uses the Content
Modifier to modify headers, properties, and body.
1. Click Deploy.
2. Wait for the Status to change to Started.
3. Go to Monitor → Manage Integration Content and confirm that the IFlow is running.
1. Open Postman.
2. Set the method to POST.
3. Paste the Endpoint URL.
4. Go to Headers → Add:
pgsql
CopyEdit
Content-Type: application/xml
xml
CopyEdit
<Order>
<ID>12345</ID>
<Amount>250.00</Amount>
</Order>
6. Click Send.
bash
CopyEdit
curl -X POST "https://<your CPI tenant>/http/orderProcessing" \
-H "Content-Type: application/xml" \
-d '<Order><ID>12345</ID><Amount>250.00</Amount></Order>'
🚀 Next Steps
✅ Deploy another IFlow using different authentication mechanisms (OAuth, Basic Auth)
✅ Implement a Retry Mechanism using Exception Subprocess
✅ Automate testing with Postman collections
Would you like me to guide you through error handling or logging setup? 😊
You said:
yes
ChatGPT said:
Handling errors effectively in SAP CPI is crucial for troubleshooting and ensuring smooth
integrations. Below, we will cover:
You can use a Groovy script to capture error details and log them for debugging.
groovy
CopyEdit
import com.sap.gateway.ip.core.customdev.util.Message
import java.util.HashMap
If a message fails due to a temporary issue (e.g., API downtime), you can automatically retry
before failing completely.
📌 Tip: You can also store the error message in a Data Store and manually reprocess it later.
Error Details
Payload Before Failure
Exception Stack Trace
4️⃣ Use Resend Feature to manually retry a failed message.
🚀 Next Steps
✅ Enhance Logging → Store logs in an external system (e.g., SAP BTP, Cloud Storage).
✅ Alert Mechanisms → Send email alerts when critical errors occur.
✅ Retry Logic → Implement an Exponential Backoff retry strategy.
Would you like help with email notifications for error alerts? 😊
You said:
Would you like a Groovy script alternative for any of these scenarios?
ChatGPT said:
Below are Groovy script alternatives for common Content Modifier use cases in SAP CPI.
return message
}
return message
}
return message
}
🚀 Next Steps
You said:
📌 Steps to Implement:
1️⃣ Add an Exception Subprocess inside your Integration Flow (IFlow).
2️⃣ Inside the Exception Subprocess, add:
responseCode = connection.getResponseCode()
📌 When to Use?
✅ If you need dynamic retry attempts.
✅ If your retry logic depends on API response codes.
📌 Steps to Implement:
📌 When to Use?
✅ Best for high-volume messages that need guaranteed delivery.
✅ Ideal for longer retry intervals (e.g., retry after 30 minutes).