[SalesForce] How to make triggers not fire when we use API tools like Data Loader

I have written and deployed a Contact Trigger in our production environment, and whenever I would have to use Data Loader to mass update contact records, I would always have to go into the code and add myself in an if statement (using UserInfo.getName()), stating that only run the code if the current user's full name is not myself) to avoid triggers from firing when I mass update. Then I can proceed to use Data Loader.

Sample:

if((trigger.isUpdate ||trigger.isInsert) && UserInfo.getName() != 'Mark Liu'){

    for (Contact c1: trigger.new){

        ***Rest of Codes here....***

Is there a method similar to UserInfo.getName() that the system can tell when an operation is using API tools like Data Loader? If there is, I could perhaps use that in my if statement to avoid firing this Contact Trigger when an update/insert is being performed from the Data Loader. Any help is much appreciated!

Best Answer

The name method is not a good way of going about this. For one, it's a pain to change if others need to use the same functionality, or if you do want to fire triggers.

If you did want to make a "No triggers" flag, I would make it a custom checkbox field, then mark it as TRUE on your file before uploading via the data loader. If your trigger fires "Before" then you can just uncheck the box after skipping the rest of the code. If there is "After" code on the trigger then you will need to create a static variable to hold the NoTriggers boolean, and then use that to skip the "After" logic as well.

You could also create a custom setting that could be checked and unchecked when triggers should be skipped or not. In your trigger, only skip the code if the custom setting field is checked, and a System Administrator (or whatever your profile is) is logged in. Profile is more durable to use here than user name.

Related Topic