function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Prathyusha Balguri 4Prathyusha Balguri 4 

I have a method that takes account id as a parameter so it can be used in SOQL query to count the associated contacts of that account record. How do I pass the parameter to SOQL query in the where clause? I tried the below but it didn't work.

User-added image
Best Answer chosen by Prathyusha Balguri 4
Akshay Dhiman 63Akshay Dhiman 63
Hi Prathyusha,

Please use the below query it will resolve your issue:-

List<Contact> conList = [SELECT Id FROM Contact WHERE accountId =: recid];

Also in your query, the returned type is the list of accounts and you are assigning it to the list of Contact and also I have written a simple query instead of an inner query for the same purpose which you want to achieve.  Let me know if you need any help on this.

Hope this explanation will resolve your query. Mark it as the best answer if you find it helpful.
Thanks
Akshay

All Answers

ShirishaShirisha (Salesforce Developers) 
Hi Prathyusha,

Greetings!

Seems like the parameter which is you are trying to pass to the method is recId but the variable you have used in the SOQL query is rec.

I would suggest you to try by using recId to see,if it fix the issue.

Kindly mark it as best answer if it helps so that it can help others in the future.

Warm Regards,
Shirisha Pathuri
Prathyusha Balguri 4Prathyusha Balguri 4
Thank you Hemanth and Shirisha, but none of the above worked. 
Akshay Dhiman 63Akshay Dhiman 63
Hi Prathyusha,

Please use the below query it will resolve your issue:-

List<Contact> conList = [SELECT Id FROM Contact WHERE accountId =: recid];

Also in your query, the returned type is the list of accounts and you are assigning it to the list of Contact and also I have written a simple query instead of an inner query for the same purpose which you want to achieve.  Let me know if you need any help on this.

Hope this explanation will resolve your query. Mark it as the best answer if you find it helpful.
Thanks
Akshay
This was selected as the best answer
Hemanth MSNSHemanth MSNS
Hi Prathyusha,

Replace WHERE Id = '+rec' with WHERE AccountId = recId.

Regards,
Hemanth MSNS