Skip to main content

I have this user case where a community user needs to see the appointments of all the contacts in his parent account.Appointment has a lookup relationship to contact.  Right now, in the first soql query I am getting the contactid of the user.

myContact=[select id,email, from contact where id =:loggdinUserRecord.contactid];

 The I am running a query to get the account id of that contact:

con=[select account.name,account.id from contact where id=:myContact.id];

and in the third and fourth query, I am getting the contact names for each of those accounts and the appointment for each of those contacts.

Is this how it's supposed to be or can I optimize the soql queries to make it more efficient?

Thanks

 
2 answers
  1. Aug 4, 2017, 5:54 PM
    Now that I think about it, you can do it in one query and keep the Appointments organized by Contact.  This gives you a list of all the Contacts in the User's Contact's Account, and their Appointments as child records.

    <pre>

    List<Contact> contacts =

        [   SELECT  Id, FirstName, LastName,

                (   SELECT  Id, <other fields>

                    FROM    Appointments__r

                )

            FROM    Contact__c

            WHERE   AccountId IN (SELECT Contact.AccountId FROM User WHERE Id = :loggedInUserRecord.Id)

        ];

    </pre>
0/9000