3

I am attempting to update the parent of a newly created object with fields from grandparent of the object. Does anybody have an answr to this ? I'm currently trying to convert this code into this. As it is now, it will update the grandchild which the trigger is running on - but I don't want an update on the grandchild, only the parent.

I'm getting some questions, so let me clarify : The hierarchy goes like this :

  • Grandparent
    • Parent
      • Grandchild

and I want the trigger to run upon the insert of a new Grandchild. It will then grab the fields from the Grandparent of the Grandchild and in turn update the Parent of the Grandchild.

trigger updateParentwithGrandparent on Grandchild__c (after insert) {
// after insert on Grandchild__c sobject


Set<Id> parentIds=new Set<Id>();
for (Grandchild__c gc : trigger.new)
{
   parentIds.add(gc.Parent__c);
}

// now pull back the parents, and the corresponding grandparent
Map<Id, Parent__c> parentsById=new map<Id, Parent__c>();

parentsById.putAll([select id, Grandparent__r.MyField__c from Parent__c where id in :parentIds]);

// now iterate the grandchildren, copying the Grandparent MyField__c to the grandchild MyField__c
//
for (Grandchild__c gc : trigger.new)
{
   Parent__c parent=parentsById.get(gc.Parent__c);
   gc.MyField__c=parent.Grandparent__r.MyField__c;
}

}

Thank you very much for your help and time.

  • Your question is vague. What exactly are you trying to do here? You are trying to, upon insertion of a grandchild, write "MyField__c" from the grandparent to the parent? Nothing written to the newly created grandchild? Please help elaborate... – Nathan Williams Oct 18 '13 at 18:48
  • Just so I understand. The code above acts on a grandchild based on fields in the grandparent. What is your desired outcome? To have it update the parent object with a field from the grandparent? But you still want it triggered on off the grandchild, or no? – Chris Duncombe Oct 18 '13 at 18:49
  • That's exactly right Dunc. Trigger gets initiated by the Grandchild - and then gets fields from the GrandParent - which then updates the Parent to the Grandchild. In other words, the middle man in the hierarchy (between the Grandparent and the Grandchild). – Zoom_v Oct 18 '13 at 19:01
  • I'm still confused. You say "updates the parent to the grandchild", but what your code is doing is updating the grandparent to the grandchild, and what you asked for was grandparent to parent... I'm very confused – Nathan Williams Oct 18 '13 at 19:06
7

I think this answers your question where MyField__c is updated from Grandparent to Parent upon grandchild insertion:

trigger updateParentwithGrandparent on Grandchild__c (after insert) {

    // Get a list of all Parent Ids
    Set<Id> parentIds = new Set<Id>();
    for (Grandchild__c gc : trigger.new) {
        parentIds.add(gc.Parent__c);
    }

    // Get a map of all Parents with Grandparent field information
    Map<Id, Parent__c> parentsById = new Map<Id, Parent__c>();
    parentsById.putAll([SELECT Id, Grandparent__r.MyField__c FROM Parent__c WHERE Id IN :parentIds]);

    // Loop through each parent and update Parent.MyField__c from Grandparent.MyField__c
    List<Parent__c> parentsToUpdate = new List<Parent__c>();
    for (Id parentId : parentsById.keySet()) {
        Parent__c parent = new Parent__c();
            parent.Id = parentId;
            parent.MyField__c = parentsById.get(parentId).Grandparent__r.MyField__c;
        parentsToUpdate.add(parent);
    }

    // Update Parents (bulkified)
    update parentsToUpdate;

}
  • thank you very much ! I'm getting an "Invalid Type : Parent" on this line : Parent__c parent = new Parent(); Can you help with that ? – Zoom_v Oct 18 '13 at 19:46
  • 1
    Oh, I'm sorry, that should be Parent__c parent = new Parent__c(); updating the post now – Nathan Williams Oct 18 '13 at 19:50
  • 1
    That did it ! Thanks so much Nathan ! I really appreciate it !!! – Zoom_v Oct 18 '13 at 20:06
3

Looks like you need an update statement at the end, to commit the changes to your list of changed Parent__c records.

update parentsById.values();

  • It's still only updating the grandchild, which was not (I think) the intent. We need more info... – Nathan Williams Oct 18 '13 at 18:53
  • Ah, I see that now. Good point. – Carolyn Grabill Oct 18 '13 at 18:54
  • Correct, I don't think the goal was to get the code in the example to work, its to change it to the desired outcome. What that outcome is, still unsure – Chris Duncombe Oct 18 '13 at 18:54

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.