There are several ways to create dynamic file names at receiver adapter in SAP PI/PO. In this example we will discuss how to create a dynamic file name at receiver file channel using a User Defined Function (UDF). Filename creation with UDF is a great way when the file name logic is too complicated to be handled by other file name creation methods such as variable substitution etc. Since you are writing a custom code, you can include complex logics for file name easily. You can write a simple UDF to create the file name in the message mapping. File name created in the message mapping can be set at the receiver using Adapter Specific Message Attributes (ASMA).
In this example (iDoc to File scenario) I am setting the iDoc Message type and Material number to the file name in the below format.
File name format: <Message Type>_<Material Number>_TimeStamp.XML
Lets look at the how to create file name in above format at receiver communication channel using a UDF.
Step 1: Create File Name User Defined Function (UDF).
UDF name: Set_FileName
Configure the parameters of the UDF. Since we require file name to have ‘Message Type’ and the ‘Material Number’ from the input, we need to create two import parameters in the UDF. One for ‘Message Type’ and another one for ‘Material Number’.
Step 2: Code UDF Logic to Set the File Name.
Here we need to set the file name to dynamic configuration key ‘FileName’. You can change the UDF code here as per your file format requirement.
String filename = new String("");
DynamicConfiguration conf1 = (DynamicConfiguration) container.getTransformationParameters().get(StreamTransformationConstants.DYNAMIC_CONFIGURATION);
DynamicConfigurationKey key1 = DynamicConfigurationKey.create("http:/"+"/sap.com/xi/XI/System/File","FileName");
filename = MessageType+"_"+MaterialNumber+"_.xml";
String exception = e.toString();
File name generation UDF comes in handy when your file name logic is complex and it cannot be handled by other dynamic file name generation methods such as variable substitution.
Step 3: Configure the Message Mapping program.
Map the created file name generation UDF as above. In the example we import two values from the input structure to the UDF, they are Message type (MESTYP) and Material Number (MATNR).
Step 4: Configure the receiver file communication channel .
You can set any text as the ‘File Name Schema’ as it would be replaced by the ASMA.
Configure ASMA by checking ‘Use Adapter Specific Message Attributes’ (ASMA) and also ‘File Name’. These two settings should be activated in the receiver communication channel to runtime to override the hardcoded file name in ‘File Name Schema’ with the file name set from the UDF.
Set the time stamp since we need the timestamp at the end of the file name.
Test Case for Dynamic File Generation using UDF.
If you would like to learn configuration steps for dynamic file name generation using variable substitution method, check out the linked article. Additionally, I have written another post to illustrate how to encode and decode Base64 using UDFs. You can follow all posts related to UDFs here.
Please leave a comment if you have any questions of the dynamic file generation steps discussed here.