I have a structure question today regarding a dynamic SOQL query that I seem to be entering the wrong custom object id
value for in the WHERE
clause.
I have retrieved the custom object id
from my SelectOption
list object on a button submission. I then want to add this in to my SOQL query to filter further my search.
- My list of
SelectOption
instances are formed of id and name values respectively for value and label formed by retrieving and looping through a custom object namedProject
. - Project__c is a lookup field.
Dynamic SOQL Apex function:
public String selectedProjectId { get; set; }
public String returnDynamicSOQL(string customObjectName)
{
string dynamicSOQL = 'SELECT Id, Name, ' + customObjectName + '_Name__c, Date_Registered__c, Status__c, Originator__c, Owner__c, ' + customObjectName + '_Type__c, Project__c FROM ' + customObjectName + '__c ';
if (projectIsSelected)
{
dynamicSOQL += ('WHERE Project__c.Id = ' + selectedProjectId);
}
return dynamicSOQL;
}
I am receiving this error:
Visualforce Error
System.QueryException: expecting a colon, found 'a4S24000000aPDfEAM' Class.RAIDController.getRAIDObjects: line 59, column 1 Class.RAIDController.getRisks: line 71, column 1
So I have the id
for the project custom object selected at least, am I wrong in thinking this is what I need to retrieve the exact project by id
? Does the WHERE
clause need me to specify Project__c.Id or can I just use Project__c?
Any help much appreciated!