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

Why does this trigger not work?

The following trigger,  and an alternative suggested on this board, do not work as planned because the IF always evaluates to FALSE, even when the account manager field is non-blank.


Original Failing Trigger

trigger OpportunityTrigger on Opportunity (before ​insert) {

  for(Opportunity o : {
    String accountManager = (String)o.Q2_Account_M​gr__c;
    if((accountManager != null && accountManager.l​ength() >= 15)) {      
      o.OwnerId = o.Q2_Account_Mgr__c;  
    } } }


Suggested Alternative

for (Opportunity o : trigger.New) {

if (o.Q2_Account_Mgr__c != null) {

o.OwnerId = o.Q2_Account_Mgr__c;


Related Info

  • field Q2_Account_Mgr__c is a lookup field on the Account record that is related to a User
  • Irrespective of whether this field conatins data or is empty, the IFs in either trigger always evaluate to FALSE

Is the problem that since this trigger runs on the Opportunity that it can't see a lookup field on the Account?  Or ??




If Q2_Account_Mgr__c is a formula field then in the before trigger it will be always null so your condition always evaluates to false


How can I fix?


Create another field and a workflow to update the newly created field whenever the formula field updates > Copy the value of the formula field.


If you want this to assign even before opportunity creation, you can always write a query to access that field from account.