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
Giancarlo AmatiGiancarlo Amati 

Multikeys map loop

Dear All,
I have the following Case Trigger wherein bulk I create an internal list of cases (from the Trigger.new) and an internal list of accounts for each case with their SalesDivision.  
However, I'm confused by the 'for' loop as I get the error messages:

Variable does not exist: Sales_Division__c
DML requires SObject or SObject list type: Map<Id,Account>

Any help? 
Thank you
GC
 
Set<String> setAcountId = new Set<String> ();
for(Case obj: Trigger.New){
	if(obj.AccountId != null){
		setAcountId.add(obj.AccountId);
	}
}
 
//For each Account, find the Sales Division
Map<Id,Account> accWithCaseSalesDiv = new Map<Id,Account>(
	[SELECT Id, Sales_Division__c, (SELECT Id FROM Cases) FROM Account WHERE Id IN : setAcountId 
   ]
);

    for (Map<Id,Account> obj:accWithCaseSalesDiv )  {
        if (String.isEmpty(obj.Sales_Division__c)) {
            obj.Sales_Division__c = 'Americas';
            update obj;
        }
    }   

Map<Id,BusinessHours> accBHID = new Map<Id,BusinessHours>(
	[SELECT Id, name, (SELECT Id FROM Cases) FROM BusinessHours WHERE Id IN : setAcountId 
   ]
);    
MagulanDuraipandianMagulanDuraipandian
Hi,
At line no 14, use the below

for (Map<Id,Account> obj:accWithCaseSalesDiv.values() )

AT line no 17, do no update inside for loop. Add it to a list of Account and finally update after the for loop.
Giancarlo AmatiGiancarlo Amati
Hi Magulan,
Thank you for your reply. I modified the loop in the following way, but I don't uderstand you last part:
 
for (Map<Id,Account> obj : accWithCaseSalesDiv.values() )  {
        if (String.isEmpty(obj.Sales_Division__c)) {
            obj.Sales_Division__c = 'Americas';
        }
    }
I still get the error 'Variable does not exist: Sales_Division__c' which is odd. 

 
Arunkumar RArunkumar R
Hi,
You have to change your code like below,
 
  //For each Account, find the Sales Division
	Map<Id,Account> accWithCaseSalesDiv = new Map<Id,Account>(
		[SELECT Id, Sales_Division__c, (SELECT Id FROM Cases) FROM Account WHERE Id IN : setAcountId 
	   ]
	);

    for (Id currAccountId :accWithCaseSalesDiv.keySet())  {
		Account acc = accWithCaseSalesDiv.get(currAccountId);
        if (String.isEmpty(acc.Sales_Division__c)) {
            acc.Sales_Division__c = 'Americas';
        }
    }   
	
	if(accWithCaseSalesDiv.size() > 0{
		update accWithCaseSalesDiv.values();
	}

In your existing code, you are doing DML within for loop. It will hit governor limit. So I have moved the DML to outside. You just need to update the MAP.
v varaprasadv varaprasad
Hi,

Please check once below snippet.
for (Account obj:accWithCaseSalesDiv.values())  {
        if (String.isEmpty(obj.Sales_Division__c)) {
            obj.Sales_Division__c = 'Americas';           
        }
    }   
	
	 update accWithCaseSalesDiv.values();

Hope this helps you!


Thanks
Varaprasad
@For Support: varaprasad4sfdc@gmail.com​



 
Giancarlo AmatiGiancarlo Amati
Hi Arunkumar,

thank you, that worked better. However, at line 10 of your code, I still get 'Field is not writeable: Account.Sales_Division__c' which I believe has to be moved inside a 
if (Trigger.isBefore && Trigger.isUpdate)  {...}

block.
 
manish Deshmukh 6manish Deshmukh 6
Hi ,
Try Below code
for (Account obj : accWithCaseSalesDiv.values() )  {
        if (String.isEmpty(obj.Sales_Division__c)) {
            obj.Sales_Division__c = 'Americas';
        }
    }