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
Allan NolandAllan Noland 

Getting the Error System.QueryException: unexpected token: Includes Cannot figure out what I am Doing wrong

Hello,

I am building up a search string for use in a database.query(string).  I keep getting this error System.QueryException: unexpected token: Includes.  I have reworked the code several times with different ' ' escape methods.  Please help me figure out what i am doing wrong.  I have pasted the code that builds up the string along with the debug log to show what the string is once it is built up.
 

Here is the Code that builds up the string

Coder_Request__c coderrequestparams = [SELECT General_Specialties__c,Minimum_Experience_Requirement__c,Requested_EM_Specialties__c,Requested_Inpatient_Facility_Specialties__c,
            Requested_Outpatient_Specialties__c,Requested_Surgical_Specialties__c,Required_Credentials__c From Coder_Request__c Where id = 'a35M0000000398G']; //:pageid];
        string findGeneral_Specialties = coderrequestparams.General_Specialties__c;
        string findMinimum_Experience_Requirement = coderrequestparams.Minimum_Experience_Requirement__c;
        string findRequested_EM_Specialties = coderrequestparams.Requested_EM_Specialties__c;
        string findRequested_Inpatient_Facility_Specialties = coderrequestparams.Requested_Inpatient_Facility_Specialties__c;
        string findOutpatient_Specialties = coderrequestparams.Requested_Outpatient_Specialties__c;
        string findRequested_Surgical_Specialties = coderrequestparams.Requested_Surgical_Specialties__c;
        string findRequired_Credentials = coderrequestparams.Required_Credentials__c;
        string searchstring = '';
            IF(findGeneral_Specialties !=null)searchstring = searchstring + 'General_Specialties__c Includes(\''+ findGeneral_Specialties +'\''+ ')';
        	//IF(findMinimum_Experience_Requirement !=''&& searchstring =='')searchstring = searchstring + 'Minimum_Experience_Requirement__c Like'+'\''+'findMinimum_Experience_Requirement+ '%\'';
              // Else IF(findMinimum_Experience_Requirement !=''&& searchstring != '')searchstring = searchstring + 'AND Minimum_Experience_Requirement__c Like '+ '\''+ findMinimum_Experience_Requirement+ '%\'';
        	IF(findRequested_EM_Specialties !=null&& searchstring =='')searchstring = searchstring + 'E_M_Specialties__c Includes(\''+ findRequested_EM_Specialties+'\'' +')';
                Else IF(findRequested_EM_Specialties !=null&& searchstring != '')searchstring = searchstring + 'AND E_M_Specialties__c Includes(\''+ findRequested_EM_Specialties + '\'' +')';
        	IF(findRequested_Inpatient_Facility_Specialties !=null&& searchstring =='')searchstring = searchstring + 'Facility_Inpatient_Specialties__c Includes(\''+ findRequested_Inpatient_Facility_Specialties+'\''+ ')';
                Else IF(findRequested_Inpatient_Facility_Specialties !=null&& searchstring != '')searchstring = searchstring + 'AND Facility_Inpatient_Specialties__c Includes(\''+ findRequested_Inpatient_Facility_Specialties+ '\'' +')';
        	IF(findOutpatient_Specialties !=null&& searchstring =='')searchstring = searchstring + 'Facility_Outpatient_Specialities__c Includes(\''+ findOutpatient_Specialties+ '\'' +')';
                Else IF(findOutpatient_Specialties !=Null&& searchstring != '')searchstring = searchstring + 'AND Facility_Outpatient_Specialities__c Includes(\''+ findOutpatient_Specialties+ '\'' +')';
        	IF(findRequested_Surgical_Specialties !=null&& searchstring =='')searchstring = searchstring + 'Surgical_Specialties__c Includes(\''+ findRequested_Surgical_Specialties+'\'' + ')';
                Else IF(findRequested_Surgical_Specialties !=null&& searchstring != '')searchstring = searchstring + 'AND Surgical_Specialties__c Includes(\''+ findRequested_Surgical_Specialties+ '\'' +')';
        	IF(findRequired_Credentials !=null&& searchstring =='')searchstring = searchstring + 'Credentials__c Includes(\''+ findRequired_Credentials+ '\'' +')';
                Else IF(findRequired_Credentials !=null&& searchstring != '')searchstring = searchstring + 'AND Credentials__c Includes(\''+ findRequired_Credentials+ '\'' +')';
        	searchstring ='SELECT Name From Contract_Coder__c Where' + searchstring;
        	System.debug(SearchString);
			Database.Query(SearchString);
Here is the Debug Log so you can see the string it is generating

11:57:19:050 USER_DEBUG [26]|DEBUG|SELECT Name From Contract_Coder__c WhereGeneral_Specialties__c Includes('Anesthesiology')
 
Best Answer chosen by Allan Noland
Allan NolandAllan Noland
Nevermind good grief on line 24 i need a space after Where it should read searchstring ='SELECT Name From Contract_Coder__c Where ' + searchstring;

All Answers

Allan NolandAllan Noland
Nevermind good grief on line 24 i need a space after Where it should read searchstring ='SELECT Name From Contract_Coder__c Where ' + searchstring;
This was selected as the best answer
Allan NolandAllan Noland
Literally figured that out just after I had given up and posted the question