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 

Invalid field CampaignName__c for AggregateResult

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() {       
              system.debug('$$$$$:'+selectedval);
            
              system.debug(mappingval.get(selectedval));
               try {
                if(selectedval != null) {
                    CampaignMember = [select Id, owner_Name__c, CurrencyIsoCode, status from
                        Campaignmember where owner_Name__c =: mappingval.get(selectedval)limit 99];
                    }else
                    
                    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

Best Answer chosen by Admin (Salesforce Developers) 
ckellieckellie

Thank yoyu for your help Anit. The below has solved my error:

 

    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');
            mappingcval.put(aID, aID);
            }
        }
                
        return optionList;

 

All Answers

Ankit AroraAnkit Arora

The problem is here :

 

AggregateResult[] groupedResults = [select Campaign_Name__c from campaignmember where
                 ownername__c=:selectedval group by Campaign_Name__c];

 You are fetching "CampaignName__c" from aggregate result without querying it in aggregate query.

 

Just change your query like this :

 

AggregateResult[] groupedResults = [select Campaign_Name__c , CampaignName__c from campaignmember where ownername__c=:selectedval group by Campaign_Name__c];

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page

ckellieckellie

Thank you for your reply.

I have replaced the code as recommended

AggregateResult[] groupedResults = [select Campaign_Name__c , CampaignName__c from campaignmember where ownername__c=:selectedval group by Campaign_Name__c];

 

and recieve the following error:

 

Compile Error: Field must be grouped or aggregated: CampaignName__c

 

I have tried to group by both fields

 

AggregateResult[] groupedResults = [select Campaign_Name__c , CampaignName__c from campaignmember where 
                        ownername__c=:selectedval group by Campaign_Name__c, CampaignName__c];

  and recieve the following visualforce error

 

System.UnexpectedException: field 'CampaignName__c' can not be grouped in a query call 

 Campaign_Name__c is a text field and CampaignName__c is a formula field.

Ankit AroraAnkit Arora

Not sure as these fields are your custom fields and don't even know the data type of them. So lets go with the error

 

Try this :

 

AggregateResult[] groupedResults = [select COUNT(Campaign_Name__c) , CampaignName__c from campaignmember where ownername__c=:selectedval group by Campaign_Name__c];

 You are not using any aggregate function, that might be a problem. Worth a shot.

 

Let me know if you still face any problem.

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page

ckellieckellie

Thank you for the code, but this code:

 

AggregateResult[] groupedResults = [select COUNT(Campaign_Name__c) , CampaignName__c from campaignmember where ownername__c=:selectedval group by Campaign_Name__c];

 

Is returning the following error:

Grouped field should not be aggregated: Campaign_Name__c

 

Ankit AroraAnkit Arora

Try any of the below, whichever suits you :

 

AggregateResult[] groupedResults = [select COUNT(Campaign_Name__c) , CampaignName__c from campaignmember where ownername__c=:selectedval group by CampaignName__c];
AggregateResult[] groupedResults = [select Campaign_Name__c , COUNT(CampaignName__c) from campaignmember where ownername__c=:selectedval group by Campaign_Name__c];

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page

ckellieckellie

Thank yoyu for your help Anit. The below has solved my error:

 

    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');
            mappingcval.put(aID, aID);
            }
        }
                
        return optionList;

 

This was selected as the best answer
deveshranjan.sinha1.3002556334deveshranjan.sinha1.3002556334

How you got "groupedResults".

 

select count(Id),AccountId from case where Case.IsClosed = false GROUP BY AccountId

 

throwing error:

 

 (279728000)|EXCEPTION_THROWN|[11]|System.UnexpectedException: Field must be grouped or aggregated: AccountId

ckellieckellie

I have morphed this code since the solution and here is the final code I am using:

 

    public List <SelectOption> getcampaignlists(){
system.debug('SFDC TEST ************ OWNER' + Owner);
system.debug('SFDC TEST1 *************' + selectedVal);

            AggregateResult[] groupedResults = [select Campaignid , Campaign_Name__c from campaignmember where 
                        ownername__c=:selectedval group by Campaignid, Campaign_Name__c ];
        List <SelectOption> optionList = new list<SelectOption>();
        optionList.add(new SelectOption( '1', 'SELECT' ));
        system.debug('SFDC TEST2 *************' + selectedVal);
        if(selectedcval!= 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');
            mappingcval.put(aID, aID);
            }
        }
        return optionList;
    }

 Hope this helps you.

harshasfdcharshasfdc

Hi all ,

 

iam getting the following error in soql query.can any one please help me out 

 

here is the code

public class ag1
{
public integer Ont1{get;set;}
public integer T1{get;set;}
public string s1{get;set;}
public list<AggregateResult> lstar=new list<AggregateResult>();

public ag1()
{
lstar=[select count(id),sum(OnTherapyCurrWk__c) s1,sum(TreatmentGapCurrWk__c)s2,sum(OrderPendingCurrWk__c)s3,sum(EIPCurrWk__c)s4,sum(PreStartCurrWk__c)s5,ProductID__c from ProdTerrData__c group by ProductID__c];

}
public list<OppClass> getResults()
{
list<OppClass> lstResult = new list<OppClass>();
for (AggregateResult ar: lstar)
{
oppClass objOppClass = new oppClass(ar);
lstResult.add(objOppClass);
}
return lstResult;
}

class oppClass
{
public integer onterapaycurrwk { get;set; }


public integer TreatmentGapCurrWk { get;set; }

public integer OrderPendingCurrWk{get;set;}

public integer EIPCurrWk{get;set;}

public integer PreStartCurrWk{get;set;}

public oppClass(AggregateResult ar)
{
onterapaycurrwk =integer.valueOf(ar.get('s1'));
TreatmentGapCurrWk = integer.valueOf(ar.get('s2'));
OrderPendingCurrWk=integer.valueof(ar.get('s3'));
EIPCurrWk=integer.valueof(ar.get('s4'));
PreStartCurrWk=integer.valueof(ar.get('s5'));
}
}
}