0

I created a brand new dev org and created a simple Lead trigger that fires on afterInsert and afterUpdate events.

trigger LeadTrigger on Lead (after insert, after update) {
    
    if(Trigger.isAfter){
        if(Trigger.isInsert){
            System.debug('Lead trigger after insert');
        } else if(Trigger.isUpdate){
            System.debug('Lead trigger after update');
        }
    }
}

When I am creating a new Lead record afterUpdate trigger fires immediately after afterInsert trigger.

enter image description here

enter image description here

enter image description here

There are no validation rules or custom formula fields on the object. It's a newly created org.

I want to send an email to the user when the trigger fires. So after lead creation, the user gets TWO emails. But that is not a big problem, it's just interesting, what caused the update.

What can cause this trigger, and how can I change it?

6
  • 2
    What's the problem with it firing...
    – Girbot
    Commented Oct 25, 2021 at 9:47
  • @Girbot I want to send an email to the user when the trigger fires. So after lead creation, the user gets TWO emails. But that is not a big problem, it's just interesting, what caused the update.
    – senoron
    Commented Oct 25, 2021 at 9:50
  • That would be good information to add to your question, plus the relevant code.
    – Girbot
    Commented Oct 25, 2021 at 9:52
  • @Girbot Yes, thanks, but there are no resons for adding code for email sending.
    – senoron
    Commented Oct 25, 2021 at 9:55
  • Do you have any Flows, Process Builders, Workflow Rules, or any additional triggers? How about any installed packages that may be updating the lead on insert? There's a lot of things you need to check.
    – sfdcfox
    Commented Oct 25, 2021 at 12:15

1 Answer 1

0

I faced the same when I tested out some race conditions for Lead triggers/assignment rules/record triggered flow in a brand new empty scratch org.

What I can tell from my debugging is that when you manually create a Lead record in LEX, it lands you on to newly created Lead detail page. Salesforce triggers an update event for this Lead once again to update IsUnreadByOwner field on the record. I suppose this is a result of you, as the owner, viewing the record. I'm absolutely sure that the second update doesn't get contained in the original insert transaction and goes through a separate transaction (2 debug logs produced for both)

Field Ref: https://developer.salesforce.com/docs/atlas.en-us.object_reference.meta/object_reference/sforce_api_objects_lead.htm)

You must log in to answer this question.

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