There are several ways to create dynamic file names at the receiver adapter in SAP PI/PO. In this example, we will discuss how to create a dynamic file name at the 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 logic for file names 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).
If you are interested in learning how to assign the parameters during runtime dynamically in SAP Integration Suite, go the linked article.
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
Let’s look at how to create a file name in the above format at the 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 the 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 the dynamic configuration key ‘FileName’. You can change the UDF code here as per your file format requirement.
try { 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"; conf1.put(key1,filename); return ""; } catch(Exception e) { String exception = e.toString(); return exception; }
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 the 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 about the dynamic file generation steps discussed here.
Hi Isuru,
Great Blog!!!
Can you please explain me in step 3 to which target field are we doing the udf mapping and how the target field is getting the file name, that is being used in ASMA.
Thanks & Regards,
Akash
Hello Akash,
The target field you map the UDF is mostly irrelevant in this scenario. You can map the UDF output to the root element of the target structure. UDF does the rest to set the dynamic configuration parameter “FileName”.
Cheers!
Isuru
Hello Isuru,
I have a file to mail scenario and i simply what the file name to be kept on the mail’s attachment.
I undestand that File adapter sets de file name http://sap.com/xi/XI/System/File FileName, right?.
can can i pass this name to the attachment name using your UDF code?.
thank you so much
Nice blog. Worked perfectly thank you.
but its not working for me. it does not overload the filename given.what could be the reason of not getting the customized file name?
Hi Priya,
Maybe you are overwriting it again from the adapter?
its update. I could achieve.could not resist myself to update it. Thank you so much.
its update. I could achieve.could not resist myself to update it. Thank you so much.
its update. I could achieve.could not resist myself to update it. Thank you so much.
We have a a similar scenario, except it is IDOC–>AS2. I tried your UDF, but the AS2Filename or AS2MessageID is not being overridden. Is there a way we can use your UDF for the AS2 receiver channel?
We have a scenario which is IDOC to AS2. I tried this method but the AS2Filename or AS2MessageID does not get overridden. The logs just show the regular file name. Can you please explain if something different has to be done for the AS2 Receiver channel?