Having COUNT_DISTINCT() is a great advantage in apex over standard Salesforce reporting capabilities. Still what you need to remember is that it doesn't return Integer (as COUNT() is doing), but List<AggregateResult> with count stored in the expr0 attribute of the first element of the list.
So while the following code is valid:
The following code will return a list:
What you need to do is take the count from the list, for example with the following code:
So while the following code is valid:
public String getObjectsForSelectedApplication() {
return String.valueOf([SELECT COUNT()
FROM MyObjects__c WHERE Application__c = :selectedApplication]);
}
The following code will return a list:
public String get UniqueUsersForSelectedApplication() {
return String.valueOf([SELECT COUNT_DISTINCT(User__c)
FROM MyObjects__c WHERE Application__c = :selectedApplication]);
}
What you need to do is take the count from the list, for example with the following code:
public String getUniqueUsersForSelectedApplication() {
List<AggregateResult> result = [SELECT COUNT_DISTINCT(User__c)
FROM MyObjects__c WHERE Application__c = :selectedApplication];
if (result.isEmpty()) {
return '0';
} else {
return String.valueOf(result.get(0).get('expr0'));
}
}
Comments
Post a Comment