+ Start a Discussion
David Torres 39David Torres 39 

System.QueryException: unexpected token: 'LIMIT' when debugging a class

Hi there! 

I'm new at Salesforce and I'm trying to debug a class but I'm receiving this error "System.QueryException: unexpected token: 'LIMIT' at line 24, column 1. Can anyone advise what is wrong_ as I cannot seem to find the answer, thanks 

public with sharing class dvd_CustomLookupController {

    @AuraEnabled(cacheable=true)
    public static List<sObject> search(String searchTerm, String searchField, string myObject, String filter) {
        
        String myQuery = null;
        String searchFieldWhere;
        system.debug(searchTerm+':'+searchField+':'+myObject+':'+filter);
        if(String.isBlank(searchField)){
            searchField = ' ';
            searchFieldWhere = 'Name';
        }else{
            searchFieldWhere = searchField;
            searchField = ' ,' + searchField;
        }
        if(String.isNotBlank(filter)){
            myQuery = 'Select Id, Name '+searchField+' from '+myObject+' Where '+searchFieldWhere+' Like  \'%' + searchTerm + '%\' AND '+filter+' LIMIT  5';
        }else {
            if(String.isBlank(searchTerm)){
                myQuery = 'Select Id, Name '+searchField+' from '+myObject+' Where LastViewedDate != NULL ORDER BY LastViewedDate DESC LIMIT  5';
                
            }else {
                myQuery = 'Select Id, Name '+searchField+' from '+myObject+' Where '+searchFieldWhere+' Like \'%' + searchTerm + 'DESC LIMIT  5';
            }
        }
        List<sObject> lookUpList = database.query(myQuery);
        system.debug(myQuery);
        
        system.debug(lookUpList.size());
        return lookUpList;
    }
}

Best Answer chosen by David Torres 39
mukesh guptamukesh gupta
Hi David,

Please use below code:-
 
public with sharing class dvd_CustomLookupController {

    @AuraEnabled(cacheable=true)
    public static List<sObject> search(String searchTerm, String searchField, string myObject, String filter) {
        
        String myQuery = null;
        String searchFieldWhere;
        system.debug(searchTerm+':'+searchField+':'+myObject+':'+filter);
        if(String.isBlank(searchField)){
            searchField = ' ';
            searchFieldWhere = 'Name';
        }else{
            searchFieldWhere = searchField;
            searchField = ' ,' + searchField;
        }
        if(String.isNotBlank(filter)){
            myQuery = 'Select Id, Name '+searchField+' from '+myObject+' Where '+searchFieldWhere+' Like  \'%' + searchTerm + '%\' AND '+filter+' LIMIT  5';
        }else {
            if(String.isBlank(searchTerm)){
                myQuery = 'Select Id, Name '+searchField+' from '+myObject+' Where LastViewedDate != NULL ORDER BY LastViewedDate DESC LIMIT  5';
                
            }else {
                myQuery = 'Select Id, Name '+searchField+' from '+myObject+' Where '+searchFieldWhere+' Like \'%' + searchTerm +'\''+ ' ORDER BY LastViewedDate DESC LIMIT  5';
			}
        }
        List<sObject> lookUpList = database.query(myQuery);
        system.debug(myQuery);
        
        system.debug(lookUpList.size());
        return lookUpList;
    }
}

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

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

Thanks
Mukesh 

All Answers

Sai PraveenSai Praveen (Salesforce Developers) 
Hi David,

I need the way you are calling the class should be different. By just giving the filter field will give you the error.

You need to mention exact filter for that as below.
 
dvd_CustomLookupController.search('','Active__c','Account','Active__c=\'True\'');

If you just give the Active__c field in the last parameter the query returned is as below which is cauing the issue.
 
Select Id, Name  ,Active__c from Account Where Active__c Like  '%%' AND Active__c LIMIT  5

Just put the debug statement after every myquery variable so you will get what the query is and where it is causing the issue.


If this solution helps, please mark it as best answer.


Thanks,
 
mukesh guptamukesh gupta
Hi David,

Please use below code:-
 
public with sharing class dvd_CustomLookupController {

    @AuraEnabled(cacheable=true)
    public static List<sObject> search(String searchTerm, String searchField, string myObject, String filter) {
        
        String myQuery = null;
        String searchFieldWhere;
        system.debug(searchTerm+':'+searchField+':'+myObject+':'+filter);
        if(String.isBlank(searchField)){
            searchField = ' ';
            searchFieldWhere = 'Name';
        }else{
            searchFieldWhere = searchField;
            searchField = ' ,' + searchField;
        }
        if(String.isNotBlank(filter)){
            myQuery = 'Select Id, Name '+searchField+' from '+myObject+' Where '+searchFieldWhere+' Like  \'%' + searchTerm + '%\' AND '+filter+' LIMIT  5';
        }else {
            if(String.isBlank(searchTerm)){
                myQuery = 'Select Id, Name '+searchField+' from '+myObject+' Where LastViewedDate != NULL ORDER BY LastViewedDate DESC LIMIT  5';
                
            }else {
                myQuery = 'Select Id, Name '+searchField+' from '+myObject+' Where '+searchFieldWhere+' Like \'%' + searchTerm +'\''+ ' ORDER BY LastViewedDate DESC LIMIT  5';
			}
        }
        List<sObject> lookUpList = database.query(myQuery);
        system.debug(myQuery);
        
        system.debug(lookUpList.size());
        return lookUpList;
    }
}

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

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

Thanks
Mukesh 
This was selected as the best answer
David Torres 39David Torres 39
Thank you Mukesh,

I knew there was something wrong in the query string, I just couldn't figure out what was it