I have a requirement where I have the custom field 'Account Owner' on Account object and I have the same field on Child Object Opportunity.When I change the Account Owner on Account then it should update the Account Owner on all Child Records.

For this I have written a Trigger(After Update) on Account Object.

Private void UpdateAccountOwnerOnOpportunity(Map<Id, SObject> newItems, Map<Id, SObject> oldItems) {

        Map<Id,Opportunity> optMap=new Map<Id,Opportunity>();
        Map<Id,Account> actMap=new Map<Id,Account>();
        for(SObject newObj :newItems.values()){
            Account newAccountObj=(Account)newObj;  
            Account oldAccountObj = (Account)oldItems.get(newObj.Id);
            if(newAccountObj.Sales_Person__c!=null && (newAccountObj.Sales_Person__c != oldAccountObj.Sales_Person__c)){
            for(Opportunity opt:[Select Id,AccountId,Sales_Person__c from Opportunity where AccountId IN:actMap.keySet()]){
                Account ac  = actMap.get(opt.AccountId);
            update optMap.values();

The above code is working as expected. but fails in below scenario.

When we designed the application,we had migrated some Old Opportunities records from Legacy Environments to our New Org by Bypassing all Validations and Alomost all of the old Legacy Records are at 'Closed' Stage.All these Opporunities Refer to certain Account.

So If I update Account Owner Field on Account which has Legacy Opportunity Records then I face FIELD_CUSTOM_VALIDATION_EXCEPTION error message on screen and which is oblivious as we have now enabled all validation rule .

My Expectation is Even If I have such existing records in my Org & If I change Owner then it must change Account Owner on all existing Opportunity Records.

I am looking for Best Apprach of how to bypass validations for Opportunity Records when I update the Update from Account exiting.

Can someone please suggest what needs to be done when we deal with Legacy Org Records?

1 Answer 1


You'll need to make changes to your validation rules to manage this. There's nothing you can do in Apex directly.

The simplest approach that I see is to include an ISCHANGED(OwnerId) check within your Validation Rules and simply prevent them from going off upon a change in ownership. In most cases you would want to AND() this with your existing formula.

A user-based exclusion will not work, because the trigger is always running in the context of the user who updated the Account - which could be anyone.

The other options would be:

  • Do not update legacy Opportunities in your trigger.
  • Change your validation rule to not run on legacy Opportunities.
  • Update your legacy Opportunities so that they pass validation.
  • So you are saying, add the NOT(ISCHANGED(OwnerId)) in all Validation Rule?
    – SFDCDJ
    Mar 9, 2018 at 13:16
  • Yep. ANDed to the existing formula.
    – David Reed
    Mar 9, 2018 at 13:18
  • Currently we have a checkbox 'ByPassValidation' on User object and we have added the checkbox to all validation rule of opportunity.so when we want to update Opportunity,we set the checkbox value to true and update the records. Can I use this checkbox somewhere in my trigger to bypass validations.
    – SFDCDJ
    Mar 9, 2018 at 13:19
  • No. Your trigger is running in the context of the user who updated the Account.
    – David Reed
    Mar 9, 2018 at 13:20
  • Your suggestion is useful but this requires a lot of rework like, update all validation rule & then find all formula and update the same.
    – SFDCDJ
    Mar 9, 2018 at 13:22

You must log in to answer this question.

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