0
var action = component.get("c.getCheckType"); 
action.setParams({"recordId": recordId});

I am trying to call a funtion from Helper class of an Aura controller to apex controller and I want to get the returned value by method(A string returned by the Apex controller) . How can i access it in helper class?

1 Answer 1

0

You use setCallback to get the value back. I typically wrap this in a Promise to make it easier to use back in the controller.

// Helper
callServer: function(component, event, helper) {
  return new Promise((resolve, reject) => {
    const action = component.get("c.getCheckType");
    const recordId = component.get("v.recordId");
    action.setParams({ recordId });
    action.setCallback(this, (response) => {
      const state = response.getState();
      if(state === 'SUCCESS') {
        resolve(response.getReturnValue());
      } else if(state === 'ERROR') {
        reject(response.getError());
      }
    });
    $A.enqueueAction(action);
  });
}

You call this method as follows:

actionMethod: function(component, event, helper) {
  helper.callServer(component, event, helper).then(
    (result) => {
      // This is your string
    }
  ).catch(
    (error) => {
      // Display an error here
    }
  );
}

You must log in to answer this question.

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