+ Start a Discussion
Jon KeenerJon Keener 

Error with trigger trying to stop Account ownership change

I'm testing a trigger to see if I can block a user attempting to change the owner of an account.  The trigger below is as simple as I can make it, and I receive the error message shown below.  I'm assuming that the error is occurring because account ownership is changed on the view screen, and not the edit screen.  I'm assuming something similar would occur if this type of trigger were attempted on a recordtype change.
Thanks for any assistance!
Jon Keener
trigger ChangeAccountOwnerCheck on Account(before update) {
  if (Trigger.old.OwnerId != Trigger.new.OwnerId)
       Trigger.new.OwnerId.addError('Customer ownership is managed by SAP.  Please contact Master Data to make this change.');
Error Message:
An internal server error has occurred
An error has occurred while processing your request. The salesforce.com support team has been notified of the problem. If you believe you have additional information that may be of help in reproducing or correcting the error, please contact support@salesforce.com. Please indicate the URL of the page you were requesting, any error id shown on this page as well as any other related information. We apologize for the inconvenience.

Thank you again for your patience and assistance. And thanks for using Salesforce!

Error ID: 1788794875-117

Click here to return to the previous page.
Ron HessRon Hess
Here you go

this is working for me

trigger ChangeAccountOwnerCheck  on Account bulk (before update) {
  if (Trigger.old[0].OwnerId != Trigger.new[0].OwnerId)
       Trigger.new[0].OwnerId.addError('Customer ownership is managed by SAP.  Please contact Master Data to make this change.');



I used the same code for checking it on opportunities but it gives the following error.

System.DmlException: Update failed. First exception on row 0 with id 006T0000002msNlIAI; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, You cannot change the record owner: [OwnerId]

I deactivated all validation rules on opportunity but still i get this one.

Thanks in advance for any help.