1

In the following trigger, it gets child records of a custom object based on the contact id of a record that was just saved. I need to update their data into a field on the parent contact record.

The field on the contact record is named Related_Staff_Positions__c and it is a long text field.

I.e. if 3 child records were returned, I need to clear the Related_Staff_Positions__c field, and then add each record on a separate line in the Related_Staff_Positions__c field such that line 1 shows first name, last name role line 2 same thing line 3 same etc.

What do I add in the second for loop below?

trigger trgr_Related_Staff_Position on Related_Staff_Positions__c (after insert,after update,after delete) 
{


List<String> contactIds = new List<String>();    

List<Related_Staff_Positions__c> rsp_List = new List<Related_Staff_Positions__c>();    
rsp_List = [Select Contact__c , first_name__c, last_name__c from Related_Staff_Positions__c where Id IN: Trigger.New];

for(Related_Staff_Positions__c rsp : rsp_List){
    contactIds.add(rsp.Contact__c);
}

List<Related_Staff_Positions__c> otherChildren = [Select Contact__c , first_name__c, last_name__c from Related_Staff_Positions__c where Id IN: contactIds];

for(Related_Staff_Positions__c rsp : otherChildren){

}

}

1 Answer 1

1

The pattern I would use for the insert and update is this:

trigger MyTrigger on Related_Staff_Positions__c (after insert, after update) {
    Map<Id, Contact> m = new Map<Id, Contact>();
    // Find the parent contacts
    for (Related_Staff_Positions__c rsp: Trigger.new) {
        m.put(rsp.Contact__c, new Contact(
                Id = rsp.Contact__c,
                Related_Staff_Positions__c = ''
                ));
    }
    // Query all child objects and re-build the text in the parent contacts
    for (Related_Staff_Positions__c> rsp : [
            Select Contact__c, first_name__c, last_name__c
            from Related_Staff_Positions__c
            where Contact__c in :m.keySet()
            order by CreatedDate
            ]) {
        Contact c = m.get(rsp.Contact__c);
        c.Related_Staff_Positions__c += rsp.first_name__c + ' ' + rsp.last_name__c + '\n';
    }
    update m.values();
}

The delete case will need to use Trigger.old.

3
  • Hi Keith, the code works great. Now that I am trying to understand it...1) what is update m.values saving, the resetting Related_Staff_Positions__c to being empty? 2) When does the c.Related_Staff_Positions__c +=...line get saved? 3) in your line m.put(rsp.Contact__c, new Contact( Id = rsp.Contact__c, Related_Staff_Positions__c = '' )); why does "new Contact" achieve? You're not creating a new contact
    – Apex N-u-b
    Commented Jun 27, 2016 at 12:52
  • @ApexN-u-b 1) It is updating the Contact object instances to the database. 2) When the update m.values(); is done. 3) This line adds a new (in memory only) Contact object to a map keyed by the Contact ID. Using the map ensures there will only ever be one Contact object per Contact ID in memory ready for when the update is done.
    – Keith C
    Commented Jun 27, 2016 at 13:12
  • A common pattern in Apex is to create collections in memory and manipulate those e.g. appending to the text field. Once the in-memory objects are fully updates they are then committed to the database using an insert or update.
    – Keith C
    Commented Jun 27, 2016 at 13:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .