In SAP Cloud Platform Integration (CPI), configuring dynamic file names is not only possible but can be done using a variety of techniques.
In the past, I wrote some articles on defining dynamic parameters such as filename, directory, etc in the receiver file adapters in SAP PI/PO. There are several techniques like ASMA and Variable Substitution. However, SAP Integration Suite CI (CPI) technique is more straightforward.
Common Use Cases of Dynamic File Name and Directory
- Adding a Timestamp to File Names
- Include a custom timestamp in the file name (e.g.,
filename_yyyyMMddHHmmss.xml
).
- Include a custom timestamp in the file name (e.g.,
- Creating Unique File Names with Message IDs
- How to append a unique message ID to avoid file overwriting (e.g.,
filename_<messageId>.xml
).
- How to append a unique message ID to avoid file overwriting (e.g.,
- Adding Custom Parameters (e.g., Sender or Receiver Information)
- Dynamically including sender/receiver names in the file name (e.g.,
file_<senderID>_to_<receiverID>.xml
).
- Dynamically including sender/receiver names in the file name (e.g.,
- Adding Incoming Message Data Segments
- Dynamically including data elements from the incoming message like OrderID, InvoiceID, etc in the file name (e.g.,
<OrderID>_yyyyMMddHHmmss.xml
).
- Dynamically including data elements from the incoming message like OrderID, InvoiceID, etc in the file name (e.g.,
- Determination of Target Location Based on Content
- At runtime determine the target directory the file should be saved based on incoming message content, incoming filename pattern, etc. (e.g. Move files starting with “Order_” or “Order” directory)
Scenario – Content-Based File Passthrough Interface
I will use the following scenario to demonstrate how the target directory can be determined during runtime and dynamically assigned to the receiver adapter.
We define the filename with a unique time stamp and copy the file name prefix from the incoming file.
Imagine a scenario where you have files with different file name prefixes in a certain directory in the SFTP server. I want to build an iFlow that can fetch and route these files to the target based on their file name prefix.
For example, files starting with “Order” should be moved to “Orders” target folder on SFTP server. Invoices to “Invoices” folder and all other files to “Other” folder.
In this scenario, we will make use of the following features of SAP Integration Suite interface development techniques,
- Standard Header/Exchange Property Parameters
- Custom Header/Exchange Property Parameters Using Content Modifier
- Camel Simple Expressions
Step 1 – Configure the SFTP Sender Adapter
I am fetching all the files in the directory “In”. Here the Location ID is the location I have registered in Cloud Connector. If you are interested in learning more you can check my complete Cloud Integration with SAP Integration Suite online course.
Step 2 – Configure Content-Based Router
The filename of the incoming file will be available in the header parameter, “CamelFileNameOnly“. We will route the files based on the prefix of the filename. Using a regex expression, we can find if the filename matches the pattern we are looking for.
${header.CamelFileNameOnly} regex 'Order.*'
Regular expression to check if the file name starts with “Order”
${header.CamelFileNameOnly} regex 'Invoice.*'
Regular expression to check if the file name starts with “Invoice”
Step 3 – Make Use of Exchange Parameter or Header Parameter to Set the Directory
Let’s make use of content modifiers to determine the directory at runtime. We will have an exchange property parameter named “directory” to set the value of the directory.
Step 4 – Configure the Reciever Adapter Using Dynamic Parameters
Make use of the Exchange Parameter to define the target directory in the receiver adapter configuration.
We will make use of a couple of Camel Expressions to define the filename dynamically.
${file:onlyname.noext}
Camel Simple Expression to get the prefix or the filename of the incoming file without the extension.
${date:now:yyyy-MM-dd}
Camel Simple Expression to add the date as the 2nd part of the file name in the format yyyy-MM-dd.
${date:now:HH-MM-SS}
Camel Simple Expression to add the time as the 3rd part of the file name in the format HH-MM-SS.
Other Methods of Setting a Dynamic File Name in SAP Integration Suite CI (CPI)
In the example, we made use of a custom Exchange/Header Parameter, a standard header parameter and a Camel Simple Expression to dynamically define the directory and filename at the receiver adapter.
However, other methods can set adapter parameters dynamically at runtime.
Using Groovy Script Or an UDF
Groovy scripting allows for complex logic when setting dynamic file names. This method is helpful when you need to combine multiple variables or perform complex transformation logic to define the adapter parameters.
import java.text.SimpleDateFormat
// Get current timestamp
def sdf = new SimpleDateFormat("yyyyMMddHHmmss")
def timeStamp = sdf.format(new Date())
// Get message ID
def messageId = message.getHeader("CamelMessageId", String.class)
// Set file name dynamically
def fileName = "file_" + messageId + "_" + timeStamp + ".xml"
// Set the file name as a header
message.setHeader("CamelFileName", fileName)
Here we define a file name in pattern: file_<messageID>_<time stamp>.xml
Using Content from the Incoming Message
You can set a dynamic file name by extracting content from the incoming message payload or headers, such as customer ID, order number, or invoice number, and appending it to the file name.
Create Use an Xpath expression or other methods to extract the values you need from the incoming message payload and assign
To summarize, we can make use of standard and custom Header/Exchange Property parameters to determine different receiver adapter parameters. Not only use parameters, but you can also use Simple Expressions to assign custom values during runtime.
You can use interface development features offered by SAP Integration Suite like Content Modifier, Groovy Scripts, or UDFs to determine the values for different parameters of the adapter like filename, directory, etc.
Encode Message Payload to Base64 on CPI!
How to use Base64 message encoder in SAP Integration Suite.
Subscribe for moreMy First Interface on CPI!
Learn how to develop your first iFlow on SAP Integration Suite within 7 minutes!
Subscribe for moreSIGN UP TODAY!
Sign up to receive our monthly newsletter and special deals!