0

I created a custom object named Related_Staff_Positions which is a child lookup to the contact object.

Whenever one saves a new record, in a trigger I wish to get all records in the Related_Staff_Positions object for the given contact and the contact id itself.

Here is my skeleton code that does not work at all. What do I need to modify? Sorry but I am still new at this.

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

//  Set<Id> setConID = new Set<Id>();
    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 Contact__c IN: Trigger.New];
    <ID> id = trigger.get(contact_c);
    if(Trigger.isInsert)
    {
        for(Related_Staff_Positions__c ct : Trigger.New)
        {
            ct.Contact__c;
        }
    }
    else if(Trigger.isUpdate) 
    {
        for(Related_Staff_Positions__c ct : Trigger.New)
        {

        }

    }
}
4
  • Read some docs about Triggers. If you write the trigger on Related_Staff_Positions__c, then your trigger.new will give you list of Related_Staff_Positions__c; not list of Contacts.
    – highfive
    Commented Jun 27, 2016 at 8:47
  • Can you just tell me, I want to do a query with the contact__c id of the record that was just saved, how to get it? I don't want to iterate through every Related_Staff_Positions__c record so I don't want to iterate thought trigger.new but how to do it? Does this make sense?
    – Apex N-u-b
    Commented Jun 27, 2016 at 8:57
  • As per my understanding you want to iterate on all the child records of the contact. hope this will give you an idea : salesforce.stackexchange.com/questions/94895/…
    – C0DEPirate
    Commented Jun 27, 2016 at 9:02
  • Sorry, I wasn't clear. The trigger fired because a user saved a record, it has a contact lookup field named contact__c. I want that id so I can query the Related_Staff_Positions__c object and get the related records to the contact. How to access that id? It can't be in the Trigger.New loop because that iterates thought all records, I only want the current record's contact id.
    – Apex N-u-b
    Commented Jun 27, 2016 at 9:08

2 Answers 2

1

If you need to get the parent Contact id, you have to access the Contact__c of incoming child records. And your query filter should be set to filter from child Ids and not the Contact__c. So your initial query should be like below.

rsp_LIst = [Select Contact__c , first_name__c, last_name__c from Related_Staff_Positions__c where Id IN: Trigger.New];

And also your trigger should be bulkified as the best practice. Don't consider only one record, but all the incoming records in the trigger context. Iterate through the result list and collect the Contact Ids.

rsp_LIst = [Select Contact__c , first_name__c, last_name__c from Related_Staff_Positions__c where Id IN: Trigger.New];
List<String> contactIds = new List<String>();
for(Related_Staff_Positions__c rsp : Related_Staff_Positions__c){
    contactIds.add(rsp.Contact__c);
}

// then you can retrieve the other child records which mapped into the given contact
List<Related_Staff_Positions__c> otherChildren = [Select Contact__c , first_name__c, last_name__c from Related_Staff_Positions__c where Id IN: contactIds];
2
  • This is what I was looking for. Thanks. If I can push my luck, I need to add these values into a long text field named "Related_Staff_Positions__c" which is on the contact object, how to do this? 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 as line 1 first name, last name role line 2 same thing line 3 same etc..
    – Apex N-u-b
    Commented Jun 27, 2016 at 9:56
  • This will be a long answer and not related to this post. Try, 1). iterate over otherChildren 2). count them and manipulate with you logic. 3). retrieve the contact with Related_Staff_Positions__c field and update the contact as you need.
    – highfive
    Commented Jun 27, 2016 at 11:43
1

I am not sure why you are allowed to compile your code. Because this line:

rsp_LIst = [Select Contact__c , first_name__c, last_name__c from Related_Staff_Positions__c where Contact__c IN: Trigger.New];

would give the

Invalid Bind Variable error

Anyways, you can change the above line to search with the Related_Staff_Positions__c Id like this:

rsp_LIst = [Select Contact__c , first_name__c, last_name__c from Related_Staff_Positions__c where ID IN: Trigger.New];

You will get Contact ID from this list as:

rsp_LIst[0].Contact__c

Hope this helps!

4
  • Here is what I don't understand, ins't Trigger.New a collection of all records in the given object? If so, how do I get the specific record that I just saved?
    – Apex N-u-b
    Commented Jun 27, 2016 at 9:31
  • Trigger.New is the collection of all the "NEW" records. Meaning the record you just saved. If you have written trigger on Object A, Trigger.New would contain the record of Object A you just saved either by inserting or updating. Commented Jun 27, 2016 at 9:34
  • That was clear. Is it possible for Trigger.New to have more than one record?
    – Apex N-u-b
    Commented Jun 27, 2016 at 12:02
  • Yes. In case you are updating or inserting the record through data loader or in bulk anyways, then the Trigger.new would have more than one record. Commented Jun 27, 2016 at 12:24

You must log in to answer this question.

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