I have used the following query in my custom controller, but apex compiler is not allowing this. How can I recreate this query?

Error: Compile Error: expecting a colon, found EventSchedule__c.Name

Integer capacity= [
    SELECT EventSchedule__c.Capacity__c
    FROM EventSchedule__c, Ticket__c
    WHERE Ticket__c.EventSchedule__c = EventSchedule__c.Name
    AND Ticket__c.Date_Time__c=:listStrings.get(i)
    AND Ticket__c.Events__c=:listStrings2.get(j)
  • I believe you need a colon in the first where comparison, Ticket__c.EventSchedule__c =: EventSchedule__c.Name. But I'm testing to confirm.
    – HomerJ
    Commented Nov 24, 2015 at 22:24
  • Apex requires that you surround SOQL and SOSL statements with square brackets in order to use them on the fly. Additionally, Apex script variables and expressions can be used if preceded by a colon (:).
    – HomerJ
    Commented Nov 24, 2015 at 22:45

2 Answers 2


Between objects EventSchedule__c and Ticket__c needs a Lookup relationship or Master-Detail relationship,as Ticket__c.EventSchedule__c(Lookup field) = EventSchedule__c.Id.
Now you can run apex like the following code:

List<Ticket__c> tickets = [
  SELECT EventSchedule__r.Capacity__c
  FROM Ticket__c
  WHERE Ticket__c.Date_Time__c=:listStrings.get(i)
  AND Ticket__c.Events__c=:listStrings2.get(j)
if(!tickets.isEmpty()) {
    Integer capacity = (Integer)tickets[0].EventSchedule__r.Capacity__c;

SOQL fieldExpression Syntax and SOSL WHERE conditionExpression terms can only have this form:

fieldName comparisonOperator value

and value has to be:

A value used to compare with the value in fieldName. You must supply a value whose data type matches the field type of the specified field. You must supply a native value—other field names or calculations are not permitted. If quotes are required (for example, they are not for dates and numbers), use single quotes. Double quotes result in an error.

This means you cannot compare two fields: you can only compare a field with a value that is a constant or a bound external value (using the :).

So you will have to change your approach and inject the name via an Apex bind variable.

  • Question for my own learnification: This applies to SOSL, but does it also apply to SOQL? I rarely have need to use SOSL so I admit I'm not as savy as I probably should be.
    – HomerJ
    Commented Nov 24, 2015 at 22:42
  • 1
    @HomerJ Posted the wrong link; included both now.
    – Keith C
    Commented Nov 24, 2015 at 22:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .