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
Austin_SteveAustin_Steve 

Cross Object Update Trigger

I am very new to salesforce, and even newer to Apex.  I'm trying to update a field on my account object, after an opportunity has been closed - won. Here is my first attempt;

 

trigger AccountTypeUpdate on Opportunity (after update) {
   
    Account myAccount=trigger.update[0];
    if (opportunity.StageName = 'A - Closed Won') {
     myAccount.type = 'Customer';
     update myAccount;
    }    //else nothing
}

 

Evidently it doesn't work, and I realize I would have to select the account associated with that opportunity, I'm just not sure how to go about doing that.  Any help would be greatly appreciated.  

Cool_DevloperCool_Devloper

Modified your code-

 

trigger AccountTypeUpdate on Opportunity (after update) {
list<Account> accounts = new List<Account>(); 

Set<Account> accSet = new Set<Account>();

Map<ID,Account> accMap = new Map<ID,Account>();

for(Opportunity opp : Trigger.new)

   {   

       if (opp.StageName = 'A - Closed Won')

      {
          accSet.add(opp.AccountId);

      }

      //    myAccount.type = 'Customer';
      //    update myAccount;
      //}  
   }

accounts = [Select ID, type from Account where ID in :accSet];

for(Opportunity opp : Trigger.new)

   {

        for(Account acc:accounts)

       {

           if(opp.AccountId == acc.Id && opp.StageName = 'A - Closed Won')

               acc..type = 'Customer';

       }

   }

if(accounts.size() > 0)

   update accounts;

}

 

Cool_D

gm_sfdc_powerdegm_sfdc_powerde
You should use AccountId from opportunity.  This should work -

Opportunity oppty = trigger.new[0]; Account acct = new Account(Id = oppty.AccountId); acct.name = 'my_acct'; update acct;

 

A word of caution.  This is just a sample code and it's not advisable to just read the first record from trigger.new. You should rather do this for all opportunities in trigger.new array as the application might invoke your trigger with multiple opportunities after mass updates.
bek123nycbek123nyc
were you ever able to get this code working? i am new to apex and am looking to do almost the exact same thing you were looking to do...if you'd be willing to share the code (if you were successful), would greatly appreciate it....
Bernie_in_TucsonBernie_in_Tucson

Hey Cool-D..,

 

I modified your code to fit my case - I'm trying to update the StageName of the opportunity to "Won" when the Contract is activated. Below is what I've taken and modified from your previously provided code - but I'm getting an error when I'm trying to save it.  I too and a novice and new to this...can you, or someone, review if possible and point out the error of my ways?

 

Bernie in Tucson

 

trigger OpportunityStageUpdate on Contract (after update) {
list<Opportunity> opportunities = new List<Opportunity>(); 

Set<Opportunity> oppSet = new Set<Opportunity>();

Map<ID,Opportunity> oppMap = new Map<ID,Opportunity>();

for(Contract cont : Trigger.new)

   {  

       if (cont.Status = 'Activated')

      {
          oppSet.add(con.OpportunityId);

      }

      //    myOpportunity.StageName = 'Won';
      //    update myOpportunity;
      //}  
   }

opportunities = [Select ID, type from Opportunity where ID in :oppSet];

for(Contract cont : Trigger.new)

   {

        for(Opportunity opp:opportunities)

       {

           if(cont.OpportunityId == opp.Id && Cont.Status = 'Activated')

               opp..StageName = 'Won';

       }

   }

if(opportunities.size() > 0)

   update opportunities;

}