1

Have two custom objects, Master and Child. Child has a lookup to Master.

Trigger is saving, but not firing when I update the Child:

trigger updateMasterName on Child_Custom__c (before insert, before update){
    list<Id> masIds = new list<Id>();
    list<Master_Custom__c> masters = new list<Master_Custom__c>();
    for(Child_Custom__c cc:trigger.new){
        masIds.add(cc.Master_Custom__r.id);
    }
    for(Master_Custom__c mas:[select Id from Master_Custom__c where Master_Custom__c.id IN :masIds]){
        for(Child_Custom__c ccc : trigger.new){
            if(ccc.Name == 'aaa'){
                ccc.Master_Custom__r.Name ='Wow this worked';
                masters.add(mas);
            }
        }
    }
    update masters;
}
  • I am not sure I follow your question. What is working and what isn't? Can you update your post with more context? – Sebastian Kessel Feb 10 '16 at 21:38
  • Updated...trigger is not firing, but not getting error either. . When the Child is edited or created, I just want to update the name of the related Master record. – TNSalesforceGuru Feb 10 '16 at 21:40
  • Wait. Is the trigger running or not? (Firing = running) It might be firing but not updating, or simply not firing. Which is it? – Sebastian Kessel Feb 10 '16 at 21:47
  • I have no idea if it's running or not. All I know is the name of the Master object is not getting updated. – TNSalesforceGuru Feb 10 '16 at 21:48
1

Ok, so the problem is that you are setting a field through the relationship:

ccc.Master_Custom__r.Name = 'Wow this worked';

However, what you need to do is actually set the name field on the Master object itself: ie

mas.Name = 'Wow this worked';

I think this will fix it for you.

To further illustrate, you essentially have a bunch of lists in memory, one of children records, and one of master records. Since you are modifying a property of the child record and saving the master list, the change never appears.

  • Thanks Caspar, that's how I previously had that line of code, tried again, still no luck – TNSalesforceGuru Feb 10 '16 at 21:46
  • Ok, now you need to do some debugging - make sure that you are getting to that loop using some debug statements, ensure that your trigger is set up to work on the right events. You probably could simplify your top loop from this cc.Master_Custom__r.id to this cc.Master_Custom__c. Try removing the if statement also - just to confirm that you can set something. – Caspar Harmer Feb 10 '16 at 21:50
  • Thanks Caspar, I don't know how to do debug statements. I am an Apex novice, and this is an example of my trying to learn – TNSalesforceGuru Feb 10 '16 at 21:51
  • Well that was it, made this change and it worked, thanks! cc.Master_Custom__r.id to this cc.Master_Custom__c – TNSalesforceGuru Feb 10 '16 at 21:52
  • 1
    Oh great! Happy learning. – Caspar Harmer Feb 10 '16 at 21:54
4

Your logic to identify the master to update is a little wonky. You're not associating the master with its proper child. Check out my sample below.

trigger updateMasterName on Child_Custom__c (before insert, before update){
    list<Id> masIds = new list<Id>();
    list<Master_Custom__c> masters = new list<Master_Custom__c>();

    for(Child_Custom__c cc:trigger.new){
        masIds.add(cc.Master_Custom__c);
    }

    Map<Id, Master_Custom__c> masters = new Map<Id, Master_Custom__c>([select Id from Master_Custom__c where id IN :masIds]);

    for(Child_Custom__c ccc : trigger.new){            
        if(ccc.Name == 'aaa'){
            if (masters.containsKey(ccc.Master_Custom__c)) {
              Master_Custom__c mas = masters.get(ccc.Master_Custom__c);
              mas.Name ='Wow this worked';

              masters.add(mas);
            }
        }
    }

    update masters;
}
  • 1
    The comment from @Caspar Harmer regarding cc.Master_Custom__r.id was dead on. I think your issue with the update was the use of the relationship id (__r.id) instead of the equivalent (__c). Plus the parent-child logic... – Sebastian Kessel Feb 10 '16 at 21: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.