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
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;