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!

  • 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? – Renato Oliveira Jul 2 '18 at 12:11
  • Correct @RenatoOliveira – Sohan Shirodkar Jul 2 '18 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. – Renato Oliveira Jul 2 '18 at 16:43
  • @RenatoOliveira This is what I have in my mind as the last resort if I din't get any solution. – Sohan Shirodkar Jul 3 '18 at 5:17
  • I'll post that as an answer then, in case you really don't find another way. – Renato Oliveira Jul 3 '18 at 13:52
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.