Search code examples
apex-codesoql

soql query in apex?


I'm new to this, so sorry about this q.

I have 2 objects in an MDR: Child_c and Contacts. I have a list of child_c records which contains the contact_id. From the contact object, I need to get the firstName, lastName, Id (which ='s the contact_id in child). My query is wrong though. can someone help me with it please???

Thanks!

List peopleInEventContactInfo = new List ();

for (Contact c: [ select firstname, lastname, Id, ytd__c from Contact where id in ContactIds ]){ peopleInEventContactInfo.add(c); }


Solution

  • Assuming ContactIds is a list or set of the contact Ids, then you simply need a colon in front of it:

    for (Contact c : [select firstname, lastname, Id, ytd__c
                      from   Contact
                      where  id in : ContactIds])
    {
        peopleInEventContactInfo.add(c);
    }
    

    But you also don't need the loop:

    list<Contact> peopleInEventContactInfo = [select FirstName, LastName, Id, YTD__c
                                              from   Contact
                                              where  Id in : ContactIds];