2

I'm passing a list to a serialize JSON string value back to a Visualforce page. Traditionally I can easily pass back a List to the JSON.

However, my SOQL is an aggregate. I need to be able to serialize this aggregate result. That may require converting that SOQL to a List. I'm unsure how to do that. I've tried iterating through but that requires adding "new" objects to and I'm not sure how to construct those new objects with all of the fields I need.

public Activity__c[] getActs(){
        setVar();
        return myActs;
    }

    public void setVar(){
        string soql = 'select max(Activity_Time_Stamp__c), max(Activity_Date__c), min(id), min(Task_Site__c), min(GeoCode__Latitude__s), min(GeoCode__Longitude__s), workday__r.owner.name' + 
                      ' from Activity__c ' + 
                      ' where GeoCode__Latitude__s != null ' + 
                      ' group by workday__r.owner.name';
        for (AggregateResult ar: [select max(Activity_Time_Stamp__c), max(Activity_Date__c), min(id), min(Task_Site__c), min(GeoCode__Latitude__s), min(GeoCode__Longitude__s), workday__r.owner.name 
                                 from Activity__c  
                                  where GeoCode__Latitude__s != null  
                                  group by workday__r.owner.name]
            ){
                myActs.add(new Activity__c('****WHAT DO I PUT HERE****  If this is the correct method'));
            }

        myActs = database.query(soql);        
    }
  • The loop does not seem relevant to the query at all? Can you please edit your post with a more clear question? – Adrian Larson Jul 8 '16 at 16:42
  • You can create a wrapper and add results coming from aggregate queries into that list and serialize or directly use that on page. Update if you need more assistance on this. – Kiran Machhewar Jul 8 '16 at 16:45
  • 1
    @AdrianLarson will do. Sorry for the ambiguity. I have a meeting. Will be updated in a few hours. – tjcinnamon Jul 8 '16 at 17:41
4

Just use the AggregateResult directly. If you decide to serialize it, your data would look like this:

[
   {
      "attributes": {
         "type": "AggregateResult"
      },
      "Name": "Account 0"
   },
   {
      "attributes": {
         "type": "AggregateResult"
      },
      "Name": "Account 1"
   }
]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.