Clarification on RemoteAction calls (getting data into snippet from Apex class)

  • 1
  • Question
  • Updated 11 months ago
  • Answered
  • (Edited)
I am working on calling APEX classes. (Ultimately I need to get a List<object> into snippet.
 I am trying all 4 methods outlined in https://community.skuid.com/skuid/topics/calling_apex_function (the seminal post). In this question I am trying to understand using @remoteAction (method #4). In this post
and https://community.skuid.com/skuid/topics/problem-in-calling-apex-function  or https://community.skuid.com/skuid/topics/how-to-display-data-returned-by-apex-class the syntax of the code provide seems to say that you can  fo something like: 

                       var newAccount = MyApexClass.createAccount('Acme Generators'); 

I actually get a remoting error. I know this can be handled with callback but, i am curious how the examples are getting values out of call.

my example remote method is:

public with sharing class whyNotWork {

public whyNotWork(ApexPages.StandardController controller) {

    }
@remoteAction
    public static String showIt(String a){         
    String b = a +'hello';
    return b;
     
}
}

since I would like to capture the value in the snippet the other posts' example would be easiest. Not sure how it is working in those cases. My snippet makes call just fine but since async I can't seem to get value. no matter how much waiting or using .done().

What am I missing? Thanks so much. 
Photo of Arne-Per Heurberg

Arne-Per Heurberg

  • 1,336 Points 1k badge 2x thumb

Posted 1 year ago

  • 1
Photo of Peter Hale

Peter Hale, Employee

  • 330 Points 250 badge 2x thumb
Here's an example of an Apex class that you should be able to call declaratively from Skuid:

global class MyRemoteAction {
    
    @InvocableMethod(label=‘Call my Remote Action’)
    global static List<RemoteResponse> saveGranteeWithAddress(List<RemoteRequest> requests) {
        
        RemoteRequest req = requests[0];
        System.debug('Action SF Id: ' + req.Id);
        
  res.message = ‘success Id: ’ + req.Id;     
        RemoteResponse res = new RemoteResponse();
        List<RemoteResponse> responses = new List<RemoteResponse>();
        responses.add(res);
        return responses;
    }
    
    global class RemoteRequest {
    
        @InvocableVariable(required=true description=‘The Id’ label='Id')
        public Id myId;
    }
    global class RemoteResponse {
        @InvocableVariable
        public String message;
    }
}

In Skuid, the Remote Action should have the label 'Call my Remote Action'.  

Let me know if this works!
Photo of Arne-Per Heurberg

Arne-Per Heurberg

  • 1,336 Points 1k badge 2x thumb
Hi Peter, Thank you so much for the response. Your example is an exceptionally clear example of Invocable Method! This is perfect for using Action Framework and calling Apex Method.
A couple of things if you check out Zach's post. He isn't using Action Framework and is able to access the method and have value returned.
I would love to use Action Framework but I need to use the returned List<object> in a snippet. And while I know we can capture lastAction and named values. Not really sure how to get it into the next snippet AND List<object> is not a supported return type for invokables. The object I will be getting may change as it is a third-party construct.
I will selfishly say I am looking forward to similarly awesome answers to questions to follow shortly. Thanks again!!
Photo of Peter Hale

Peter Hale, Employee

  • 330 Points 250 badge 2x thumb
Sorry I didn't see this earlier!  yes, there is a way to get the results of the action.  In the case above, it's the message String that's returned. 

Using merge syntax, you can do this: {{$PreviousAction.result.message}}
Using JS, if you add a snippet after your action, you should be able to see the same PreviousAction via this:

var params = arguments[0],
   $ = skuid.$;

console.log(params.$PreviousAction.result.message);