Discussions
- General Development
- Schema Development
- Apex Code Development
- Visualforce Development
- Formulas & Validation Rules
- Security
- Mobile
- Force.com Sites & Site.com
- Chatter Development
- Java Development
- .NET Development
- Perl, PHP, Python & Ruby
- Desktop Integration
- APIs and Integrations
- Visual Workflow
- Apple, Mac and OS X
- VB and Office Development
- AppExchange Directory & Packaging
- Salesforce Labs & Open Source Projects
- Other Salesforce Applications
- Jobs Board
- Force.com Discussion Boards
- :
- Developer Boards for Force.com and Database.com
- :
- Apex Code Development
- :
- Incrementing a counter on a parent record with wor...
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic to the Top
- Bookmark
- Subscribe
- Printer Friendly Page
Incrementi ng a counter on a parent record with workflow rule field updates involved
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
12-09-2012 02:38 PM
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__
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__
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

