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
Nagarjuna Reddy.PNagarjuna Reddy.P 

system.queryexception in test class

Hi All,
     I have a simple apex method used in ligthing lookup to search name and phone fields.
apex method:
     public static List<sObject> getRecordList(String objName,String searchText,String fieldInSearch,String phone){
        string searchKey = '%' + searchText + '%';
        string Query =' select ' +fieldInSearch+ ',' +phone;
        Query+=' from '+objName;
        Query+=' where '+fieldInSearch+ ' like  \''+searchKey+'\' LIMIT 5';
            system.debug('Query == >> '+Query);
        List<sObject> sObjectList = Database.query(Query);
        system.debug(' #### sobjectList ' + sObjectList);
        return sObjectList;
Test class :
 @isTest static void searchAcc(){
        String searchText = 'test';
        string field = 'name';
        string obj = 'Account';
        string phone='12345';
        List<account> a = apexHandler.getRecordList(obj,searchText,field,phone);
        system.assertEquals(1, a.size());
when i run the test the method gets failed and showing error System.QueryException: unexpected token: '12345' and at line no Database.query(Query) from apex method.
I gone through different solutions but unable to resolve, could any one help me with this to resolve and run test method succesfully.
Danish HodaDanish Hoda
Hi there,
system.debug('Query == >> '+Query); --> if you check this staement, you would be getting debug sattement like:

select name, '12345' from Account where name like  'test' LIMIT 5

Please change your logic for query
Nagarjuna Reddy.PNagarjuna Reddy.P
Thank you Danish for your reply,
       I need both name and phone fields in lookup search so that one can avoid selecting wrong records so I hope logic is correct...
based on your idea i just changed value for phone field in test method as string phone ='phone' instead of giving hardcoded value i.e '12345'.so that it satisfying dyanmic query and test method was succesful..
            Once again thank you for your help..
Mahesh NirmalMahesh Nirmal

Please change the test class data to- 

@isTest static void searchAcc(){
        String searchText = 'test';
        string field = 'name';
        string obj = 'Account';
        string phone= 'phone';
        List<account> a = Devforumclass.getRecordList(obj,searchText,field,phone);
        system.assertEquals(0, a.size());

Below is the coverage for the test class.

User-added image
Hope this will help you

Thanks and regards,


Nagarjuna Reddy.PNagarjuna Reddy.P
Hi Mahesh,
                    I had already applied the same logic as you suggested,its perfect and test method was successful.
any way thank you for your reply...
Ashu sharma 38Ashu sharma 38

I am also getting the same error System.QueryException: unexpected token how to fix it.

public class TestContactTaskOnApplication {
    public static Account partnerAccount;
    public static Contact studentContact;
    public static Application__c application;
    public static RFI__c rfi;
    public static Task task;
    static testmethod void testContactTaskOnApplicationMethod() {
            Id idRecordId = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('Educational_Institution').getRecordTypeId();
            //Insert Account
            partnerAccount = new Account(Name='Gwendwyr Test', RecordTypeId=idRecordId);
            insert partnerAccount;
            //Insert Contact
            studentContact = new Contact(LastName='Someguy', firstname='Someguy' , email='',hed__Citizenship__c='Canada');  
            insert studentContact;
            //Insert Application
            application=new Application__c(,;
            insert application;
               //Insert RFI
                rfi= new RFI__c (Email__c='',Last_Name__c='Test RFI',First_Name__c='Test RFI', );
                insert rfi;
            //Insert Task
            task=new Task(Subject='Call',,,Priority ='Normal',;
            insert task;
          //  ContactTaskOnApplication.getContactTask(,rfi.Contact__c);
            //, application.Contact__c
        catch(exception e){
Apex class
public class ContactTaskOnApplication {
    public static List<Task> getContactTask(Id recordId,String contactAPIName){
        system.debug('contactAPIName' +contactAPIName);
        if(recordId==null || contactAPIName==null){
            return null;     
        String sObjName = recordId.getSObjectType().getDescribe().getName();
        system.debug('sObjName '+sObjName);
        string query='SELECT Id, ' +contactAPIName+ ' FROM '+sObjName+ ' WHERE Id=:recordId';
        system.debug('query '+query);
        List<sObject> sObjectList=database.query(query);
        system.debug('sObjectList ' +sObjectList);
        if (!sObjectList.isEmpty()){
            id contactId=(Id)sObjectList[0].get(contactAPIName);
            List<Task> tsk=[SELECT Id,Subject,Priority,ActivityDate,Due_Date__c,
                            whoId FROM task WHERE whoId=:contactId];
            system.debug('tsk '+tsk);
            return tsk;
        return null;