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
:) :) :) :) :):) :) :) :) :) 

How to update sObject inside Map

Is there a way we can update List or sObject inside Map?

 

Map<id,Opportunity> oppMap = new Map<id,Opportunity>([select id, Name from opportunity]);

 

oppMap.put(OPPORTUNITY_ID, Opportunity.set(1,'oppName'));     //This is currently not available I am just showing you an example.

 

update Map.values();

:) :) :) :) :):) :) :) :) :)

I found Something intersting.

 

Map<id,Opportunity> oppMap = new Map<id,Opportunity>();
oppMap.put('006d000000Cu0fj', new Opportunity(id = '006d000000Cu0fj', name= 'updated Opp'));
update oppMap.values();

 

sfdcfoxsfdcfox
You can update a specific entry in a map like so:

oppMap.get(OPPORTUNITY_ID).Name='OppName';

You can also iterate through the records, like this:

for(opportunity record:oppmap.values()) {
// update record field values
}
:) :) :) :) :):) :) :) :) :)

Thanks for your update but I am specifically looking to update sobject within Map it self. I am getting  System.LimitException: Too many code statements: 200001 Error. So I if I do for loop its giving me this error.

sfdcfoxsfdcfox
You're not providing any information, so I can't really help you. Your loop is written incorrectly if you're getting an error like this. It has nothing to do with updating an sobject in a map.