0

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)){
                actMap.put(newAccountObj.Id,newAccountObj);
            }
        }
        if(!actMap.isEmpty()){
            for(Opportunity opt:[Select Id,AccountId,Sales_Person__c from Opportunity where AccountId IN:actMap.keySet()]){
                Account ac  = actMap.get(opt.AccountId);
                opt.Sales_Person__c=ac.Sales_Person__c;
                optMap.put(opt.Id,Opt);
            }
            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

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.
7
  • So you are saying, add the NOT(ISCHANGED(OwnerId)) in all Validation Rule? – Dhananjay Patil Mar 9 '18 at 13:16
  • Yep. ANDed to the existing formula. – David Reed Mar 9 '18 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. – Dhananjay Patil Mar 9 '18 at 13:19
  • No. Your trigger is running in the context of the user who updated the Account. – David Reed Mar 9 '18 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. – Dhananjay Patil Mar 9 '18 at 13:22

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.