cancel
Showing results for 
Search instead for 
Did you mean: 

How can we compare collection extension values?

Ramesh
Explorer
0 Kudos

Business Requirement:

Collection Field (REGION) has below values:

EMEA

APAC

If duplicate values are added should error out at the time of save e.g,

EMEA

EMEA 

Tried with the below document level script to error it out but is not working.

import com.sap.odp.api.ibean.common.AbsExtensionCollection.*;

geoCol = doc.getExtensionCollection("COLLGEOGRAPHY");

if (!(geoCol.size() == 0)) {

  geography = geoCol.get(0);

  regionNew = geography.get("REGION");

}

iter = geoCol.iterator();

while(iter.hasNext())

{

nextItem = iter.next();

region = nextItem.get("BLPREGION");

  if(! (hasValue(region))) {

    throw doc.createApplicationException("COLLGEOGRAPHY", "PROJERROR_REGION");

  }

  if (!(regionNew.equals(region))) {

    throw doc.createApplicationException("COLLGEOGRAPHY", "PROJERROR_REGION");

  }

}

Any idea will be appreciated.

Thanks,

Ramesh

Accepted Solutions (1)

Accepted Solutions (1)

former_member190023
Contributor
0 Kudos

Hello Ramesh,

Simplest way to check for duplicates is to add them to a HashSet.

Because the HashSet can only contain unique values, the add() method will return true if added or false if it exists.

Sample:

HashSet set = new HashSet();

geoCol = doc.getExtensionCollection("COLLGEOGRAPHY");

iter = geoCol.iterator();

while(iter.hasNext())

{

  item = iter.next();

  region = item.getExtensionField("BLPREGION");

  if(!hasValue(region)) {

  throw doc.createApplicationException("COLLGEOGRAPHY", "PROJERROR_REGION");

  }

  if(!set.add(region)) {

  throw doc.createApplicationException("COLLGEOGRAPHY", "PROJERROR_REGION");

  }

}

PS: I hope my code syntax is ok

Bogdan Toma

Ramesh
Explorer
0 Kudos

Bogdan, thank you for taking your time to answer this, it worked like a gem appreciate your help with this!

Answers (0)