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
SFDummySFDummy 

How to update map values based on key?

I have a map with id and Account map<id, account> here the sample code
map<id, Account> updateAccts = new map<id, Account>();
List<Account> getAccts = [SELECT id, Case_Mailed_to_Group__c, Case_In_Work__c, CKE__C FROM Account WHERE ID in: caseAccUpdateIds ];

for(Account a: getAccts){
   if(updateAccts.containskey(a.id){
     
         //update account field in the map   HOW update CKE__c field on account
        Account ac = updateAccts.get(a.Id);
        ac.CKE__C = a.CKE__C;  //not working
   }eles{
      updateAccts.put(a.id, a);
   }
}
update updateAccts.values();
Amit Chaudhary 8Amit Chaudhary 8
Hi SFDummy ,

Base on Salesforce account ID you are checking "containskey". In this case your code will not come in if condition. Because account ID will be unique because you are fatching "getAccts" list base on account id. So it will give unique acccont id only.
You can try below code.
map<id, Account> updateAccts = new map<id, Account>([SELECT id, Case_Mailed_to_Group__c, Case_In_Work__c, CKE__C FROM Account WHERE ID in: caseAccUpdateIds ] );

for(Account a: updateAccts.values )
{
		// Add Some value here
       // ac.CKE__C = a.CKE__C;  //not working
}

Please let me know if this will help you.

Thanks
Amit Chaudhary
CRMScienceKirkCRMScienceKirk
The questions kind of vague, so we don't really know precisely what you're looking to do, but your code has some problems.  Maybe you're trying to update the Account CKE__c based off of a parent case's CKE__c or Id?

Your query is only going to return a single Account once, so looping through the query results and checking to see if you've already worked modified isn't going to do anything.  The FOR loop will iterate through each Account in the collection once, so the check to see if updateAccts contains the Account will never be true; you'll need a 2nd loop (if that's appropriate).Within your loop, it looks like you're trying to set a CKE__c field to the value of a matching Account's CKE__c field, (even if the loop worked like you have it, the source and target values of the field would be the same).



 
SFDummySFDummy
I am processing multiple criterias in my class, after initial process I have added values to map.

I have to do second level of process, where I need to check if in previous logic the accont is added to the map and only update relevant field. 
CKE__C is just an example, I am process 4 other fields 
map<id, Account> updateAccts = new map<id, Account>();
// add accounts to map if Rates criteria met  business logic cannot post here
// create list of accIds that meet Case criteria - CaseaccUpdatIds
// Case_Mailed_to_Group__c is a date field

List<Account> getAccts = [SELECT id, Case_Mailed_to_Group__c, Case_In_Work__c, CKE__C FROM Account WHERE ID in: caseAccUpdateIds ];

for(Account a: getAccts){
   if(updateAccts.containskey(a.id){
     
         //update account field in the map   HOW update CKE__c field on account
        Account ac = updateAccts.get(a.Id);
        ac.CKE__C = a.CKE__C;  //not working
        // ac.case_mailed_to_Group is less than a.Case_Mailed_to_Group__c
       // update account field  .
   }eles{
      updateAccts.put(a.id, a);
   }
}
update updateAccts.values();

I have added comments, I am still working on my code. I am open for any suggestions to improve my code.
(1) I am creating map that  require rate updates
(2) compare date fields to update only those account that were updated in the step #1