cancel
Showing results for 
Search instead for 
Did you mean: 

Concatenate 5 fields into one

Former Member
0 Kudos

Hi ,

I tried to use Concatenate function in graphical mapping but does not seem to work. How to Concatenate 5 different fields to on target field?

Do I need to write a UDF? 

eg, Val1

      Val2

      Val3

      Val4

      Val5

Concatenate above 5 fields to one target field  String without delimiter  ?

Please advice.

Thanks,

Teresa

Accepted Solutions (1)

Accepted Solutions (1)

iaki_vila
Active Contributor
0 Kudos

Hi Teresa,

I've edited this answers because only works if the values comes from the same source field, the same tag repeated several times.

If you want to follow the unction library for future mappings.

You could put like category type all values on context or all values on queue, it depens how the values come to the UDF, and later:

imports java.lang.*;

public void nConcatenate(String input[], ResultList result, container Container){

String stringConcatenated = "";

for (i = 0; i < input.length; i++){

stringConcatenated += input[i];

}

result.addValue(stringConcatenated);

}

Finally you have an UDF that you could use to concatenate five or the number or n strings that come in the input queue

Regards.

Message was edited by: Iñaki Vila

Former Member
0 Kudos

Hi Vila,

I did define the UDF as mentioned by you. The input to the UDF is from 5 different fields. I am able to provide input to only one field to the UDF above. Am i doing something wrong? I have put category type all values on queue.

Thanks,

Teresa

former_member184681
Active Contributor
0 Kudos

Hi Teresa,

I believe this is because of the UDF: it concatenates all the values from one queue, and not five different fields (five different queues). For your requirement, use something more like Former Membermentioned. Still, if the queues of the input fields are not similar (in terms of contexts), the UDF might produce unexpected results.

Hope this helps,

Greg

rajasekhar_reddy14
Active Contributor
0 Kudos

  Hi,

    I never faced prob with concat like gicing supress value, the simple solution to debug your issue is use mapwithdefualt(blank) and use standard function concatc and check the result.

As other expecrts suggested using UDF for this requirement is good idea but debbuging is little bit risky.

Regards,

Raj

baskar_gopalakrishnan2
Active Contributor
0 Kudos

This is not hard requirement. You can pretty much handle through standard function or UDF. If you don't want UDF, go as Raja pointed out.  I have shown above code if you are getting blank values in the runtime. Use any of the above solutions. If you get repeated values in the queue, then you might have to pass five string list for each parameter. If you have issues, provide your mapping requirements here.

Former Member
0 Kudos

Hi Baskar,

Requirement is between mapping a CRM idoc and PORDCH03 idoc. I need to concatenate the following fields from CRM idoc which belong to different segments.

Where should the  target segment E1BPMEPOTEXT  be mapped as each of the source fields come from different segments ? Should I change the Context of the source fields to the Main segment in the source like IDOC ?

Thanks,

Teresa

baskar_gopalakrishnan2
Active Contributor
0 Kudos

If I understood correctly, Looks like you need to concat different idoc field segments  to the target field.

Is that so? You don' t have values coming from multiple context of the same field in the source queue.

Just use the above UDF.  Use execution type single values  and select parameters five as var1, var2...var5.   Java Type as  String for all of them.  use the above UDF and see the result.

input five fields map to udf and udf to target field.

Former Member
0 Kudos

Thanks Baskar. Yes different idoc field segment to one target idoc segment. I did create the UDF but when i define it as a parameter . How do I put the field values from different idoc segments?

baskar_gopalakrishnan2
Active Contributor
0 Kudos

If you create five parameters like var1, var2..var5  and use the udf code and activate.  After that you will see five inputs for that UDF. Assign your idoc five diiferent field elements as inputs to the UDF and map the UDF output to target element.

Former Member
0 Kudos

Hi Baskar,

I saved and activated as you mentioned. I still do not see the inputs when i define the category as single values.

Also I am getting following error during activation.

Function ConcateString, Line 17: cannot find symbol symbol  : variable result location: class com.sap.xi.tf._MES_MAP_CRM_EXT  result.addValue(sBuf.toString());  ^ Note: /usr/sap/DX7/DVEBMGS00/j2ee/cluster/server2/./temp/classpath_resolver/Mapa4f36d2f8fcd11e1bbc1000000794138/source/com/sap/xi/tf/_MES_MAP_CRM_EXT_SPM2Paragon_TO_PORDCH03_.java uses or overrides a deprecated API. Note: Recompile with -Xlint:deprecation for details. 1 error

baskar_gopalakrishnan2
Active Contributor
0 Kudos

If you create execution type as single value then  change the last line as follows

dont use result object instead as follows

return sBuf.toString();

Former Member
0 Kudos

Hi Baskar... Apprecaite your help. I activated it succesfully. Now i have these 5 fields from different segments in Source CRM idoc when I use the UDF I cannot input any of these . When I double click on UDF i see 5 variable var1 , var2 , var3. How do I map the source fields to these?

Thanks again.

baskar_gopalakrishnan2
Active Contributor
0 Kudos

Here is the source code screenshot..

Are you seeing five inputs UDF as below?

Map your 5 source fields as input on the left (one after other) and map the output to the target element.

Former Member
0 Kudos

Thank you it worked. Appreciate your patience and help .

baskar_gopalakrishnan2
Active Contributor
0 Kudos

I'm so glad that it helped and solved your problem. You might also want to check sending empty values in some input fields and this logic will take care that issue too.

Former Member
0 Kudos

Thanks Baskar. One more issue How to deal with Contexts ? I see the fields in the source segment have different contexts. I think this is causing the UDF to not work properly. Do i need to change the contexts of the source ? The UDF works perfectly fine when I tried with Constants.

Please advice.

former_member184681
Active Contributor
0 Kudos

Hi Teresa,

If you need to simply adjust the context changes between different queues, maybe you could make use of the formatByExample function? But bear that in mind that working with contexts actually depends strictly on your functional requirement.

Hope this helps,

Greg

Former Member
0 Kudos

Hi Greg,

Its like this var1 is coming from a segment which has a different context and var2 is coming from a different segment with multiple values and var3 is coming in with only 2 values so its kind of messsing up the concat UDF .

I will try out your suggestion.

Thanks,

Teresa

baskar_gopalakrishnan2
Active Contributor
0 Kudos

The above UDF is not the right candidate for handling values in contexts.  I already asked this question in the above replies.  You might have to change the code to handle it in different way. But as Greg said its truly based on business requirement. I cannot fully guarantee that this will help your need.

IMO, efficient way of handling string array concat is through java generic. Unfortunately it is not so easy to handle using UDF rather I would prefer java mapping for this.  You might want to try the below logic in UDF and see how that helps.

You need to individually concat five different segments value for each index and return the ouput as a single string array.

Change the execution type to All values of context

Set context for each element to parent node

Use 6 arguments in the method as below

Sample code

Note: Below I default the size of the output array length using the first string array list length. What will happen if the first string array list has some empty values. In that case length might be lesser compared to other string arrays length.

Former Member
0 Kudos

Hi Baskar,

  I am getting the following error below. Is th UDF i have typed correct?

Function Concatarr, Line 2: arrOutput is already defined in Concatarr(java.lang.String[],java.lang.String[],java.lang.String[],java.lang.String[],java.lang.String[],java.lang.String[],java.lang.String[],com.sap.aii.mappingtool.tf7.rt.ResultList,com.sap.aii.mappingtool.tf7.rt.Container) String[] arrOutput = new String[arr1.length];          ^ Function Concatarr, Line 10: cannot find symbol symbol  : variable result location: class com.sap.xi.tf._MES_MAP_CRM_EXT_PORDCH03_ result.addValue(arrOutput); ^ . Note: Recompile with -Xlint:deprecation for details. 2 errors

{ String[] arrOutput = new String[arr1.length];

for(int i = 0 ; i < arrOutput.length; i++)

{

arrOutput[i] = arr1[i] + arr2[i] + arr3[i] + arr4[i] + arr5[i] + arr6[i] + arr7[i];

}

result.addValue(arrOutput);

}

Thanks,

Teresa

baskar_gopalakrishnan2
Active Contributor
0 Kudos

That was sample code. When you create method with execution type as All values of context you get below method signatures with ResultList as parameter.

Here is below code.

public void concatStringArray(String[] arr1, String[] arr2, String[] arr3, String[] arr4 String[] arr5,  ResultList result, Container container) throws StreamTransformationException{

String[] arrOutput = new String[arr1.length];
for(int i =0; i < arrOutput.length; i++){

  arrOutput[i] = arr1[i] + arr2 [i] + arr3[i] + arr4[i] + arr5 [i];

}
result.addValue(arrOutput);

}

Former Member
0 Kudos

Hi Baskar,

I am getting an error is there another bracket that is needed.

Function Concat, Line 7: open bracket '{' missing for closed bracket '}':  }  ^

public void Concat(String[] arr1, String[] arr2, String[] arr3, String[] arr4, String[] arr5, String[] arr6, String[] arr7, ResultList arrOutput, Container container) throws StreamTransformationException{

String stringConcatenated = "";

for (i = 0; i < input.length; i++){

stringConcatenated += input[i];

}

result.addValue(stringConcatenated);

}

Thanks,

Teresa

baskar_gopalakrishnan2
Active Contributor
0 Kudos

You dont use my code.  You need 5 string arrays. Your code is wrong. Use the previous above code I posted.

Former Member
0 Kudos

Hi Baskar,

I have defined in my scenario seven input fields of type Argument and JAVE type string and Name arr1 , arr2 .. arr7 .

The execution type is All values in context.

You are saying not to use the code above ? Please advice.

Thanks,

Teresa.

baskar_gopalakrishnan2
Active Contributor
0 Kudos

use below code, I said

public void concatStringArray(String[] arr1, String[] arr2, String[] arr3, String[] arr4 String[] arr5,  ResultList result, Container container) throws StreamTransformationException{

String[] arrOutput = new String[arr1.length];
for(int i =0; i < arrOutput.length; i++){

  arrOutput[i] = arr1[i] + arr2 [i] + arr3[i] + arr4[i] + arr5 [i];

}
result.addValue(arrOutput);

}

Former Member
0 Kudos

Thanks Baskar. I am getting a funky output in the UDF like shown in the snapshot.

baskar_gopalakrishnan2
Active Contributor
0 Kudos

This logic would not help. I already pointed out. The values exists from different queue for different segment and we don't coordinate accordingly for all the five fields. Plus we do get nulll values for some and we don't catch too. You might want to look other options. This would not help.

Former Member
0 Kudos

What other options do we have? I didnt know that concat would be so complicated . You are right....say arr1 has 6 values and arr2 is missing 2 values it will be difficult to know which values were missing.

Please suggest some ideas !! Thank you very much

Answers (4)

Answers (4)

Former Member
0 Kudos

I can post any attachments here in SDN. My file size is in form of JPEG or PNG and less than 1 MB. Is there any other file extension that is acceptable?

Former Member
0 Kudos

I'm not in favor of the concat funciton. I would go with UDF, much cleaner.

You will need 5 arguments

String concatValues;

concatValue = val1 + val2 + val3 + val4 + val5;

return concatValues'

something similar to above.

baskar_gopalakrishnan2
Active Contributor
0 Kudos

I would use the UDF and make it simpler.

create method using 5 arguments 

 

StringBuffer sBuf = new StringBuffer();

  sBuf = var1.length()>0? sBuf.append(var1): sBuf.append("");

  sBuf = var2.length()>0? sBuf.append(var2): sBuf.append("");

  sBuf = var3.length()>0? sBuf.append(var3): sBuf.append("");

  sBuf = var4.length()>0? sBuf.append(var4): sBuf.append("");

  sBuf = var5.length()>0? sBuf.append(var5): sBuf.append("");

  result.addValue(sBuf.toString());

source five fields  --> UDF --> target field

Modified code to handle if the value comes empty.

Former Member
0 Kudos

Hi Baskar,

I am getting the following error below for your UDF.

Thanks,

Teresa

cannot find symbol symbol  : method length() location: class java.lang.String[]  sBuf = var1.length()>0? sBuf.append(var1): sBuf.append("");              ^ Function ConcatStr, Line 5: cannot find symbol symbol  : method length() location: class java.lang.String[]  sBuf = var2.length()>0? sBuf.append(var2): sBuf.append("");              ^ Function ConcatStr, Line 7: cannot find symbol symbol  : method length() location: class java.lang.String[]  sBuf = var3.length()>0? sBuf.append(var3): sBuf.append("");              ^ Function ConcatStr, Line 9: cannot find symbol symbol  : method length() location: class java.lang.String[]  sBuf = var4.length()>0? sBuf.append(var4): sBuf.append("");              ^ Function ConcatStr, Line 11: cannot find symbol symbol  : method length() location: class java.lang.String[]  sBuf = var5.length()>0? sBuf.append(var5): sBuf.append("")

baskar_gopalakrishnan2
Active Contributor
0 Kudos

The UDF is coded for passing five string arguments not as string [].  If you pass  five string[] then the above code has to be modified. Again, we reference the length in the arrray it is just length not length().

Are you having repeated values in the Queue? If so the above logic would not work.

former_member184681
Active Contributor
0 Kudos

Hi Teresa,

Standard PI's concat function can only concatenate two arguments, but still you can use one concat by another to achieve the required result. Just map the result of the first concat as one of the parameters of the later concat, and so on. UDF can also be an alternative, if you want to keep the mapping more readable.

Hope this helps,

Greg

Former Member
0 Kudos

Hi Greg,

I did use the Concat of first two as input argument to another Concat. When i go to display queue it shows me SUPPRESS . What happens if there is no value to concatenate in one of the fields.

Thanks,

Teresa

former_member184681
Active Contributor
0 Kudos

Hi Teresa,

I don't remember seeing a SUPPRESS as an output from concat function. As far as I remember, it takes the value from the previous context, if there is a value missing in the next one. So in each context, you will have as many outputs, as in the input parameter that has more values in that context.

Are you sure the SUPPRESS is not generated by another function, like createIf (before or after concat)? Could you please post a screenshot from Display Queue of the concat function?

Regards,

Greg