1

I have a before update trigger on User object which checks for certain User fields. If that field matches a condition I am supposed to skip the update of that user record, else make some field updates on that record. I achieved this using addError() method.

newUser.addError('Skipping non-company employees');

When I use data loader to update multiple records, the before update trigger is executing multiple times. For example, if I have 4 records in my CSV file with one record to be skipped and 3 different email updates to be made on other 3 records, the individuals with the updated email IDs are getting 3 emails each for verifying the email ID change. This means my before update trigger is fired multiple times. When I do not have any records to be skipped with addError(), this the users get only one verification email, as expected.

This is very strange. I have referred this post, but the explanation given should not make field updates multiple times as per my knowledge.

Any help would be appreciated!

5
  • So you are trying to import a sheet which contains user data, and when the user is not on the company, you don't do whatever you are doing on the trigger? That's the logic? Jul 2, 2018 at 12:11
  • Correct @RenatoOliveira Jul 2, 2018 at 14:10
  • 1
    Have you considered just not using addError at all? I mean, just bypass the users you don't need to run your code on. Since you are running on before update you can easily copy the changed fields from the existing record on the database, which is available on Trigger.old as a list and Trigger.oldMap as a map of Id to User. Jul 2, 2018 at 16:43
  • @RenatoOliveira This is what I have in my mind as the last resort if I din't get any solution. Jul 3, 2018 at 5:17
  • I'll post that as an answer then, in case you really don't find another way. Jul 3, 2018 at 13:52

2 Answers 2

0

You can consider not using addError at all. Just bypass the users you don't need to run your code on: since you are running a trigger on before update you can easily copy the changed fields form the existing record on the database, which are available on Trigger.old and Trigger.oldMap as both list and map of Id to User.

0

If you make updates to a batch of records through the API or using apex with allOrNone set to false, and a trigger uses addError on some of the records within a batch, Salesforce will rollback the changes that occurred within the trigger, and then call the trigger again with the records that did not have errors.

Note that if the same trigger operates in both a before and after context, and addError is used in the before context, the trigger will be called with the after context on the non-error records before rolling back, and then calling trigger again (in both the before and after contexts).

You must log in to answer this question.

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