• deveshranjan.sinha1.3002556334
  • 0 Points
  • Member since 2011

  • Chatter
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
  • 2

I am using the aggregatere result function to a second, dependant picklist and am trying to assign the second picklist to the recorrd. I am trying to attach the aggregated result value to a mapping value.


Here is the error message:

System.SObjectException: Invalid field CampaignName__c for AggregateResult

Class.CampaignListOwner7.getcampaignlists: line 61, column 35 External entry point 

Class code:


public with sharing class CampaignListOwner7{
List<SelectOption> selectedOwnerList = new List<SelectOption>();
Map<id, String> mappingval = new Map<id, String>();
Map<id, String> mappingcval = new Map<id, String>();
public String selectedval { get;  set;}
public String selectedcval { get;  set;}
public String cval { get;  set;}

    public CampaignListOwner7(ApexPages.StandardController controller) {

    public String getCampaign() {
        return this.Campaign;
    public String getCampaignStatus() {
        return this.CampaignStatus;

    public string s;
    public String campaign;
    public string campaignstatus;
    public transient string               Campaignid{ get; set;}
    private transient CampaignMember[]    members; //the members for that campaign
    public transient String               owner; //owner ID

    public string getOwner() {
        return this.owner;
    public void setOwner(String s) {
 this.Owner = s; }

  public List<CampaignMember> CampaignMember { get;  set;}
    public List<SelectOption> getselectedOwner(){
         List<SelectOption> optionList = new List<SelectOption>();
            optionList.add(new SelectOption( '1', 'SELECT' ));
            for(User u:[select id,name from user where id in (select ownername__c from campaignmember) order by name]) {
            optionList.add(new SelectOption(u.id, u.name));
            mappingval.put(u.id, u.name);
            return optionList;
    public List <SelectOption> getcampaignlists(){
system.debug('SFDC TEST ************ OWNER' + Owner);
        AggregateResult[]groupedResults = [select Campaign_Name__c from campaignmember where
                 ownername__c=:selectedval group by Campaign_Name__c];
        List <SelectOption> optionList = new list<SelectOption>();
        optionList.add(new SelectOption( '1', 'SELECT' ));
        system.debug('SFDC TEST *************' + selectedVal);

        if(selectedval!= null){
    for(Aggregateresult ar :groupedResults) {
            optionList.add(new SelectOption(String.valueof( ar.get('Campaign_Name__c')), String.valueof(ar.get('Campaign_Name__c'))));
            String aID = (string)ar.get('Campaign_Name__c');
            String abID = (string)ar.get('CampaignName__c');
            mappingcval.put(abID, aID);
        return optionList;
    public PageReference campaignRefresh() {       
               try {
                if(selectedval != null) {
                    CampaignMember = [select Id, owner_Name__c, CurrencyIsoCode, status from
                        Campaignmember where owner_Name__c =: mappingval.get(selectedval)limit 99];
                    CampaignMember = [select Id, owner_Name__c, Campaign_Name__c, status, CurrencyIsoCode from
                        Campaignmember where owner_Name__c =: mappingval.get(selectedval) 
                        and Campaign_Name__c =: mappingcval.get(selectedcval) limit 99];
               }catch( Exception e ){
                ApexPages.addMessages( e );
             system.debug('##################' + CampaignMember);
             return null;


How do I put the aggregate result to the mappingcval?


Thank you

Could someone please help me with this error that is driving me crazy. I have Apex code that is schedule to run once a day, but errors due to my soql query with this message:


"caused by: System.UnexpectedException: Field must be grouped or aggregated: pkl_Year__c"


The code is:



global class ProgramSnapshot implements Database.Batchable<sObject>{
    String query = 'Select SUM(num_3rdParty_Volume__c) num_sum3rd, SUM(num_Customer_Volume__c) num_sumCust, SUM(num_Our_Volume__c) num_sumOur, pkl_year__c FROM Program_Volume__c GROUP BY pkl_year__c LIMIT 100';    
    global database.querylocator start(Database.BatchableContext BC){
        return Database.getQueryLocator(query);
    global void execute(Database.BatchableContext BC, List<sObject> scope){
        List<zzTest_DW__c> test_DWs = new List<zzTest_DW__c>();
        for(SObject s : scope){
            zzTest_DW__c a = new zzTest_DW__c();
            a.num_3rdParty_Volume__c = Integer.valueOf(String.valueOf(s.get('num_sum3rd')));
            a.num_Customer_Volume__c = Integer.valueOf(String.valueOf(s.get('num_sumCust')));
            a.num_Our_Volume__c = Integer.valueOf(String.valueOf(s.get('num_sumOur')));
            a.txt_GroupName__c = 'Test Group';
            a.txt_Model__c = 'Test Model';
            a.txt_ModelCode__c = 'Test Model Code';
            a.txt_Platform__c = 'Test Platform';
            a.txt_Program_Name__c = 'Test Program Name';
            a.txt_Year__c = String.valueOf(s.get('pkl_year__c'));
            a.date_Snapshot__c = Date.Today();
        insert test_DWs;
    global void finish(Database.BatchableContext BC){



If I try to aggregate the pkl_year__c field, by doing a MAX(pkl_year__c), then I get an error of:


"System.QueryException: Grouped field should not be aggregated: pkl_Year__c"


What am I doing wrong??

  • March 10, 2011
  • Like
  • 0