+ Start a Discussion

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

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