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
Nasif HasnainNasif Hasnain 

SOQL query or report

I want to query those record which have only specific type of picklist value.
Example: suppose we have an object called Company and in this company we have a record SF, AB, DC.
SF has picklist value x
AB has picklist value x,y
DC has picklist value  x there are many record like this....
and some reord has not picklist value.

so i want to query only those record which has picklist value only x. Can anyone help on this.
Best Answer chosen by Nasif Hasnain
SarvaniSarvani
Hi Nasif,

Please use Excludes filter on both your report and SOQL for your requirement. For example, I am considering to fetch all the Accounts (Parent records) whose contacts (child records) are having picklist value only 'X' but not any other picklist values like Y or Z. Your Report filter should have filters like below which will only fetch record SF in your case:
User-added image
SOQL for fetching all the Account records based on the picklist value on contact object will be like below:
Select Account.id, Account.name,id,Multiselect__c , from contact where Multiselect__c INCLUDES ('X' ) and Multiselect__c  Excludes ('Y') and Multiselect__c  Excludes ('Z')

Hope this helps! Please mark as best if it solves your issue

Thanks

All Answers

ANUTEJANUTEJ (Salesforce Developers) 
Hi Nasif,

I think you can apply the file wherein you can mention the condition that if the value of the picklist is 'x' only then retrieve the record.

>> You could do something like this: select id from lead where status='open'

Here the status is the picklist and open is the value.

I hope this helps and in case if this came in handy can you please choose this answer as the best answer so that it can be used by others in the future.

Regards,
Anutej
Nasif HasnainNasif Hasnain
actually Lead is a parent object and Company is a child object and if i query - select id,  (select id from company where picklistvalue = x) from Lead then i am getting those parent record which has picklist x and y both. But i want parent record which child has only x picklist
ANUTEJANUTEJ (Salesforce Developers) 
Can you try adding the filter even after the lead in the query and check if you are able to get only the lead records where the picklist value is 'x'.
SarvaniSarvani
Hi Nasif,

Please use Excludes filter on both your report and SOQL for your requirement. For example, I am considering to fetch all the Accounts (Parent records) whose contacts (child records) are having picklist value only 'X' but not any other picklist values like Y or Z. Your Report filter should have filters like below which will only fetch record SF in your case:
User-added image
SOQL for fetching all the Account records based on the picklist value on contact object will be like below:
Select Account.id, Account.name,id,Multiselect__c , from contact where Multiselect__c INCLUDES ('X' ) and Multiselect__c  Excludes ('Y') and Multiselect__c  Excludes ('Z')

Hope this helps! Please mark as best if it solves your issue

Thanks
This was selected as the best answer