Reply
Regular Visitor
transflux
Posts: 1
0

Incrementing a counter on a parent record with workflow rule field updates involved

I'm running into an issue with the order of execution around triggers and workflow rules.

 

I have a custom field on the Contract object called Parent Contract.

I also have a custom field called Number of Active Child Contracts on the Contract object.
I have an after insert/after update trigger that increments the Active Child Contracts when the child contract is activated and then decrements the counter when the child contract is deactivated.

There are also workflow rules on the Contract object that perform some field updates.

 

Due to the order of execution of triggers and workflow rules, the trigger is increasing the number by two when the child contract is activated. It decrements correctly when the child contract is deactivated.

 

Has anyone come across a solution to prevent the trigger from updating the parent the second time around on the activation?

If this were an update to the child contract, I could use the trigger.old value for my base to increment, but since it is another record, I don't see a clear way to do it.

 

The other idea I have is to move the workflow rule field updates into my trigger (since it is a master trigger for all contract events), but that would require any future field updates to also be added into the trigger (not good as it will require developer skills, not admin skills in the support team).

 

Here's the code snippet for reference:

 

    for (Contract newContract : Trigger.New) {
        boolean isInsert = trigger.IsInsert;
        boolean isStatusChange = trigger.isUpdate && trigger.oldMap.get(newContract.Id).Status != newContract.Status;

        //this if block does the check to build the list of parent contracts to update for number of active child contracts
        if (newContract.Parent_Contract__c != null) {

            parentContractList = [SELECT Id,Number_of_Active_Child_Contracts__c FROM Contract WHERE Id =:newContract.Parent_Contract__c];

 

            for (Contract ParentContract : parentContractList){

               // if the child contract has been activated, increment the parent contract's counter field
                if (newContract.Status == 'Activated' && isStatusChange){
                    // if the counter field isn't populated, set it to one to avoid errors
                    if (ParentContract.Number_of_Active_Child_Contracts__c == null) {
                        ParentContract.Number_of_Active_Child_Contracts__c = 1;
                    // otherwise, increment the counter by one
                    } else {
                        ParentContract.Number_of_Active_Child_Contracts__c ++;
                    }                        
                    parentContractsToUpdate.add(ParentContract);
                // else if the contract is now not activated, decrement the parent contract's counter field
                } else if (newContract.Status != 'Activated' && isStatusChange) {
                    // if the counter field isn't populated, set it to zero now
                    if (ParentContract.Number_of_Active_Child_Contracts__c == null) {
                        ParentContract.Number_of_Active_Child_Contracts__c = 0;
                    // otherwise, decrement the counter by one
                    } else {
                        ParentContract.Number_of_Active_Child_Contracts__c --;
                    }
                    parentContractsToUpdate.add(ParentContract);
                }
            }

        }        

.... rest of trigger code