0

I have ParentObject__c that has ChildObject__c, and there's a field Payment__c on ParentObject that gets updated in several different ways based on the values in both ParentPicklist__c on ParentObject and ChildPicklist__c on ChildObject. There are nine different variations of how Payment calculates based on the values in those two picklists.

I've had some success with Workflow Rules. Payment calculates based on currency fields on both ParentObject and ChildOject. I created 6 RollUp Summary fields on ParentObject that are each not blank if ChildPicklist is set to a certain value, so that I can run the workflow rules based on RollUpSummaryX != 0 and ParentPicklist = VALUE. There are 9 workflow rules activated.

Everything runs correctly if there's a change to ParentObject, but if there's a change to ChildPicklist, then the workflow rules don't run until ParentObject gets edited and saved once again, even though the value in the ParentObject's RollUpSummaries have changed. I even tried adding a workflow rule to ChildObject that would affect a field on ParentObject every time ChildObject was modified, just to try and trigger the workflow rules to run.

Am I making sense? How can a change to ChildObject trigger workflow rules on ParentObject?

I first tried a simple Apex Trigger, my first trigger I've made, and was able to get it to work in theory, but the problem was that it runs before update, and so when the ParentPicklist value changes, the Payment field doesn't change until ParentObject is edited and saved AGAIN. So the failure of the workflow rules is better than this failure.

Should I try with a Trigger once more, and is there a way to reference the value in a field that is about to be saved and use that to determine the way the trigger runs? (i.e. beforeUpdate check value of ParentPicklist)

Or should I create the workflow rule or trigger on the ChildObject, and if I do that, what happens if there's a change on the ParentObject, will it trigger a workflow on ChildObject?

  • I added an identical workflow rule to the ChildObject, so that when ChildObject gets modified Payment__c is updated the same way that when ParentObject gets modified. The real problem is that the formulas for updating Payment__c reference RollUpSummaries of ChildObject, which change based on ChildPicklist, but that aren't calculated until after the Workflow Rules run. So we need to re-run the workflows after the RollUpSummaries are re-calculated. help! – Jack Sanford May 27 '15 at 21:11
2

You could create after insert / update trigger on child object that would update parent object(just load it from database and update) that would trigger workflows on Parentobject.

Following code should do the trick.

trigger updateParent on Child_Object__c (after insert, after update) {

    List<Id> listOfParentIdsToUpdate = new List<Id>();

    for(Child_Object__c current : Trigger.new){
        listOfParentIdsToUpdate.add(current.Parent_Object__r.Id);
    }

    List<Parent_Object__c> objectsToUpdate = [SELECT id FROM Parent_Object__c WHERE id IN : listOfParentIdsToUpdate];
    update objectsToUpdate;

}

However, you need to remember that you should never add trigger on Parent_Object__C that would update child, it will result in infinite loop and error.

  • I think this is what I want to do, but so new to Apex I am struggling with how to set up that trigger. – Jack Sanford May 29 '15 at 14:59
  • Jack, I updated my answer, please check out sample code. – Kamil Grzywacz May 29 '15 at 19:25
  • Thanks so much for giving me the code. I think that might work for another use. This particular issue seems to be impossible due to the roll-up summary fields being the last thing to re-calculate no matter what. The calculation I'm trying to do on a parent object after modifying the child relies on several roll-up summaries across several objects, including a grandparent (Opportunity) and an uncle (a different child of Opportunity). We are looking at ways of simplifying things, but it looks like we may have to calculate SUMs in Apex to achieve this. – Jack Sanford Jun 1 '15 at 17:58
1

You might find that even though you can roll up without Andrew Fawcett's Declarative Rollup Lookup Summaries tool, using it here might be advantageous as it does cause updates. In addition, it allows you to do child picklist concatenations, unique concatenations, and the like. Might help.

  • Unfortunately that didn't work. Even after I re-created the roll up summaries involved using the Declarative tool the trigger running on the child object used the old values for calculations, and the workflow rules didn't fire on the parent object. – Jack Sanford May 29 '15 at 17:47
  • Try switching from real time to a scheduled run? – Adrian Larson May 29 '15 at 20:58

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.