Dynamic File Name UDF Example – SAP PI/PO

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’.

Configure dynamic file name UDF import parameters
Configure UDF import parameters

 

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.

 

UDF java code to set file name
UDF java code to set file name

 

 

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 created UDF in Message Mapping program
Map created UDF in 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-receiver-file-communication-channel
Configure file communication channel

 

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.

Configure receiver adapter Adapter Specific Message Attributes
Configure receiver adapter Adapter Specific Message Attributes

 

Set the time stamp since we need the timestamp at the end of the file name.

Receiver Communication Channel timestamp configuration
Receiver Communication Channel timestamp configuration

 

Test Case for Dynamic File Generation using UDF.

 

File name in Message Monitor log.
File name in Message Monitor log.

 

If you would like to learn configuration steps for dynamic file name generation using variable substitution method, check out the linked article.

Please leave a comment if you have any questions of the dynamic file generation steps discussed here.

One Comment

Add a Comment

Your email address will not be published. Required fields are marked *