0

I am trying to execute following apex class , But facing the following error Incompatible types since an instance of List is never an instance of AggregateResult.

I am using the below code:

public with sharing class Eisai_Banzel_Survey_Cls{

    public list<Schema.AggregateResult> listSurvey = new list<Schema.AggregateResult>();
    private static String UserRoleId = Userinfo.getUserRoleId();
    List<UserRole> listRole = new List<UserRole>();
    List<String> listRoleIds = new List<String>();

    public list<Survey_Target_vod__c> getSurveylist(){

        listRoleIds.add(UserRoleId );

        Set<Id> allSubRoleIds = getAllSubRoleIds(new Set<ID>{UserRoleId});

        //Get all the Surveys to be displayed that would be visible to the user as per role hierarchy and sorted according to terriotry

        listSurvey = [SELECT Status_vod__c, Territory_vod__c,Count(Id), User_vod__r.Name,User_vod__r.UserRoleId, User_vod__r.IsActive 
                      FROM Survey_Target_vod__c 
                      WHERE CreatedDate = THIS_YEAR 
                      AND (OwnerId =:Userinfo.getUserId() OR User_vod__r.UserRoleId IN :allSubRoleIds ) 
                      AND User_vod__r.IsActive = TRUE
                      GROUP BY Status_vod__c,Territory_vod__c,User_vod__r.Name,User_vod__r.UserRoleId, User_vod__r.IsActive
                      ORDER BY Territory_vod__c ASC NULLS FIRST];


        if(listSurvey.size()>0){
            return listSurvey;
        }        

        else return null;

        return null;
    }

    private static Set<ID> getAllSubRoleIds(Set<ID> roleIds) {

        Set<ID> currentRoleIds = new Set<ID>();

        // get all of the roles underneath the passed roles
        for(UserRole userRole :[select Id from UserRole where ParentRoleId 
                                IN :roleIds AND ParentRoleID != null])
            currentRoleIds.add(userRole.Id);

        // go fetch some more rolls!
        if(currentRoleIds.size() > 0)
            currentRoleIds.addAll(getAllSubRoleIds(currentRoleIds));

        return currentRoleIds;

    }

}

Facing the error in return listSurvey; :

if(listSurvey.size()>0){
    return listSurvey;
}  
4

When you execute a query that includes a GROUP BY term it is always an array (list) of AggregateResult objects that is returned - see Working with SOQL Aggregate Functions.

So your method will have to return that type:

public AggregateResult[] getSurveylist() {
    ...
    return [SELECT Status_vod__c s, Territory_vod__c t,Count(Id) c, User_vod__r.Name n,User_vod__r.UserRoleId r, User_vod__r.IsActive a
                  FROM Survey_Target_vod__c 
                  WHERE CreatedDate = THIS_YEAR 
                  AND (OwnerId =:Userinfo.getUserId() OR User_vod__r.UserRoleId IN :allSubRoleIds ) 
                  AND User_vod__r.IsActive = TRUE
                  GROUP BY Status_vod__c,Territory_vod__c,User_vod__r.Name,User_vod__r.UserRoleId, User_vod__r.IsActive
                  ORDER BY Territory_vod__c ASC NULLS FIRST];
}

and any other code using the returned result will have to access the values using get(fieldName).

So to get the various values in Apex (where I've added an alias in the above query for each field):

for (AggregateResult ar: getSurveylist()) {
    String status = (String) ar.get('s');
    String territory = (String) ar.get('t');
    Integer count = (Integer) ar.get('c');
    ...
}
| improve this answer | |
0

As far as I can tell, you are supposed to be returning

list<Survey_Target_vod__c>

but you are actually returning

list<Schema.AggregateResult>

So you'll need to make them compatible...

| improve this answer | |
  • When using list<Survey_Target_vod__c> I get, the following error : Illegal assignment from List<AggregateResult>to List <Survey_Target_vod__c> – Salesforce Developer Apr 12 '16 at 17:49
  • When using list<Survey_Target_vod__c> I get, the following error : Illegal assignment from List<AggregateResult>to List <Survey_Target_vod__c>. As I am using the Aggregate method, the return type is not supported as per error. So I am making this change. Please let me know if there is anything that I am missing here. Thanks! – Salesforce Developer Apr 12 '16 at 17:56

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.