+ Start a Discussion
shashikant pandeyshashikant pandey 

Getting Unexpected token error

Hi All,

While creating dynamic query I am getting Error  Unexpected token : '{ 
An unexpected error has occurred. Your development organization has been notified.


below are my code:
public void agreementSearchResult(){
       // String searchString = '%' + agreementSearch + '%';
         String condition1 = ' Apttus__Contract_End_Date__c >= TODAY';
        String condition2 = ' AND Name LIKE \'%' +agreementSearch+'%\'';
        String condition3 = ' AND Id IN :'+agreementIds;
        String sorting = ' ORDER BY BD_Company_Information__r.Name';
        String query='SELECT Name, Apttus__FF_Agreement_Number__c, Apttus__Primary_Contact__c,Apttus__Primary_Contact__r.Name,' 
                        +'Apttus__Account__c,Apttus__Account__r.Name,BD_Company_Information__c,'
                        +'BD_Company_Information__r.Name,Apttus__Account__r.TopLevelAccountNameText__c,'
                        +'CurrencyIsoCode FROM Apttus__APTS_Agreement__c where';
        
        //system.debug('Query string =>'+query+condition1+condition3+condition2+sorting);
        if (agreementSearch !=null && agreementSearch !=''){
            system.debug('############################################');
            query+=condition1+condition3+condition2+sorting;
            system.debug('Agreement Search string : =>'+query);
            agreementList = Database.query(query);
            system.debug('Agreement List => '+agreementList);
            
        }
        else{
            system.debug('&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&');
            query+=condition1+condition3+sorting;
            system.debug('Query String ===>'+query);
            agreementList = Database.query(query);
            system.debug('Empty Agreement Search string: =>'+query);
        }
        
     
    } 


Below are my debug log:
SELECT Name, Apttus__FF_Agreement_Number__c, Apttus__Primary_Contact__c,Apttus__Primary_Contact__r.Name,Apttus__Account__c,Apttus__Account__r.Name,BD_Company_Information__c,BD_Company_Information__r.Name,Apttus__Account__r.TopLevelAccountNameText__c,CurrencyIsoCode FROM Apttus__APTS_Agreement__c where Apttus__Contract_End_Date__c >= TODAY AND Id IN :{a233O0000000GrXQAU, a233O0000000HIOQA2, a233O0000000KiBQAU, a233O0000000LCbQAM, a233O0000000LD0QAM, a233O0000000LDUQA2, a233O0000000LDZQA2, a233O0000000LE3QAM, a233O0000000LbXQAU, a233O0000000NthQAE, ...} ORDER BY BD_Company_Information__r.Name

could anyone can help me out to resolve the issue as soon as possible.

Thanks!!
David Zhu 🔥David Zhu 🔥
When building query string for database.query(), you cannot use syntax " ID in :agrementIds".
You may have to change this line 
string condition3 = ' AND Id IN :'+agreementIds;

to 

string condition3 = ' AND Id IN (\''+String.join(agreementIds,'\,,\'') + '\')';
shashikant pandeyshashikant pandey
Thanks David for your reply.
 public void agreementSearchResult(){
        
        String condition1 = ' AND Apttus__Contract_End_Date__c <TODAY';
        String condition2 = ' AND Name LIKE \'%' +agreementSearch+'%\'';
        String condition3 = ' Id IN :agreementIds';
        String sorting    = ' ORDER BY BD_Company_Information__r.Name';
        String query      = 'SELECT Name, Apttus__FF_Agreement_Number__c,AgreementStatus__c, Apttus__Primary_Contact__c,Apttus__Primary_Contact__r.Name,' 
                            +'Apttus__Account__c,Apttus__Account__r.Name,BD_Company_Information__c,Apttus__Contract_End_Date__c,'
                            +'BD_Company_Information__r.Name,Apttus__Account__r.TopLevelAccountNameText__c,'
                            +'CurrencyIsoCode FROM Apttus__APTS_Agreement__c where ';
        
        system.debug('Query string =>'+query+condition1+condition3+condition2+sorting);
        if (agreementSearch !=null && agreementSearch !=''){
            system.debug('############################################');
            //query+=condition1+condition2+condition3+sorting;
            query+=condition3+condition2+condition1+sorting;
            system.debug('Agreement Search string : =>'+query);
            agreementList = Database.query(query);
            system.debug('Agreement List => '+agreementList);
            /*for (Apttus__APTS_Agreement__c agreement : agreementList) {
            agreementWrappersAll.add(new AgreementWrapper(agreement));
            }*/
            
        }
        else{
            system.debug('&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&');
            system.debug('Query string 0 ===>'+query);
            query+=condition3;
            system.debug('Query String 1 ===>'+query);
            query+=condition1;
            system.debug('Query String 2 ===>'+query);
            query+=sorting;
            agreementList = Database.query(query);
            system.debug('Empty Agreement Search string: =>'+query);
            system.debug('Ag List ======>'+agreementList);
            /*for (Apttus__APTS_Agreement__c agreement : agreementList) {
            agreementWrappersAll.add(new AgreementWrapper(agreement));
            }*/
        }
        
     
    } 

This code worked.