on 04-02-2015 3:13 PM
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
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.