cancel
Showing results for 
Search instead for 
Did you mean: 

How to read a collection field values

Ramesh
Explorer
0 Kudos

Business Requirement:

Collection Field (REGION and COUNTRY) has below values:

EMEA                   Armenia

APAC                   China

Global                  blank

I have a requirement in the collection to find if the region value is "Global" to issue an error :

import com.sap.odp.api.doccommon.masterdata.*;

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

import com.sap.odp.common.db.metadata.*;

geoCol = doc.getExtensionCollection("COLLGEOGRAPHY");

// New collection entries are captured in the below variables

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

geography = geoCol.get(0);

regionNew = geography.get("REGION");

countryNew = geography.get("GEOCOUNTRY");

}

set = new HashSet();

iter = geoCol.iterator();

while(iter.hasNext())

{

nextItem = iter.next();

region = nextItem.get("REGION");

country = nextItem.get("GEOCOUNTRY");


// If the region is blank mandate region

if(! (hasValue(region))) {

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

}

// Error for duplicate countries

if (!set.add(country)) {

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

}

if ((regionNew.equals("Global"))) {

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

}

}

The last IF condition is not working in the above scenario how do I read a collection value?

Thanks,
Ramesh

Accepted Solutions (0)

Answers (1)

Answers (1)

former_member190023
Contributor
0 Kudos

Hello Ramesh,

I feel that this block is obsolete:

// New collection entries are captured in the below variables

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

geography = geoCol.get(0);

regionNew = geography.get("REGION");

countryNew = geography.get("GEOCOUNTRY");

}

If you are not purposely doing something with the *first* value of the collection you should remove it.

For the validation, you are doing everything correct, except you are checking 'regionNew', which will always have the first value of the collection.

To check all the values you have to replace the if with:

if ((region.equals("Global"))) {

Bogdan

Ramesh
Explorer
0 Kudos

Thanks Bogdan, for taking time to respond. I tried that for some reason it does not recognize the value list value 'Global' though I see that in the drop down. Is there any other way I can compare the literal? I tried region == "Global" but no luck.

former_member190023
Contributor
0 Kudos

Oh, my mistake then, I thought it was a String value.

You'll need to get the VLV display name before

import com.sap.odp.api.ibean.IBeanHomeLocator;

regionName = IBeanHomeLocator.lookup(session, region).find(region).getDisplayName();

if (regionName.equals("Global")) {

...


}

Bogdan