cancel
Showing results for 
Search instead for 
Did you mean: 

How to remove duplicates in a field

0 Kudos

Hi Folks,

Currently my project is using SAP PI 7.0 Version.

for one of the requirement : i need to remove the duplicates in the filed:

i wrote UDF as below:

ArrayList aList = new ArrayList();

aList.add(a[0]);

result.addValue(a[0]);

for(int i=1; i<a.length; i++){

if(aList.contains(a[i]))

     continue;

else{

aList.add(a[i]);

result.addValue(a[i]);

}

}

but i am getting the below error:

RuntimeException in Message-Mapping transformation: Runtime exception during processing target field mapping

/ns0:Messages/ns0:Message1/Offer/LocationGroups/LocationGroup[8]/LocationGroupID. The message is:

Exception:[java.lang.StringIndexOutOfBoundsException] in class com.sap.aii.mappingtool.flib3.

TextFunctions method substring[, com.sap.aii.mappingtool.tf3.rt.Context@14d914d9]

Kindly check and provide me the Exact UDF code or any alternate method to resolve the issue.

Thanks in advance.

best regards,

Hari.

Accepted Solutions (1)

Accepted Solutions (1)

yeeloon-khoo
Explorer
0 Kudos

Hi Hari,

The queue in your example, all the values is unique, not duplicated, so probably not UDF problem.

The error is complaint about string index out-of-bound at LocationGroup[8], which is 9th start from index 0. At 9th string of your queue is empty string, so hit error when map to substring(1..4) but with string with zero length.

Are you sure is UDF lead to this error or providing the correct error? post a display queue of the removeDuplicate might help.

I have an UDF similar with your UDF too is working fine.http://scn.sap.com/docs/DOC-63651

List uniqueList = new ArrayList();


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

  String value = contextValues[i];


  if (!uniqueList.contains(value)) {

      uniqueList.add(value);

      result.addValue(value);

  }
}

Regards,

Yee Loon

0 Kudos

Hi Yee Loon Khoo,

thanks for your quick reply.

thanks for the above UDF.

i have solved the issue by using existing fuctions.

Answers (2)

Answers (2)

Former Member
0 Kudos

Refer the Raghu's blog  -

sahithi_moparthi
Contributor
0 Kudos

Hi Hari,

Please use belowe UDF at context :

String[] v_current = new String [var1.length];

v_current[0] = "";

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

{

      if( var1.length == 1 )

     {

          result.addValue( var1[i]);

     }

                      else  if( var1[i].compareTo(v_current[0]) != 0)

     {

          result.addValue( var1[i]);

           v_current[0] = var1[i];

     }

}