0

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 {  
    @AuraEnabled
    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();
                console.log(count);
                component.set("v.totalUnits", count);    
            }else
                alert(state);        
        });
        $A.enqueueAction(serverCall);
    }
})

1 Answer 1

2

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

@AuraEnabled
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 {
            alert(state);
        }
1
  • 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 .