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
Vaibhab ShahVaibhab Shah 

Trigger to update related opportunities of an Account

Hi,
I have a requirement where on changing the billingcity on account, i need to update a custom field city__c of all the related opportunities.

My trigger loooks like:

public static void updateCityInAllRelatedOpportunities(List<Account> accNewList,Map<Id,Account> oldMapAcc)
    {
        Set<Id> accountIds = new Set<Id>();
        for(Account acc : accNewList)
        {
            Account accOld = oldMapAcc.get(acc.Id);
            if(acc.BillingCity!=NULL && acc.BillingCity!=accOld.BillingCity)
            {
                accountIds.add(acc.Id);
            }
        }
        
        for (Opportunity oppList : [Select Id,City__C,Account.BillingCity from Opportunity where AccountId in : accountIds])
        {
           
        }
       
    }

I am getting the error like: Loop variable must be a generic SObject or List or a concrete SObject or List of: Opportunity

Rgds,
Vai.
ANUTEJANUTEJ (Salesforce Developers) 
Hi Vaibhav,

You can try using the below code and make changes as per the requirement as I just wrote the outline:
Set<Id> accountIds = new Set<Id>();
        for(Account acc : accNewList)
        {
            Account accOld = oldMapAcc.get(acc.Id);
            if(acc.BillingCity!=NULL && acc.BillingCity!=accOld.BillingCity)
            {
                accountIds.add(acc.Id);
            }

map<Id,List<Opportunity>> accountOppMap = new map<id,List<Opportunity>>();
List<Account> lstAccount = [select id, name, (select id,city__c, name from Opportunities) from account];

for(Account acc : lstAccount)
{
   accountOppMap.put(acc.id, acc.Opportunities);
    system.debug(acc.id+'----'+acc.Opportunities);
}

for (Id key : accountOppMap.keySet()) {
    
    List<String> toOpp = accountOppMap.get(key);
    for(Opportunity o: toOpp ){
	
	//update the city as per your requirement.
	}
}

For the error  I tried checking and I found this link that could be of help: https://salesforce.stackexchange.com/questions/33918/error-compile-error-loop-variable-must-be-of-type-sobject-at-line-7-column-13

I hope this helps and in case if this comes handy can you please choose this as best answer so that it can be useful for others in the future.

Regards,
Anutej
Vaibhab ShahVaibhab Shah
Hi Anutej,

Thanks for giving the hint... Here you go with the full one. Did lot of changes on the previous one....

public class AccountUtility {
    public static void updateRelatedOpportunitiesCity(List<Account> newAccount, Map<Id,Account> oldMapAccount)
    {
        //Segregating the unique Account Ids
        Set<Id> accountIds = New Set<Id>();
        for(Account acc : newAccount)
        {
            Account oldAccount = oldMapAccount.get(acc.Id);
            if(Null!=acc.BillingCity && acc.BillingCity!= oldAccount.BillingCity)
            {
                accountIds.add(acc.Id);
            }
        }
        
        //Preparing the Map of Account Ids and List of Opportunities
        Map<Id,List<opportunity>> accOppMap = new Map<Id,List<opportunity>>();
        for(Account acc : [Select id,Name,BillingCity,(Select Id,Name,Description from Opportunities) from Account where id in : accountIds])
        {
            accOppMap.put(acc.Id,acc.opportunities);
        }
        
        //Preparing the Map of Account Ids and account details
        Map<Id,Account> accMap = new Map<Id,Account>([Select id,Name,BillingCity from Account where id in : accountIds]);
        
        List<Opportunity> oppToUpdate = new List<Opportunity>();
        for(Id key : accOppMap.keyset())
        {
            List<Opportunity> oppLst = accOppMap.get(key);
            for(Opportunity opp: oppLst)
            {
                opp.Description = accMap.get(key).billingcity;
                oppToUpdate.add(opp);
            }            
        }
        
        update oppToUpdate;
        
    }
}

Please share your views.
ANUTEJANUTEJ (Salesforce Developers) 
It looks good as I had a look but I would suggest you once have a look at the following link that has a list of best practices mentioned:

>> https://developer.salesforce.com/forums/?id=906F0000000DBl8IAG

In case if this comes handy can you choose it as the best answer for future reference.