cancel
Showing results for 
Search instead for 
Did you mean: 

UDF - Substring issue

Jean-Philippe
Participant
0 Kudos

Hello,

I am trying to create dynamically receiver filename based on input filename

Filename length are fixed (41).

For instance :

File that I have receive is called XXXX-XXXX-XXXXX-AAAAA-QQQQQ-EEE

Filename that I should have in my receiver is XXXX-XXXX-XXXXX-EEE-AAAAA-QQQQQ

I am using a UDF and dynamic configuration for this purpose with substring that I am using at root level of my mapping.

When I am using only one substing, it works correctly but as soon as I am adding a second one, it is crashing saying :


Details: com.sap.aii.mappingtool.tf7.MessageMappingException; Runtime exception

when processing target-field mapping /ns0:Document; root message: Exception:[java.lang.StringIndexOutOfBoundsException:

String index out of range: -17]

I don't understand as it seems that I am wrong in filename length but that is not possible...

Maybe I am missing something else in my code... or in the execution type of my UDF (using Single values in that case)


public String FileName(String a, Container container) throws StreamTransformationException{

String filename2    = "";

DynamicConfiguration conf1 = (DynamicConfiguration) container

    .getTransformationParameters()

    .get(StreamTransformationConstants.DYNAMIC_CONFIGURATION);

//if (conf1 != null) {

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

String b = conf1.get(key1);

String e = b.substring(0, 16);

String c = b.substring(18, 11);

String d = b.substring(31, 3);

filename2 =  e + d + c ;

conf1.put(key1, filename2);

//}

  return "";

Thank you in advance for your help.

Accepted Solutions (1)

Accepted Solutions (1)

Jean-Philippe
Participant
0 Kudos

Hi,

Length is always 41. And I know when I am using b.length().

Problem here is not if parameters of substring or not are correct but when I am using more than one substring...

For instance :

- If I use String e = b.substring(0, 35); it will works...whatever the parameter..Filename in output will be 35 long.

- BUT if I use 2 substring in a row in the same UDF such as String e = b.substring(0, 35); and String c = b.substring(0, 35); it will failed when I am testing from an E2E.

I must have forget something somewhere...

Former Member
0 Kudos

Hello Jean,

You need to return your filename2 and you don't need any input parameter. Code should be like below. Adjust your substring parameter if needed.

  • public String FileName(Container container) throws StreamTransformationException{  
  • String filename2    = "";  
  • DynamicConfiguration conf1 = (DynamicConfiguration) container  
  •     .getTransformationParameters()  
  •     .get(StreamTransformationConstants.DYNAMIC_CONFIGURATION);
  • DynamicConfigurationKey key1 = DynamicConfigurationKey.create( "http:/"+"/sap.com/xi/XI/System/File","FileName");  
  • String b = conf1.get(key1);  
  • String e = b.substring(0, 16);  
  • String c = b.substring(18, 11);  
  • String d = b.substring(31, 3);  
  • filename2 =  e + d + c ;  
  • conf1.put(key1, filename2);
  • return filename2;

Regards,

Soumyadip

Jean-Philippe
Participant
0 Kudos

Hi Basu,

I still have the same failed :

Mapping "http://test/OM_TEST_XX" failed to execute: MappingException: Mapping failed in runtimeRuntime Exception when executing application mapping program com/sap/xi/tf/_MM_TEST_XX_; Details: com.sap.aii.mappingtool.tf7.MessageMappingException; Runtime exception when processing target-field mapping /ns0:Document; root message: Exception:[java.lang.StringIndexOutOfBoundsException: String index out of range: -17] in class com.sap.xi.tf._MM_TEST_XX_ method FileName[com.sap.aii.mappingtool.tf7.rt.Context@6c6961a], ApplicationRuntimeException: Runtime Exception when executing application mapping program com/sap/xi/tf/_MM_TEST_XX_; Details: com.sap.aii.mappingtool.tf7.MessageMappingException; Runtime exception when processing target-field mapping /ns0:Document; root message: Exception:[java.lang.StringIndexOutOfBoundsException: String index out of range: -17] in class com.sap.xi.tf._MM_TEST_XX_ method FileName[com.sap.aii.mappingtool.tf7.rt.Context@6c6961a], MessageMappingException: Runtime exception when processing target-field mapping /ns0:Document; root message: Exception:[java.lang.StringIndexOutOfBoundsException: String index out of range: -17] in class com.sap.xi.tf._MM_TEST_XX_ method FileName[com.sap.aii.mappingtool.tf7.rt.Context@6c6961a], BaseRuntimeException: Exception:[java.lang.StringIndexOutOfBoundsException: String index out of range: -17] in class com.sap.xi.tf._MM_TEST_XX_ method FileName[com.sap.aii.mappingtool.tf7.rt.Context@6c6961a], java.lang.reflect.InvocationTargetException, StringIndexOutOfBoundsException: String index out of range: -17


It seems that doing a second substring is making it lost the length...

azharshaikh
Active Contributor
0 Kudos

Hi Jean,

Should not be an issue....

Can you try to assign String b to some temp variables like var1, var2 and var3...

Can do Substring on these temp variables instead of doing it directly on String b.

Regards,

Azhar

Jean-Philippe
Participant
0 Kudos

Hi,

I have changed to (see below) but still the same error....

Nevertheless, it is working for just one substring...

Could it come from MM structure occurrences or missing some java libs...I am quite lost as it should work like that.


String filename2    = "";

DynamicConfiguration conf1 = (DynamicConfiguration) container

    .getTransformationParameters()

    .get(StreamTransformationConstants.DYNAMIC_CONFIGURATION);

//if (conf1 != null) {

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

//String OurSourceFileName = conf1.get(key1);

String b = conf1.get(key1);

String var1 = b;

String var2 = b;

String var3 = b;

String e = var1.substring(0, 19);

String c = var2.substring(20, 3);

String d = var3.substring(25, 5);

filename2 =  e + c +d ;

conf1.put(key1, filename2);

//}

  return filename2;

Former Member
0 Kudos

Hello Jean,

While doing substring operation the first parameter indicates start index and second parameter indicates last index. So in your case it will be like below:

  • String e = b.substring(0, 15);  
  • String c = b.substring(15, 27);  
  • String d = b.substring(27, 31);  
  • filename2 =  e + d + c;

I hope this will solve your problem.

Regards,

Soumyadip

Jean-Philippe
Participant
0 Kudos

You are right. It is working now.

Thank you for your help.

Answers (1)

Answers (1)

azharshaikh
Active Contributor
0 Kudos

Hello Jean,

Seems your Input file name String b...is less than 31.

Can you check the Length prior to doing Substring in your UDF.

Regards,

Azhar