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
ckellieckellie 

AggregateResult Error

The below code is driving two dependant picklists with the second picklist being an aggregated list of campaign names.

 

Objective:

Use the aggregatedresult functionality to group campaign names by the related campaignmember.owner. Apex coding is the only way  I can achieve the intended results, using dependant upicklist fields in the UI will not meet my business requirements.

 

In trying to incorporate the aggregate / group by functionality, I am recieving the following error:

 

 

Compile Error: Invalid field campaignid for SObject AggregateResult 

 

 

Here is my code:

 

 

public with sharing class CampaignListOwner2{

    public String campaignstatus { get; set; }

    public transient string campaignlists;
    Id campaignid;
    public string s;
    
 
   /**
    *Refresh campaign list
    *@return page reference
    */
    
   
    public PageReference campaignRefresh(){
        
        return null;
    }
    

    public String getString() {
        return s;
    }
            
    public void setString(String s) {
        this.s = s;
    }

    
    


public List<SelectOption> getselectedOwner(){
        List<SelectOption> selectedOwnerList = new List<SelectOption>();
        selectedOwnerList.add(new SelectOption( '1', 'SELECT' ));
        for(User u:[select id,name from user where id in (select ownername__c from campaignmember) order by name]) {
        selectedOwnerList.add(new SelectOption(u.id, u.name));
        System.debug('************* selectedOwner id : ' + u.id);
    }
        system.debug('^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^' + selectedOwnerList);
        return selectedOwnerList;
    }

public List <SelectOption> getcampaignlists(){
    system.debug('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%' + s);
    
    AggregateResult[]groupedResults = [select
       campaignid from campaignmember where ownername__c=:s group by campaignid];
    
    List <SelectOption> campaignLists = new list<SelectOption>();
    campaignLists.add(new SelectOption( '1', 'SELECT' ));
    
    for(Aggregateresult ar :groupedResults) {
        campaignlists.add(new SelectOption(ar.campaignid, ar.name));

    }
    return campaignlists;
            
    }
}

 

 

How do I solve this error by adding the required fields to the aggregateresult sobject? If i have hit a dead end on this code. What is another way to achieve the same objective of tabulating a grouped list of campaign names driven by the campaignmember owner?

 

Thank you

Best Answer chosen by Admin (Salesforce Developers) 
Devendra NataniDevendra Natani

 

public List <SelectOption> getcampaignlists(){
    
AggregateResult[] groupedResults = [select  campaign.name name, campaignid from campaignmember where ownername__c=:s group by campaignid,campaign.name];
   
List <SelectOption> campaignLists = new list<SelectOption>();
campaignLists.add(new SelectOption( '1', 'SELECT' ));
    
for(Aggregateresult ar :groupedResults) {
        campaignlists.add(new SelectOption(String.valueof( ar.get('campaignid')), String.valueof(ar.get('name'))));

    }
  

    return campaignlists;
            
}

 

I have changed the query little bit and used alias in it. You can take the idea from that and can write a query in a similar way. To fetch the data from aggregateResult, I used 

ar.get('name')

 

 

Please let me know if there is any issue.

 

Thanks,

Devendra Natani (Salesforce Certified Developer)

 

Blog

All Answers

shruthishruthi

I had faced a similar issue and the following method worked for me. Hope it helps you as well! :)

 

public List <SelectOption> getcampaignlists(){
    system.debug('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%' + s);
   
    AggregateResult[]groupedResults = [select
       campaignid from campaignmember where ownername__c=:s group by campaignid];
   
    List <SelectOption> campaignLists = new list<SelectOption>();
    campaignLists.add(new SelectOption( '1', 'SELECT' ));
   

Integer i = 0;

while(i<groupedResults.size()) {

    campaignlists.add(new SelectOption(groupedResults[i].get('campaignid'), groupedResults[i].get('name')));

    i++;

}
    return campaignlists;
           
    }
}

 

Cheers!

Devendra NataniDevendra Natani

 

public List <SelectOption> getcampaignlists(){
    
AggregateResult[] groupedResults = [select  campaign.name name, campaignid from campaignmember where ownername__c=:s group by campaignid,campaign.name];
   
List <SelectOption> campaignLists = new list<SelectOption>();
campaignLists.add(new SelectOption( '1', 'SELECT' ));
    
for(Aggregateresult ar :groupedResults) {
        campaignlists.add(new SelectOption(String.valueof( ar.get('campaignid')), String.valueof(ar.get('name'))));

    }
  

    return campaignlists;
            
}

 

I have changed the query little bit and used alias in it. You can take the idea from that and can write a query in a similar way. To fetch the data from aggregateResult, I used 

ar.get('name')

 

 

Please let me know if there is any issue.

 

Thanks,

Devendra Natani (Salesforce Certified Developer)

 

Blog

This was selected as the best answer
The KnightThe Knight

It works as Shruthi mentioned.

The reason for this is .

AggregateResult is an Sobject. You cannot directly use obj.fieldname.

The only way is to use Sobject's getter method Sob.get('fieldname').  

ckellieckellie

Thank you all three for your input to this solution. i have been able to solve the error as noted below. I have been working on this for months without success. Major kuddos.

 

Thank you,

 

ckellie