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
Gaurav-GuleriaGaurav-Guleria 

System.QueryException: unexpected token: ')' in Dynamic SOQL

Hi Guys,

 

---------------Code Snippet---------------------

String mystring= String.ValueOF('sa.Site_CX__r.name=:solutionSite1 OR sa.Site_CX__r.name=:solutionSite2 OR sa.Site_CX__r.name=:solutionSite3');

 

   

lstSiteApp = Database.query('select id,Site_Application_List__c,Application_Id__c,Site_CX__r.name from Site_Application__c sa where \n'+

    'sa.Application_Id__c IN(select sma.Application_ID__c from Service_Model_Application__c sma where Service_Model_ID__c=:selectedServiceModel2)\n'+

    'AND (mystring) AND sa.id NOT IN(select sd.Site_Application_ID__c from Solution_Detail__c sd where sd.Solution_ID__r.id=:recordid2)\n'+

    'ORDER BY Site_Application_List__c');

---------------------------------------------------------------------------

When i am using sa.Site_CX__r.name=:solutionSite1 OR sa.Site_CX__r.name=:solutionSite2 OR sa.Site_CX__r.name=:solutionSite3 directly in place of mystring,code is working fine(using this will not solve the purpose of Dynamic SOQL).

 

If I use mystring,Error:System.QueryException: unexpected token: ')' is coming.

 

Please help.

 

Best Answer chosen by Admin (Salesforce Developers) 
hitesh90hitesh90

Hi Gaurav,

 

You have to update your dynamic SOQL with following.

 

lstSiteApp = Database.query('select id,Site_Application_List__c,Application_Id__c,Site_CX__r.name from Site_Application__c sa where \n'+

    'sa.Application_Id__c IN(select sma.Application_ID__c from Service_Model_Application__c sma where Service_Model_ID__c=:selectedServiceModel2)\n'+

    'AND (' + mystring + ') AND sa.id NOT IN(select sd.Site_Application_ID__c from Solution_Detail__c sd where sd.Solution_ID__r.id=:recordid2)\n'+

    'ORDER BY Site_Application_List__c');

 

 

Important :
Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.
 
Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator

All Answers

hitesh90hitesh90

Hi Gaurav,

 

You have to update your dynamic SOQL with following.

 

lstSiteApp = Database.query('select id,Site_Application_List__c,Application_Id__c,Site_CX__r.name from Site_Application__c sa where \n'+

    'sa.Application_Id__c IN(select sma.Application_ID__c from Service_Model_Application__c sma where Service_Model_ID__c=:selectedServiceModel2)\n'+

    'AND (' + mystring + ') AND sa.id NOT IN(select sd.Site_Application_ID__c from Solution_Detail__c sd where sd.Solution_ID__r.id=:recordid2)\n'+

    'ORDER BY Site_Application_List__c');

 

 

Important :
Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.
 
Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator

This was selected as the best answer
Gaurav-GuleriaGaurav-Guleria

Hi Hitesh,

 

Thanks for your solution.This is working.

 

Regards,

Gaurav

Gaurav-GuleriaGaurav-Guleria

Hi Hitesh,

 

One more query on this:

 

I am adding String Yes,but System is thowing exception.

--------------Code------------------

mystring = mystring + ' AND sa.Site_CX__r.FGSS__c= ' + ' Yes ';

 

Error:

System.QueryException: unexpected token: 'Yes'

 

How can I add Yes to this?

 

hitesh90hitesh90

Hi Gaurav,

 

Try to use below code

mystring = mystring + ' AND sa.Site_CX__r.FGSS__c=  Yes ';

Gaurav-GuleriaGaurav-Guleria

Hitesh,

 

Its not working,showing same error.

hitesh90hitesh90

what is the data type of FGSS__c field?

Gaurav-GuleriaGaurav-Guleria

Its of type Text.

hitesh90hitesh90

Then try to use following

mystring = mystring + ' AND sa.Site_CX__r.FGSS__c=  \'Yes\' ';

Gaurav-GuleriaGaurav-Guleria

Thanks for your reply Hitesh:) .Its working .You rock Man.