I've got a Before Update trigger that attempts to assign a new Owner on Accounts and I'm trying to handle the potential exception where the new Owner is inactive. I can cause the error in testing (An operation was performed with an inactive user.) but the logs do not reveal the error and its exception type.

I don't want to use an extra query to check Users active status but if there's no exception that can be trapped, then that's the only choice. Any ideas on how to catch that exception?

  • I did find a reasonable hack to avoid the SOQL query by adding a checkbox formula field to the Account that provides the Active status of the potential new Owner. This can be reference in a trigger without an extra query. – AlwaysThinkin Jan 21 '15 at 18:57
  • are you doing this in a before trigger? If so, setting the ownerId to a member of Trigger.new won't throw the error until after the trigger concludes and only the user (or any apex that did a dml on the triggered object) will find out – cropredy Jan 21 '15 at 18:58
  • In Spring '15, you will be able to update records owned by inactive Users - so maybe you could wait a month? – user735 Jan 21 '15 at 18:59
  • @crop1645 I suspected that populating Owner in before (with an sObject put) might be the problem with trapping the error since there's no actual DML. Where does one insert try-catch for before events then? – AlwaysThinkin Jan 22 '15 at 21:50
  • You can't do try-catch on the implicit update/insert done by before triggers. You have to test for error conditions yourself and do addError(..) on the trigger list member – cropredy Jan 22 '15 at 22:21

Summarizing the comment thread

  1. You can't catch DML exceptions on the implicit insert/update performed on the SObject(s) used in Trigger.new. Only the "caller" of the trigger will see the error (that is, some other class/trigger doing explicit Apex DML, the user on the UI, or Dataloader/Api application)
  2. Hence, you need to write code in the trigger (or related handler class) that tests for the inactive user and uses addError(...) on the related Trigger.new record.
|improve this answer|||||

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.