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
sfdev1sfdev1 

Trigger on Opportunity that update the Account Object not working


I have created a custom field called 'Status' on the Account object. I has three picklist values : Prospect, Active and Dormant
 
The default is Propects' I an Opportunity is Closed -Won then I want to update the Account.Status field to Active
 
I started with the following code

trigger Test on Opportunity (after insert, after update) {
    List<Account> accToUpdate= new List<Account>;
 
    for (Opportunity opp : System.Trigger.new) {
       Account accounts = [select id,name,industry from account where id in opp.accountid];
       for (Account acc: accounts){
         if (acc.status__c != 'Active'){
                acc.staus__C = 'Active';
                accToUpdate.add(acc);
             }
        }
    }
    update accToUpdate;
}­­
Execute EZSAASExecute EZSAAS
You'll need to get status__c field in your SOQL - that should make it work.

I'll also recommend to use Maps for bulk updates.

IspitaIspita
Yes execute is rite you have to include the field you wamt to update here status__c in the query for retrieving account.