function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Jacob W LandisJacob W Landis 

how to access list by index in the map

Hi,

we have code below, I got error "Initial term of field expression must be a concrete SObject:list<id>", any suggestion?

Map<ID,list<ID>> idMappings = new Map<ID, list<ID>>();
Set<ID> toBeUpdatedIds = new set<ID>();

// then the code to add data into idMappings;

           for(Id key: idMappings.keySet()){
                if (idMappings.get(key).size()==1){
                    toBeUpdatedIds.add(idMappings.get(key).get[0]);
                }else{
                    toBeUpdatedIds.add(idMappings.get(key).get[idMappings.get(key).size()-1]);
                }
           }
Amit Chaudhary 8Amit Chaudhary 8
Hi Jingli Hu,

Please try below code. I hope that will help u 
Map<ID,list<ID>> idMappings = new Map<ID, list<ID>>();
Set<ID> toBeUpdatedIds = new set<ID>();
for( Id key: idMappings.keySet() ){
	List<Id> lstId = idMappings.get(key);
	if ( lstId.size()==1 )
	{
		toBeUpdatedIds.add(lstId[0]);
	}
	else
	{
		toBeUpdatedIds.add(lstId[lstId.size()-1]);
	}
}

Please let us know if this will help u
 
Baljeet KaurBaljeet Kaur
  toBeUpdatedIds.add(idMappings.get(key).get[0]);
  Just Add 
  toBeUpdatedIds.add(idMappings.get(key)[0]); to be get value From Map Of List on Index based.