Play Games

Search This Blog

Thursday, March 5, 2020

Retrieving Data from an Apex Controller - Salesforce Globe For You

Retrieving Data from an Apex Controller

To retrieve the string array from an Apex controller, bind the component to the controller. This component retrieves the string array when a button is clicked.

<aura:component controller="namespace.AttributeTypes">
    <aura:attribute name="countries" type="String[]" default="India, USA,Singapore, Germany"/>
    <aura:iteration items="{!v.countries}" var="s">
        {!s}
    </aura:iteration>
    <lightning:button onclick="{!c.getString}" label="Update"/>
</aura:component>






Set the Apex controller to return a List<String> object.



public class AttributeTypes {
    private final String[] arrayItems;
    
 @AuraEnabled
    public static List<String> getStringArray() {
        String[] arrayItems = new String[]{ 'Canada', 'Mexico', 'Austarlia' };
        return arrayItems;
    }

}


This client-side controller retrieves the string array from the Apex controller and displays it using the {!v.countriesexpression.

({
    getString : function(component, event) {
    var action = component.get("c.getStringArray");
     action.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
                var stringItems = response.getReturnValue();
                component.set("v.countries", stringItems);
            }
        });
        $A.enqueueAction(action);
    }
})

To retrieve data from an object that’s returned by an Apex controller, create an attribute with a type corresponding to a standard or custom object.

<aura:attribute name="accounts" type="Account[]"/>

No comments:

Post a Comment