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
debprakash shawdebprakash shaw 

Soql query for retrieve record based on picklist field

picklist field name = Member_Level__c

value= Level 1 - Parent Account Holder
            Level 2 - Adult Secondary Account
            Level 3 - Child Dependent Account
 

Select Id,Account.id,Account.name,Account__c,Parent_Member_Name__r.id,(Select Id from cases),(select Id from Contact_Products__r) from contact WHERE Member_Level__c=:Level 1 - Parent Account Holder And AccountId IN :list_Employer

 

I try this query in batch class but when running the code I get an error "System.QueryException: unexpected token: '1' "

please tell me how to fix that

Thanks in Advance
 

Best Answer chosen by debprakash shaw
debprakash shawdebprakash shaw
I'm using this in my batch class
Please use the below code:-
  query='Select Id,Account.id,Account.name,Account__c,Parent_Member_Name__r.id,(Select Id from cases),(select Id from Contact_Products__r) from contact WHERE Member_Level__c =\ 'Level 1 - Parent Account Holder\' And AccountId IN :list_Employer'

All Answers

TUSHAR_MATHURTUSHAR_MATHUR
Hi Debprakash,

I think the problem is you are putting a static value with ' : ' . While in apex ' : ' means you are supposed to give a variable. And if you are specifying the string value you need to put them in between  ' ' .

You must try the below query : 

Select Id,Account.id,Account.name,Account__c,Parent_Member_Name__r.id,(Select Id from cases),(select Id from Contact_Products__r) from contact WHERE Member_Level__c= 'Level 1 - Parent Account Holder' And AccountId IN :list_Employer

Thanks & Regards
Tushar Mathur
 
debprakash shawdebprakash shaw

User-added image

 

Hi Tushar

Thanks for replying, I try this query but I also get the error 

 

mukesh guptamukesh gupta
Hi DebPrakesh,


Please use below code:-
Select Id,Account.id,Account.name,Account__c,Parent_Member_Name__r.id,(Select Id from cases),(select Id from Contact_Products__r) from contact WHERE Member_Level__c = 'Level 1 - Parent Account Holder' And AccountId IN :list_Employer


What you was doing wrong:-
:
operator use for dynamic parametr, if you use static parameter then don't need this operator
Level 1 - Parent Account Holder  ->>> this is not dynamic so use in ''
 
Select Id,Account.id,Account.name,Account__c,Parent_Member_Name__r.id,(Select Id from cases),(select Id from Contact_Products__r) from contact WHERE Member_Level__c=:Level 1 - Parent Account Holder And AccountId IN :list_Employer

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh
debprakash shawdebprakash shaw

Hi Mukesh,
Thanks for replying , after using your query like

Select Id,Account.id,Account.name,Account__c,Parent_Member_Name__r.id,(Select Id from cases),(select Id from Contact_Products__r) from contact WHERE Member_Level__c = 'Level 1 - Parent Account Holder' And AccountId IN :list_Employer

I'm getting some errors in my developer console. Error screenshot below please check that

Thanks.
 

User-added image

 

TUSHAR_MATHURTUSHAR_MATHUR
Hi Deb,

According to your screenshot you have started entering string with '  and didn't end it at right place can you please provide the current code.Or else check the code correctly and then save your apex class.
debprakash shawdebprakash shaw

Hi Tushar,

here is my code below

 global Database.QueryLocator start(Database.BatchableContext bc){ 
if(sObjectType == 'Order'){
            if(fromDate == null && toDate == null ){
                query='Select Id,account_holder__r.Account__c,account_holder__r.AccountId from Order__c WHERE account_holder__r.AccountId IN:list_Employer';
            }else{
                query='Select Id,account_holder__r.Account__c,account_holder__r.AccountId from Order__c WHERE account_holder__r.AccountId IN:list_Employer AND (NOT (CreatedDate >=:fromDate AND CreatedDate <=: toDate))'; 
            }
        }else if(sObjectType == 'Contact'){
            if(fromDate == null && toDate == null ){
                query='Select Id,Account.id,Account.name,Account__c,Parent_Member_Name__r.id,(Select Id from cases),(select Id from Contact_Products__r) from Contact WHERE AccountId IN :list_Employer';
            }else{
                query='Select Id,Account.id,Account.name,Account__c,Member_Level__c,Parent_Member_Name__r.id,(Select Id from cases),(select Id from Contact_Products__r) from contact WHERE Member_Level__c ='Level 1 - Parent Account Holder' And AccountId IN :list_Employer AND account__c NOT IN :set_NonDeleteId AND CreatedDate >=:fromDate AND CreatedDate <=:toDate';
            }
        }
        
        System.debug(query);
        return Database.getQueryLocator(query);
        
    }
Thanks,
 

debprakash shawdebprakash shaw
I'm using this in my batch class
Please use the below code:-
  query='Select Id,Account.id,Account.name,Account__c,Parent_Member_Name__r.id,(Select Id from cases),(select Id from Contact_Products__r) from contact WHERE Member_Level__c =\ 'Level 1 - Parent Account Holder\' And AccountId IN :list_Employer'
This was selected as the best answer