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
Nick StebnerNick Stebner 

System.QueryException: unexpected token:

Hello, and thanks in advance for any responses!

I am trying to return a list of records from another custom object (Values_dreams_Goals) for display on my current custom object (VDG_Display).  The code below has been adapted from another application in the same org that works just fine, but returns the query error below.

System.QueryException: unexpected token: SELECTValues_Dreams_Goals__c 
Class.GoalListExt.getGoalItems: line 25, column 1

My offending class is below.  Line 25 is marked, but I may have an error in the query language itself, since I'm somewhat unfamiliar with it.

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
public class GoalListExt {

    
    public id householdId {get;set;}
    public VDG_Display__c thisDisplay {get;set;}
    
    public list<Values_Dreams_Goals__c> GoalItems;
    public integer QUERY_RECORD_LIMIT = 25;
    
    public GoalListExt() {
   
    }
    
    
    public list<Values_Dreams_Goals__c> getGoalItems()
    {
    
     string parentID = householdId;
     string query = 'SELECT';
     for(Schema.FieldSetMember f : this.getGoalItemsFields())
     {
         query += f.getFieldPath() + ',' ;
     }
     query += 'Id FROM Values_Dreams_Goals__c WHERE Household_linked__c =: parentID limit:QUERY_RECORD_LIMIT';
line 25 -->     GoalItems = (list<Values_Dreams_Goals__c>)  Database.query(query);
     return GoalItems;
     }
    
    public List<Schema.FieldSetMember> getGoalItemsFields() {
        return SObjectType.Values_Dreams_Goals__c.FieldSets.VDGTable.getFields();
    }
}
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Any help would be appreciated.
Thanks in advance.

Nick 
 
matt.ian.thomasmatt.ian.thomas
When you first instantiate the variable query, you need to set it to 'SELECT ', notice the space after SELECT. Your query string is being built with no space after select, so your query will begin "SELECTFirstField..." and then it freaks out.

About line 25: You don't need to downcast Database.query(query) to a List<Values_Dreams_Goals__c> because Values_Dreams_Goals__c is in your FROM clause; this is redundant.

Also, check out SoqlBuilder from apex-lang: http://apex-commons.github.io/query/soql-builder/ using this library can remove silly syntax errors like this, since you'll no longer need to worry about them.

Check out the following example method your query in the above code could become in some other class (let's say, "ValuesDreamsGoalsService.cls" if you were using SoqlBuilder:
public static List<Values_Dreams_Goals__c> getValuesDreamsGoals(Id parentId, List<String> fields) {
	String soql = new SoqlBuilder()
					  .selectx(fields)
					  .fromx('Values_Dreams_Goals__c')
					  .wherex(new FieldCondition('Household_Linked__c').equals(parentId))
					  .toSoql();
	return Database.query(soql);
}

Then in your code, you would simply write:
GoalItems = ValuesDreamsGoalsService.getValuesDreamsGoals(parentId, this.getGoalItemsFields());
matt.ian.thomasmatt.ian.thomas
Just note that in the above example, you'd need to convert "this.getGoalItemsFields()" to a list of Strings for it to work properly.
Nick StebnerNick Stebner
Matt--

Thanks so much.  That worked perfectly.

Nick