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
One_Kitchen_GuyOne_Kitchen_Guy 

DML Operation on list-as-value map? Please Help

Hi guys,

Im trying to update opportunities in a list in a map and I'm tearing my hair out trying to figure it out.

So my code looks something like this...

Opportunity[] opps = [SELECT Id, Expiration__c, AccountId FROM Opportunity];

		Map<Id,Opportunity[]> expiredOppsMap = new Map<Id, Opportunity[]>();

		//Some manipulation on the Opportunities in 'opps'...

		for(Integer i=0; i<opps.size(); i++){

			if(expiredOppsMap.ContainsKey(opps[i].AccountId)){

				expiredOppsMap.get(opps[i].AccountId).add(opp[i]);

			}else{

				expiredOppsMap.put(opps[i].AccountId, new List<Opportunity>{opps[i]});

			}
		}

		update expiredOppsMap.values();

The error im getting is:
DML requires SObject or SObject list type: LIST<LIST<Opportunity>>

Can someone please explain how I can update each opportunity in the lists in the map without looping through each list in the map and adding each of the opportunities to another list then updating that new list?

Thank you in advance!!

Ankit AroraAnkit Arora
Once you are done with the changes in opps then why you want it to be added in MAP's list? Why you can't directly update it? If there is any other specific requirement which says you have to do like this then you've to loop over lists from map and manupulate the data and then update it.
bob_buzzardbob_buzzard
Each map value is a list of opportunities, not a single opportunity, so you are asking to update a list of lists of opportunities, which isn't allowed. You'll need to iterate the values, build a single list and then update them all in one go. Something like :

List<Opportunity> toUpdate=new List<Opportunity>();
for (List<Opportunity> oppsList : expiredOppsMap.values())
{
  toUpdate.addAll(oppsList);
}

update toUpdate;