+ Start a Discussion
JkkJkk 

How to update an account field with value='abc' if any one of the corresponding opportunities have custom field test__c='xyz' via trigger?

How to update an account field with value='abc' if any one of the corresponding opportunities have custom field test__c='xyz' via trigger?
Danish HodaDanish Hoda
Set<Id> setAccIds= new Set<Id>();
List<Account> accountsToUpdate = new List<Account>();
for(Opportunity opty : [SELECT Id, AccountId FROM Opportunity WHERE Id IN :trigger.new]){
if(opty.test__c == 'xyz'){
setAccIds.add(opty.AccountId);
}
}
if(setAccIds != null && setAccIds.size() > 0){
for(Account account : [SELECT Id, accField__c FROM Account WHERE Id IN :setAccIds ]){
account.accField__c = 'abc';
accountsToUpdate.add(account);
}
}
if(!accountsToUpdate.isEmpty())    update accountsToUpdate;