cancel
Showing results for 
Search instead for 
Did you mean: 

Fetching the value from the filename and pass it to the target field.

former_member223432
Participant
0 Kudos
849

Hi Experts

I am working on File to Idoc scenario,


the filename has the invoice number in it and i need to pass the same to the field in the idoc.

Filename: INVOICE_JULY_US_123456_FR.csv. here 123456 is the invoice number which i need to pass it to the idoc field.

I looked into few variable substitution posts but its mostly used for receiver channel. my question is how can i fetch only the Invoice number(123456) from the filename and use it in the mapping.

Kindly assist . if you have any UDF or code, please share it.

thanks

Accepted Solutions (1)

Accepted Solutions (1)

anupam_ghosh2
Active Contributor
0 Kudos

Hi Smith,

Please try this UDF

public String extractInvoiceNumberFromFileName(Container container) throws StreamTransformationException{
DynamicConfiguration conf = (DynamicConfiguration) container.getTransformationParameters().get(StreamTransformationConstants.DYNAMIC_CONFIGURATION);

DynamicConfigurationKey key = DynamicConfigurationKey.create("http://sap.com/xi/XI/System/File","FileName");

String fileName = conf.get(key);
String invoiceNumber="";
		for(int i=0;i<fileName.length();++i)
		{
			if(fileName.charAt(i)>='0' && fileName.charAt(i)<='9')
			{
				invoiceNumber=invoiceNumber+ fileName.charAt(i);
			}
		}
return invoiceNumber;


}

Answers (2)

Answers (2)

alex_bundschuh
Product and Topic Expert
Product and Topic Expert

Hi,

in the file sender adapter you need to select the Set Adapter-Specific Message Attributes flag, then this adds the file name to the header FileName of attribute namespace http://sap.com/xi/XI/System/File.

see https://help.sap.com/viewer/5cf7d2de571a45cc81f91261668b7361/7.5.21/en-US/44682bcd7f2a6d12e10000000a...

Via a UDF, you can read the header. In this blog I have a code example how to do this: https://blogs.sap.com/2014/12/18/pi-rest-adapter-using-dynamic-attributes/

And then read the substring via standard functions. If your file name follows a specific template, you can determine the length of the attribute value using the function length, and from there extract the number via function substring.

Alex

former_member223432
Participant
0 Kudos

thank you both for the answer.