I have a map acctMap<id, decimal>. The keys are accountIds and value is a custom field. How to assign map keys to account ids and set the custom field to be the map values, so that I can update accounts using map data?

2 Answers 2


Just loop over the keyset of the map, and use the SObject constructor to set the Id and your custom field.

You need to loop over the keyset, because you can use the keys of the map to get the associated value, but not the other way around.


Assuming your map is called valuesByAcctId:

Account[] accts = new List<Account>();
for (Id acctId : valuesByAcctId.keySet() {
  accts.add(new(Account (Id = acctId, MyDecimalField__c = valuesByAcctId.get(acctId)));

update accts;

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .