1

I have a trigger on the Opportunity object that runs after an opportunity is deleted, inserted, undeleted, or updated, or before one is deleted, inserted, or updated. We use this to start a process on the Status of an Opportunity/Contract.

  • If opportunity is "In Progress", then the account type is "Prospect". 2 Weeks later, if this is still "In Progress", the account type is reverted to the previous account type (either "Lead' or "Inactive Client")
  • If opportunity is "Won", then the account type is "Active Client"
  • If opportunity is "Lost", then account type is either "Lead" or "Inactive Client"

Recently, while merging some accounts, I noticed that a couple of accounts that I had merged were reverting to their previous account type 2 weeks after the day that I had merged them. Since both accounts had opportunities merged into the master account and based on the time that the type was reverted, I was able to tell that the Opportunity_ContractTrigger was being fired when I was merging.

Has anyone had a similar issue to this when merging accounts, and what do you recommend I change in the Trigger so that it doesn't begin running when I merge accounts with opps? I reviewed this post (Account Merge Trigger Execution Order) which says that "child records that are reparented as a result of the merge operation do not fire triggers, but unfortunately my trigger related to Opportunity is being fired.

Below is the code for trigger. Please let me know if you have any other questions. Thank you.

Opportunity_ContractTrigger

trigger Opportunity_ContractTrigger on Opportunity__c (after delete, after insert, after undelete, after update, before delete, before insert, before update) {

    if(trigger.isInsert && trigger.isBefore) {
      Opportunity_ContractTriggerHandler.OnBeforeInsert(trigger.new);
      
    } else if(trigger.isInsert && trigger.isAfter) {
        Opportunity_ContractTriggerHandler.OnAfterInsert(trigger.newMap);
        
    } else if(trigger.isUpdate && trigger.isBefore) {
        Opportunity_ContractTriggerHandler.OnBeforeUpdate(trigger.oldMap, trigger.newMap);
        
    } else if(trigger.isUpdate && trigger.isAfter) {
        Opportunity_ContractTriggerHandler.OnAfterUpdate(trigger.oldMap, trigger.newMap);
        
    } else if(trigger.isDelete && trigger.isBefore) {
        Opportunity_ContractTriggerHandler.OnBeforeDelete(trigger.oldMap);
        
    } else if(trigger.isDelete && trigger.isAfter) { 
        Opportunity_ContractTriggerHandler.OnAfterDelete(trigger.oldMap);

    } else if(trigger.isUnDelete) {
        Opportunity_ContractTriggerHandler.OnUndelete(trigger.new);
    }
}
6
  • does your account trigger have any updates on Opportunity? Commented Aug 16, 2021 at 19:28
  • @KrisGoncalves The account trigger does not have any updates on Opportunity, but the Opportunity trigger can update fields in the account, such as the Account Type example mentioned above. When fired, the classes called by the account trigger just affect fields on the account level, including the Account Type field mentioned above. Commented Aug 17, 2021 at 14:42
  • what exactly is handling the 2 weeks later logic? A scheduled job, time-based workflow, etc. Commented Aug 18, 2021 at 17:43
  • @KrisGoncalves we have a scheduled job that runs daily. If the opp's LastModifiedDate was 2 weeks prior, the status of the opp is "in progress", and the account type is not Active Client, then the Account Type is reverted to the previous account type. Commented Aug 18, 2021 at 20:51
  • i'm unclear on how you're certain the trigger is at fault (I haven't run tests, but a simple system.debug and quick merge test should clarify this). After merging, what is the "type" of the account? Would it not meet criteria to "revert" as when the job runs in 2 weeks there's now an opportunity that has been updated 2 weeks ago during the merge (re-parented)? Even though the child objects' trigger doesn't run, the last modified date is still changed. Commented Aug 18, 2021 at 20:55

1 Answer 1

2

To clarify, your opportunity trigger is not running as the documentation is correct - child triggers do not run.

A single merge operation fires a single update event for the winning record only. Any child records that are reparented as a result of the merge operation do not fire triggers.

If you were to add a System.debug('opp trigger running'); into your opportunity trigger and then merge accounts, you'll see no debug statement in your log (tested in a clean slate dev org). If you do, that's indicative of some opportunity update you are doing within your account automation.

What you left out is the important piece to your issue - you are using the LastModifiedDate of opportunity in a scheduled job as the key to your logic.

When an account merges, the LastModifiedDate of the opportunity does get updated due to the account change (even though the triggers are not run).

As such, you need to re-evaluate that design. Without specifics, I'd probably recommend not relying on LastModifiedDate. Any little change (fixing name, etc) will push your 2 weeks back but is that what you'd like to occur? It sounds like you care about tracking 2 weeks from when an Opportunity is set as In Progress.

As such, you could look at a couple options and I would recommend #2:

  1. Create a custom date field and essentially stamp this when the opportunity is created or updated to that stage. This allows you to have a Opportunity In Progress Date to utilize in your scheduled job that won't get polluted with any unrelated changes.
  2. Go declarative and rely on existing time-dependent functionality - look at Flows that have the ability to do scheduled paths. Within your record-triggered flow for Opportunity, you could "schedule" your account update to occur in 2 weeks on opportunity entering that desired stage and if that criteria is still met 2 weeks later, then the account will be updated. If the user changes the stage, then the account update will not run.

You must log in to answer this question.

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