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
Krrish KarthikKrrish Karthik 

System.query Exception

public list<AggregateResult> Agre = new list<AggregateResult>();
Sql = 'SELECT Owner.Name,Avg(Evaluated_Area_1__c)ev1,Avg(Evaluated_Area_2__c)ev2,Avg(Evaluated_Area_3__c)ev3,Avg(Evaluated_Area_4__c)ev4,'
                 +'Avg(Evaluated_Area_5__c)ev5 FROM Survey_Result__c where'
                 +' Survey_Date__c >=:Date.Parse(FromDate) And Survey_Date__c <=:Date.Parse(ToDate) Group by Owner.Name';
         System.debug('qqqqqqqq'+Sql);       
         Sql1 = 'SELECT Avg(Evaluated_Area_1__c)ev1,Avg(Evaluated_Area_2__c)ev2,Avg(Evaluated_Area_3__c)ev3,Avg(Evaluated_Area_4__c)ev4,'
                 +'Avg(Evaluated_Area_5__c)ev5 FROM Survey_Result__c where '
                 +'Survey_Date__c >=:Date.Parse(FromDate) And Survey_Date__c <=:Date.Parse(ToDate)';
         System.debug('xxxxxxx'+Sql1);       
       
         if(alUsr.Checked){       
             Agre = Database.query(Sql);
             System.debug('sssssssss'+Agre);
         }
         else if(alUsr.Checked1){       
             Agre = Database.query(Sql1);
             System.debug('vvvvvvv'+Agre);
         }


i got an Error  System.QueryException: unexpected token: '('
Ashish_SFDCAshish_SFDC
Hi Karthik,


Looks like its expecting something else as per the syntax or the attributes.

Which line and keyword is it mentioning in the error?

SOQL should be wrapped under the square brackets [] 

Try this, 


public list<AggregateResult> Agre = new list<AggregateResult>();


Sql = ['SELECT Owner.Name,Avg(Evaluated_Area_1__c)ev1,Avg(Evaluated_Area_2__c)ev2,Avg(Evaluated_Area_3__c)ev3,Avg(Evaluated_Area_4__c)ev4,'
                 +'Avg(Evaluated_Area_5__c)ev5 FROM Survey_Result__c where'
                 +' Survey_Date__c >=:Date.Parse(FromDate) And Survey_Date__c <=:Date.Parse(ToDate) Group by Owner.Name'];
         System.debug('qqqqqqqq'+Sql);      
         Sql1 = ['SELECT Avg(Evaluated_Area_1__c)ev1,Avg(Evaluated_Area_2__c)ev2,Avg(Evaluated_Area_3__c)ev3,Avg(Evaluated_Area_4__c)ev4,'
                 +'Avg(Evaluated_Area_5__c)ev5 FROM Survey_Result__c where '
                 +'Survey_Date__c >=:Date.Parse(FromDate) And Survey_Date__c <=:Date.Parse(ToDate)'];
         System.debug('xxxxxxx'+Sql1);      
      
         if(alUsr.Checked){      
             Agre = Database.query(Sql);
             System.debug('sssssssss'+Agre);
         }
         else if(alUsr.Checked1){      
             Agre = Database.query(Sql1);
             System.debug('vvvvvvv'+Agre);
         }




Regards,
Ashish
Krrish KarthikKrrish Karthik
This Line is Error Ashish
Agre = Database.query(Sql);
Agre = Database.query(Sql1);

Krrish KarthikKrrish Karthik
thanks Ashish.. 
Ashish_SFDCAshish_SFDC

Hi Krish, 


Sql will get only one record in it, i think you should use a list instead of a Single Object. 

// Create an empty list of String
List<String> my_list = new List<String>();

String should contain the SObject name. 

Remove the single quotes in the SOQL Statement as single quotes will make the items listed inside it as String. 

// Create a list of account records from a SOQL query
List<Account> accs = [SELECT Id, Name FROM Account WHERE Name = 'Siebel'];


https://www.salesforce.com/us/developer/docs/apexcode/Content/langCon_apex_collections_lists.htm

http://www.salesforce.com/us/developer/docs/apexcode/Content/langCon_apex_loops_for_SOQL.htm


Regards,

Ashish