cancel
Showing results for 
Search instead for 
Did you mean: 

UDF to capture the source file name or the source folder for mapping

Former Member
0 Kudos

Hi All,

i have 2 files - File1 from File2 from 2 different folders- single comm channel FTP (FCC). i have to pass a constant A and B respectively to a field in target structure.

my options are -

either create a new field in source str populating different constants and then map to the target str

or a udf to get the source file name or the source folder so that i can map the constant using the value mapping/FixValue mapping.

please suggest. thanks.

rajib

Accepted Solutions (1)

Accepted Solutions (1)

deepak_shah
Contributor
0 Kudos

Hi,

please use the UDF to determine the file name. Then based on file name either pass A or B to your target field.

The code is as given below. Just tweak the code to pass either A or B as per your requirement.

public String G_GetFileName(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 ourSourceFileName = conf.get(key);

return ourSourceFileName;

Regards,

Deepak.

Former Member
0 Kudos

hi deepak,

i have been trying to work out the UDF. i am not much used to udf so could you pl let me know what will be the value in Imports? few elaborations would be very helpful. thanks.

regards,.

baskar_gopalakrishnan2
Active Contributor
0 Kudos

These imports are required.

import com.sap.aii.mapping.api.DynamicConfiguration;

import com.sap.aii.mapping.api.DynamicConfigurationKey;

import com.sap.aii.mapping.api.StreamTransformation;

import com.sap.aii.mapping.api.StreamTransformationException;

Former Member
0 Kudos

Hi Bhasker,

i have put the below imports like this - com.sap.aii.mapping.api.DynamicConfigurationKey;com.sap.aii.mapping.api.DynamicConfiguration;com.sap.aii.mapping.api.StreamTransformationException;com.sap.aii.mapping.api.StreamTransformation;

and code -

DynamicConfiguration conf = (DynamicConfiguration) container.getTransformationParameters().get(StreamTransformationConstants.DYNAMIC_CONFIGURATION);

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

String ourSourceFileName = conf.get(key);

return ourSourceFileName;

getting error -

RuntimeException in Message-Mapping transformation: Exception:[java.lang.NullPointerException] in class com.sap.xi.tf._MM_File2Bapi_ method G_GetFileName$[com.sap.aii.mappingtool.tf3.rt.Context@5db43aa]

baskar_gopalakrishnan2
Active Contributor
0 Kudos

Your coding looks good except import statement not found in the first line. You get runtime exception. This will occur if the file does not reside in the directory.

Basically key is null and you are trying to do lookup using key. If you want to avoid exception during no file exist, you can check not null condition

if(key != null){

String ourSourceFileName = conf.get(key);

}

But still that does not help...for mapping to target fields.

Former Member
0 Kudos

thanks.

ok i think i am near now. am able to pass the file name directly to the target through the udf.

But i have 2 files from different location now and got the file names using this udf. Now both files has dates at the end. so am not able to pass AAA for File1.txt and BBB for File2.txt. am trying this through the fixed value mapping. please suggest.

after the file name extraction code, i have included (* -for date n timestamps):

if(ourSourceFileName.equals("ABC*.txt"))

return "FILE1";

else if(ourSourceFileName.equals("XYZ*.txt"))

return "FILE2";

Answers (1)

Answers (1)

baskar_gopalakrishnan2
Active Contributor
0 Kudos

Go for this option.

either create a new field in source str populating different constants and then map to the target string

UDF can be avoided if you have an easy option.

Former Member
0 Kudos

Hi Bhasker,

the problem is, the source file do not have the required field which should contain my constant. So i am trying to populate a new field in the XI itself??

regards,

baskar_gopalakrishnan2
Active Contributor
0 Kudos

Okay. Now I got your entire need. In that case use Dynamic configuration to fetch the filename details using UDF