0

Just looking for a simple example of an apex class that can get a field value of a custom object so that the value can be returned into a variable on a Lightning Component.

Apex Class

    public class Flow_Taffic_Log {
    @AuraEnabled
    public static Traffic_Log__c get_doorSwing() {
        return [SELECT Door_Swing__c FROM Traffic_Log__c WHERE Day_of_Year__c = '283' AND Year__c = '2019' LIMIT 1];
    }
}

Controller

updateGreetings: function(component, event)
      {
        alert("hello world");
        var theresult = component.get('c.get_doorSwing');
        component.set('v.doorSwing', theresult);
        alert(theresult);
      }

The alert I'm getting is

SecureAction: [object Object]{ key: {"namespace":"c"} }

0

1 Answer 1

1

When calling an Apex server-side controller, you're required to enqueue an asynchronous action, with a callback to consume the result. In the code that you shared, theresult is not the result of the Apex call, but a reference to the action that your JavaScript controller may enqueue.

From the documentation, here's a schematic and rather verbose example of how that works:

({
    "echo" : function(cmp) {
        // create a one-time use instance of the serverEcho action
        // in the server-side controller
        var action = cmp.get("c.serverEcho");
        action.setParams({ firstName : cmp.get("v.firstName") });

        // Create a callback that is executed after 
        // the server-side action returns
        action.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
                // Alert the user with the value returned 
                // from the server
                alert("From server: " + response.getReturnValue());

                // You would typically fire a event here to trigger 
                // client-side notification that the server-side 
                // action is complete
            }
            else if (state === "INCOMPLETE") {
                // do something
            }
            else if (state === "ERROR") {
                var errors = response.getError();
                if (errors) {
                    if (errors[0] && errors[0].message) {
                        console.log("Error message: " + 
                                 errors[0].message);
                    }
                } else {
                    console.log("Unknown error");
                }
            }
        });

        // optionally set storable, abortable, background flag here

        // A client-side action could cause multiple events, 
        // which could trigger other events and 
        // other server-side action calls.
        // $A.enqueueAction adds the server-side action to the queue.
        $A.enqueueAction(action);
    }
}) 

In your code, c.get_doorSwing corresponds to c.serverEcho in the example; it's a reference to the server action. The example includes both (a) the callback function you'll have to implement to consume the result and (b) the critical synchronous call to $A.enqueueAction(). If you don't make that call, your server action will never be executed.

1
  • Fantastic. I appreciate the explanation
    – Stvvn
    Commented Oct 10, 2019 at 19:50

You must log in to answer this question.

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