+ Start a Discussion
mobile vistexmobile vistex 

Ordered field must be grouped or aggregated

hi iam trying to group name  and order by period but giving above error how can i do that
my query is:
list<Agreement1__c> al = [select Name,driver__C,period__C,sales__c,quantity__c from Agreement1__C group By Name order by period__c ];
please tell me how can delete that error
Darshan Shah2Darshan Shah2
Hi mobile vistex,

Make some change as shown below as underline, and let me know whether it solves your problem.

list<Agreement1__c> al = [select Name,driver__C,period__C,sales__c,quantity__c fromAgreement1__C group By Name, driver__C, period__C, sales__c, quantity__c order by period__c ];

Warm Regards,
Darshan Shah
mobile vistexmobile vistex
thanks for your reply 
  bt it is giving an error like  (Illegal assignment from List<AggregateResult> to List<gtms1__Agreement1__c>);
Darshan Shah2Darshan Shah2
Hello Mobile Vistex,

This query will return AggregateResult. you need to get values from that.
So change query as below and let me know whether it works for you or not.

List<AggregateResult> lstAggregateResult = [select Name, driver__C, period__C, sales__c, quantity__c fromAgreement1__C group By Name, driver__C, period__C, sales__c, quantity__c order by period__c ];

for (AggregateResult ar : lstAggregateResult)
    /*Get values from AggregateResult*/
      system.debug('===== Name:'+ar.get('Name'));
      system.debug('===== sales__c:'+ar.get('sales__c'));

Warm Regards,
Darshan Shah
Darshan Shah2Darshan Shah2
Hi Mobile Vistex,

Kindly let me know whether above code is working or not.
If working then mark this question as SOLVED so other people can get help from this :)