I have defined a method in the apex controller like below, I don't get any errors, but when loading the page it says ERROR

Here is the apex method:

public class Address_Penetration_ApexController {  
    public static integer getAddress_UnitCount() {       
        List<AggregateResult> count = [select COUNT(Id) from Address__c where Name like '%13750 100 Ave%'];
        return Integer.valueOf(count);

Here is the JS Controller

    fetchAddress : function(component, event, helper) {
        var serverCall = component.get("c.getAddress_UnitCount");
        serverCall.setCallback(this, function(response){
            var state = response.getState();
            if(state === "SUCCESS"){
                var count = response.getReturnValue();
                component.set("v.totalUnits", count);    

1 Answer 1


The AggregateResult is not an Integer. Use COUNT() instead:

public static integer getAddress_UnitCount() {       
    return [select COUNT() from Address__c where Name like '%13750 100 Ave%'];

Note you don't need the temporary variable or Integer.valueOf.

P.S. You should always use curly brackets, even if you don't need them. It makes your code more legible.

        }else {
  • Thanks @sfdcfox
    – sumchans
    Dec 3, 2020 at 18:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .