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
bek123nycbek123nyc 

Apex Trigger Assistance

i've created below trigger to update a specific Account field whenever an Opportunity goes to 90%. 

While the trigger seems to be firing, the update to the Account is not taking place.

 

Snippet from debug log also provided at bottom...

 

 

 thanks for any help you can provide...

 

 

 

 ==================

 

trigger AddToParatureAt90 on Opportunity (after update) {
  List<id> acctIds = new List<id>();
  for(Opportunity opp: Trigger.new) {
  
  
    if (opp.StageName == '90-Closed-Signed') 
            {acctIds.add(opp.AccountId);}
         }
 
    List<account> accounts = [SELECT a.Id FROM Account a WHERE a.Id IN :acctIds];

    for (Account a: accounts) {a.AddParatureAccount__c ='Yes-Add' ;}
    
    update accounts;
  }

===============

Debug Log Snippet

05:20:58.658|CODE_UNIT_STARTED|[EXTERNAL]OpportunityAcctPhoneNumber on Opportunity trigger event AfterUpdate for 006T00000053bhx
05:20:58.658|SOQL_EXECUTE_BEGIN|[10,30]|Aggregations:0|SELECT a.Id FROM Account a
WHERE a.Id IN :acctIds
05:20:58.661|SOQL_EXECUTE_END|[10,30]|Rows:0|Duration:3

 

BritishBoyinDCBritishBoyinDC
Try adding AddParatureAccount__c into the Account Select clause...
bek123nycbek123nyc

thanks....i updated code per your idea....below, still not working....

i'm thinking that for some reason, the "acctids" list is not containing any results, but can't yet say for sure that's what it is or how to fix it....thanks for any other help you can provide...

 

 

trigger AddToParatureAt90 on Opportunity (after update) 

{
 
  List<id> acctIds = new List<id>();
 
  for(Opportunity opp: Trigger.new) 
  
  
       if (opp.StageName == '90-Closed-Signed')    {acctIds.add(opp.AccountId);}
         
  
    List<account> accounts = [SELECT a.Id,a.AddParatureAccount__c FROM Account a WHERE a.Id IN :acctIds];

    for (Account a: accounts) {a.AddParatureAccount__c ='Yes-Add' ;}
     
    update accounts; 
  }
BritishBoyinDCBritishBoyinDC

Hmm,

 

Try adding some debug statements shown below, and then enable the debug log for your user, try again and see what the log says...

 

 

trigger AddToParatureAt90 on Opportunity (after update) { List<id> acctIds = new List<id>(); for(Opportunity opp: Trigger.new) if (opp.StageName == '90-Closed-Signed') {acctIds.add(opp.AccountId);} system.debug('Check Ids added:' + acctIds.size()); List<account> accounts = [SELECT a.Id,a.AddParatureAccount__c FROM Account a WHERE a.Id IN :acctIds]; system.debug('Check How Many Accounts retrieved:' + accounts.size()); for (Account a: accounts) {a.AddParatureAccount__c ='Yes-Add' ;} update accounts; }

 

 

 

bek123nycbek123nyc

great suggestion....just updated, and here's what i got in the log...

 


15:33:20.456|METHOD_ENTRY|[12,5]|system.debug(String)
15:33:20.456|METHOD_ENTRY|[12,39]|LIST:Id.size()
15:33:20.456|METHOD_EXIT|[12,39]|size()
15:33:20.456|USER_DEBUG|[12,5]|DEBUG|Check Ids added:0
15:33:20.456|METHOD_EXIT|[12,5]|debug(ANY)
15:33:20.456|SOQL_EXECUTE_BEGIN|[14,30]|Aggregations:0|SELECT a.Id,a.AddParatureAccount__c FROM Account a WHERE a.Id IN :acctIds
15:33:20.459|SOQL_EXECUTE_END|[14,30]|Rows:0|Duration:2
15:33:20.459|METHOD_ENTRY|[16,3]|system.debug(String)
15:33:20.459|METHOD_ENTRY|[16,55]|LIST:SOBJECT:Account.size()
15:33:20.459|METHOD_EXIT|[16,55]|size()
15:33:20.459|USER_DEBUG|[16,3]|DEBUG|Check How Many Accounts retrieved:0
15:33:20.459|METHOD_EXIT|[16,3]|debug(ANY)
15:33:20.459|CUMULATIVE_LIMIT_USAGE
15:33:20.459|LIMIT_USAGE_FOR_NS|(default)|

 

BritishBoyinDCBritishBoyinDC

Looks like your suspiciions were correct - so I'd do it again with a new degug statement to show you what the stage actually is...

 

 


for(Opportunity opp: Trigger.new)

system.debug('Stage Name: ' + opp.StageName);

if (opp.StageName == '90-Closed-Signed') {acctIds.add(opp.AccountId);}

 

 

 

bek123nycbek123nyc

interesting, when i add the debug, i now get a compile error...

 

 Error: Compile Error: Variable does not exist: opp.AccountId at line 13 column 22

 

code below...

 

i admit, i am new to using debug, but would've thought those could be plugged in pretty much anywhere....any ideas? 

btw, thank you for all your help here

 


 

trigger AddToParatureAt90 on Opportunity (after update)

{
 
  List<id> acctIds = new List<id>();
 
  for(Opportunity opp: Trigger.new)
 

system.debug('Stage Name: ' + opp.StageName);
 
       if (opp.StageName == '90-Closed-Signed')  
        {acctIds.add(opp.AccountId);}
        
    system.debug('Check Ids added:' + acctIds.size());
   
    List<account> accounts = [SELECT a.Id,a.AddParatureAccount__c FROM Account a WHERE a.Id IN :acctIds];

  system.debug('Check How Many Accounts retrieved:' + accounts.size());
 
    for (Account a: accounts) {a.AddParatureAccount__c ='Yes-Add' ;}
    
    update accounts;
  }

dmchengdmcheng

I don't know what's causing this error, but try removing the "a" alias from the SOQL statement since you don't need it, and comment out the debug line you just added.

 

If you still get no results, then comment out all the lines in the for loop except for the StageName debug line just so you confirm that the StageName values are what you expect, then uncomment you code line-by-line to narrow things down.

bek123nycbek123nyc

thanks for all the support here...the final working trigger is included below...

 

 

trigger AddToParatureAt90 on Opportunity (after update)

{
 
  List<id> acctIds = new List<id>();
 
  for(Opportunity opp: Trigger.new)
 
//system.debug('Stage Name: ' + opp.StageName);
 
      if (opp.StageName == '090-Closed-Signed')  
        {acctIds.add(opp.AccountId);}
        
    system.debug('Check Ids added:' + acctIds.size());

   
     List<account> accounts = [SELECT Id,AddParatureAccount__c FROM Account WHERE Id IN :acctIds];

   system.debug('Check How Many Accounts retrieved:' + accounts.size());
 
    for (Account a: accounts) {a.AddParatureAccount__c ='Yes-Add' ;}
    
     update accounts;
  }