[SalesForce] Recursion in Trigger and Process builder

I have a before update/insert trigger on Contact

trigger ContactTrigger on Contact (before insert, before update) {

    for(contact con:Trigger.New){
        If (con.FirstName ==con.LastName){
            con.FirstName='Default';
            con.Level__c = 'Primary';
        }
    }
}

Also, I have a process builder, on contact object that triggers on record update/insert

Condition : Level = Primary (not marking the advanced check for specific change to occur to invoke process builder)

Action:

  1. Change FirstName = LastName
  2. Add a case and assign it to this contact.

Now, I am adding a contact with First name ='test' and last Name = test'

The record gets saved as 'Default test' with one case created and assigned.

Thus order of execution : Trigger –> process builder –> Trigger

I was expecting this to get into recursion issue. But it is not. May I know the reason? I am just trying to understand the order of execution.

Best Answer

I believe the order of execution you're seeing runs like this:

  1. Trigger goes off. First name is set to 'Default' and Level to 'Primary'
  2. Process executes. First name is set to 'Test' and Case is created.
  3. Trigger is executed again. First name is set to 'Default'.
  4. Process is configured not to evaluate a record multiple times in a transaction, and hence does not execute because it has already processed this record.
  5. Transaction commits.

You do have recursion, but Process Builder's configuration is preventing it from running away and causing a limit exception.