+ Start a Discussion
dai tran 6dai tran 6 

Why can't set to list Product?

I want distinct value of Family:
List<Product2> res= [select Family From Product2  where Family !='None' GROUP BY Family];

But it show error:
Illegal assignment from List<AggregateResult> to List<Product2>

Best Answer chosen by dai tran 6
Shruti SShruti S
To get distinct family values, you should be using the code below : 
List<AggregateResult> products = [SELECT Family FROM Product2 GROUP BY Family];

List<String> uniqFamilies = new List<String>();
for( AggregateResult agr : products ) {
    String family = String.valueOf( agr.get( 'Family' ) );
    
 uniqFamilies.add( family );
}

All Answers

Shruti SShruti S
Please use the below code : 
List<AggregateResult> res= [select Family From Product2  where Family !='None' GROUP BY Family];
Shruti SShruti S
To get distinct family values, you should be using the code below : 
List<AggregateResult> products = [SELECT Family FROM Product2 GROUP BY Family];

List<String> uniqFamilies = new List<String>();
for( AggregateResult agr : products ) {
    String family = String.valueOf( agr.get( 'Family' ) );
    
 uniqFamilies.add( family );
}
This was selected as the best answer