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
Adil_SFDCAdil_SFDC 

SOQL Query re use

Hi I have a SOQL query that i want to reuse by changing the where clause. I dont want to repeat the SOQL multiple times. How can this be acheived. 

Here is my query 

List<Case> groupedCases = [Select Photographer_Contact__r.Name,Scheduled_Date__c from Case 
                                where RecordType.DeveloperName In('Time_Booking', 'Media_Package', 'Photography') 
                                and Scheduled_Date__c =:dates and Photographer_Contact__r.id = :con.id];
In the above query i need results without Scheduled_Date__c =:dates 
and another query I need results without Photographer_Contact__r.id = :con.id

so that i can use respective results for further processing. 
Or is there a way i can process the resultlist 'groupedcases' not to include them .?

Deepak Kumar ShyoranDeepak Kumar Shyoran
If you don't want to fire the query on multiple times then you need to remove the where Part from your query and then need to use loop to filter data from a single list based you criteria.

And in other requirement where you want to exclude those field so you can exclude them by using != for ex .  where cheduled_Date__c =:dates
Adil_SFDCAdil_SFDC
Hi Thank you for quick response. 
My query returns different results if i change my where. 
how can i get more explicit soql results. 
can you give an example

Deepak Kumar ShyoranDeepak Kumar Shyoran
For that either you need to fire a another option and if you don't want to go with that then need to use a loop create a another list by explicit records you want to avoid using if condition inside a loop.
Adil_SFDCAdil_SFDC
u mean to say avoid query in for loop?