5

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!

3 Answers 3

8

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.

6
  • Thanks, Matt. This is a brilliant idea. This will allow each user in our org to toggle between skipping triggers or applying triggers to perform necessary business logic when they are in the org. Commented Mar 5, 2015 at 22:33
  • I'd second the usage of the hierarchy custom setting that is checked in the triggers. This way you can disable the trigger just for the individual user that is doing the data loading. Commented Mar 6, 2015 at 0:01
  • Hi Matt - This doesn't happen all the time... But we have another admin that uses Dataloader that would need to bypass the triggers to mass update/upload records into Salesforce most of the time. Could you please elaborate/give examples on how I can build this out using a static variable? And I assume that the "NoTriggers" boolean will be a custom field created for the User Object? My Contact Trigger has Before Delete and After Update and After Insert. Commented Mar 6, 2015 at 1:15
  • Actually... Could someone please show me or provide me with some info on how to use Custom Settings? I'm trying to create a hierarchy custom setting for myself to test it out, but the custom boolean field that I have created is not showing up on my User record. How am I supposed to turn it on and off? And how can I reference it in apex code? Commented Mar 6, 2015 at 1:17
  • Can you create a new question for this issue? It's a different question and no one's going to see it buried in the comments of a resolved question. Thanks! Commented Mar 6, 2015 at 15:14
0

My understand here is why would you want to disable or skip execution of trigger. Triggers contain important business logic and may deeply impact the system functionality if skipped.

If there is some issue with Dataloader loading too many records or too many SOQL , try refining your trigger to make sure it is bulkified.

Otherwise other option is reducing the batch size when loading data via data loader

Dataloader configuration

3
  • Hi Kaw - my code is bulkified, and I understand that I can configure the API tools to have them load one record at a time... I have asked other people in my org who uses Data Loader to configure it that way, but they said loading record one by one is way too slow. Commented Mar 5, 2015 at 22:30
  • Its a choice you need to make between time and functionality. I would suggest strongly not to skip triggers as it can lead to data inconsitency at various levels and once data has gone bad it cannot be updated later. I suggest you set a size which is not very less nor very high. salesforce provides an option of bulk API for faster processing use that, but dont skip triggers.
    – Kaw Sumit
    Commented Mar 5, 2015 at 22:32
  • Kaw is right that you should not be skipping code that sets values or updates related objects unless you are being very careful. When I have skipped triggers in the past, it's been because I had some older data that did not conform to the current business rules, and just needed to be loaded for the purposes of completeness. This shouldn't be something you are doing a majority of the time. If it is, I would suggest carving out exceptions in the trigger logic itself e.g. don't create a related record/update a field if XX condition is not met. Commented Mar 5, 2015 at 22:52
0

The best way to handle this is to create a custom setting, name it background job and then enter a login account like [email protected]. Then in your trigger code check if the user executing matches the custom setting background job user.

1
  • You can just use Hierarchy Custom Settings to specify users, or Custom Permissions.
    – Adrian Larson
    Commented Jan 17, 2017 at 18:45

You must log in to answer this question.

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