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
Matt Higel 13Matt Higel 13 

After Insert trigger does not fire

I have a debugged my trigger down to the barest code, and have some strange behavior that I need help to get sorted.

When I create a new Contact using the following trigger:
trigger ContactTrigger on Contact (before insert, after insert) {
	System.debug('1');
	if(Trigger.isBefore) {
	        System.debug('2');
		if(Trigger.isInsert) {
			System.debug(3');
		}
	}
	if(Trigger.isAfter) {
		System.debug('4');
		if(Trigger.isInsert) {
			System.debug('5');
		}
	}
	System.debug('6');
}
I would expect to see a debug statement for each number from 1-6.  However, numbers '4' and '5' do not print, which seems to indicate that the After Insert event is not firing for the Contact object.

If I remove the Before Insert event and create a Contact:
trigger ContactTrigger on Contact (after insert) {
	System.debug('1');
	if(Trigger.isAfter) {
		System.debug('2');
		if(Trigger.isInsert) {
			System.debug('3');
		}
	}
	System.debug('4');
}
Then numbers from 1-4 are correctly output to the debug log (i.e. the After Insert event is firing).

There are no compilation errors, and no errors in the debug logs.  In either version of the trigger, a Contact record is created.  I can validate that After Insert behavior on the Contact object does execute as expected in other sandboxes for my org.

If there's an issue with the above code that would explain the behavior, then I'm missing it.
Nachu RY 4Nachu RY 4
Hi Matt,

The above first code is working fine

.User-added image
correct the  Line 6  : instead of System.debug(3'); try System.debug('3');

Thanks
goabhigogoabhigo
I copied and pasted your code and found out that it prints 1, 2, 3, 6 in one transaction/event and 1,4, 5, 6 in another. Check the screenshot below:
User-added image

Note that I have used ### inside the debug log, hence ### 1, ### 2, etc.

You too should get the similar logs. Is there any other trigger on the contact object? Did you check other debug logs that would have generated at the same time?

--
Regards,
Abhi.
Matt Higel 13Matt Higel 13
Abhi,

I agree that I should get similar logs, but I don't.  The second set of debug statements (i.e. 1,4, 5, 6) never prints as long as a Before Insert event is also managed.

There are a number of additional triggers on the Contact object, but all of them are from managed packages, and none of them are new to this sandbox or org.  

At this point I can only guess that there is a system issue that I need SFDC Support to help resolve.

Thanks!


Matt