2

I have a fresh dev org, with nothing at all in it other than this one trigger that is below. When I create a new lead, this trigger is ran. (ie, I put the new leads name as John Smith and Company as ABC Co). When I click save, the record shows with hello world as the first and last name on the new lead. I do not understand what would cause the before update trigger to run on insert. This only happens in lightning, not in classic. I am in Version 41.0 but also tried it in 40.0 and 34.0 and I get the same results. Any insight would be greatly appreciated. Thank you (if this is a dupe, sorry...I could not find an answer to this).

trigger LeadTrig on Lead (before update) {
    for(Lead l : trigger.new){
        l.firstName = 'hello';
        l.lastName = 'world';
    }
}
3
  • Are you populating the address? We're currently experiencing something similar in our production org related to Accounts. We are building a new process where Accounts are being created with the Shipping Address populated, whereas before it never was. We realized that a Salesforce automated process which updates the Geolocation on the Address is causing our update triggers to fire.
    – Aaron P.
    Dec 21, 2017 at 17:28
  • I am not populating an address. In the New Lead modal I am only filling out Last Name and Company as they are the only two required fields and something is triggering the update trigger. The only code in my dev org is the above code snippet and I have made no rules, workflows, or anything. Just spun up the dev org and put that code in and it is hitting update. Must have some out of the box config stuff on lead in lightning.
    – bemon
    Dec 21, 2017 at 17:36
  • Try cranking your debug levels all the way up (especially PROFILING). You may have to comb through line by line but should eventually find the issue.
    – Adrian Larson
    Dec 21, 2017 at 18:20

2 Answers 2

2

Lead update apex trigger fires on new lead creation in Lightning Experience https://help.salesforce.com/articleView?id=000271134&language=en_US&type=1

Salesforce currently has this documented as working by design.

Lead update apex trigger fires on new lead creation in Lightning Experience
Knowledge Article Number    000271134
Description 

In Lightning Experience, Lead update apex trigger fires on new lead creation from UI. This behavior is not observed in Classic.

This scenario can be reproduced by following the below steps.       
Create an apex trigger on Lead  (Sample shown below)                  
                                 trigger testleadupdateLEX on Lead (before update) {
                                 System.debug('before update');
                                  }
Set the debug logs to observe the behavior and create a new lead.
Observe Lead update trigger getting fired when a new lead is created  from Lightning Experience.

Note: Lead update trigger does not get fired when a new lead is created from Classic.




Resolution  

This is currently working as designed and this behavior is expected while creating a new lead in Lightning experience.The lead creation process is doing an implicit update behind the scenes causing the trigger to execute.
2
  • Thank you, I should have posted that I found the answer awhile back but I did not post it. Marking as best answer as I forgot all about this and when I read my question I could not remember my findings from before. So thank you for helping me remember the things I have forgot.
    – bemon
    Mar 19, 2019 at 22:40
  • 1
    I run into this issue while doing David Liu's Pluralsight APEX Academy video's. Hope that it will be useful for others.
    – horzel_evh
    Mar 21, 2019 at 5:50
5

A typical reason for this is that you have Lead Assignment rules. Once those execute, a new owner is assigned and hence the Lead object is updated. You can see this in the debug log if you enable Workflow INFO and Apex Code DEBUG

Or, you may have workflow rules or Process Builder rules that do updates on the Lead. The object will have changed and per SFDC convention outlined in Trigger and Order of Execution, the triggers will re-execute

2
  • Do those Lead Assignment rules come out of the box for a new dev org? In my dev org, I have not made any rules, processes, workflows, etc. The only thing I have done is make the one before update trigger.
    – bemon
    Dec 21, 2017 at 17:39
  • @bemon - OOTB there are no Assignment Rules. So, you'll need to look at Debug Log to see what is happening after the record insertion
    – cropredy
    Dec 21, 2017 at 18:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .