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

 
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
 
public class OpportunityUpdateClass{
 
   public stats void UpdateAccountStatustoActive(Opportunity[] opps){
 
         Map<String,Opportunity> oppMap = new Map<String,Opportunity>();
 
          for (Opportunity opp:opps){
               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'
                      }
                }
         }
 
   }
}
micwamicwa
You forgot to update the account:

Code:
trigger Test on Opportunity (after insert, after update) {
    Map<Id,Opportunity> opps= Trigger.newMap;
    
    List<Account> accToUpdate= new List<Account>;
 
for(Account a : accounts){
accMap.put(a.Id, a);
}

for (Opportunity opp : opps){
Account accounts = [select id,name,industry from account where id in pp.accountid];
  for (Account acc: accounts){
if (acc.status__c != 'Active'){
acc.staus__C = 'Active';
accToUpdate.add(acc);
}
}
}

update accToUpdate;

}

 

sfdev1sfdev1

Thanks for the code.

I tried to compile it but I'm getting this error :

ErrorError: Compile Error: unexpected token: ; at line 4 column 49

sfdev1sfdev1
I added () to the end of List<Account> accToUpdate= new List<Account>
 
but now I getting a different error