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:
   @AuraEnabled  
     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.debug('Account'+a);
        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.debug('Account'+a);
        system.assertEquals(0, a.size());
    }
}

Below is the coverage for the test class.

User-added image
Hope this will help you

Thanks and regards,

Mahesh

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
Hi,

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

@isTest
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() {
        try{
            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='test@test.com',hed__Citizenship__c='Canada');  
            insert studentContact;
            
            //Insert Application
            application=new Application__c(Contact__c=studentContact.id,Partner_Account_Application__c=partnerAccount.id);
            insert application;
            
               //Insert RFI
                rfi= new RFI__c (Email__c='testrfi@somerfi.com',Last_Name__c='Test RFI',First_Name__c='Test RFI',Contact__c=studentContact.id );
                insert rfi;
            
           
            //Insert Task
            task=new Task(Subject='Call',whoId=studentContact.id,ActivityDate=system.today(),Priority ='Normal',Due_Date__c=system.today()-1);
            insert task;
           
            ContactTaskOnApplication.getContactTask(application.id,application.Contact__c);
          //  ContactTaskOnApplication.getContactTask(rfi.id,rfi.Contact__c);
            
            //application.id, application.Contact__c
        }
        catch(exception e){
            
        }
        
    }
}
Apex class
=====
public class ContactTaskOnApplication {
    
    @AuraEnabled
    public static List<Task> getContactTask(Id recordId,String contactAPIName){
        system.debug('recordId'+recordId);
        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;
    }
    
}