Remote Actions¶
By annotating your Apex code as a RemoteAction, you can easily access it via a JavaScript—as long as you create a Visualforce page using that RemoteAction’s controller and embed your Skuid page with the skuid:page Visualforce component. This workflow has several advantages:
- Remote actions are fast and lightweight.
- They don’t need to call web services.
- End user do not need to have API Access.
However, the fact that you must have a dedicated Visualforce page and Apex controller to use them may be a hindrance, depending on your use case.
Note
Remote Actions and Lightning Experience
As remote actions are meant for use within Visualforce pages, this method of accessing Apex is not recommended if your org is transitioning to Lightning.
Consider using InvocableMethods, but note their usage will count against API limits for your org.
Note
While actionFunctions may seem to be a similar to remote actions, they are very different in practice and considerations. If you intend to use Apex within Skuid, we recommend remote actions instead of actionFunctions. Remote actions make Apex logic accessible via JavaScript remoting methods, making it easier to execute while using JavaScript in Skuid.
To use remote actions:
- Annotate your Apex method as a @RemoteAction.
- Create a Visualforce page with your Apex controller and a skuid:page component.
- Write JavaScript code that references your Apex.
Annotate your Apex class as a @RemoteAction¶
To mark your Apex as a RemoteAction, you’ll need to add the @RemoteAction annotation before your method declaration.
For this example, we’ll use Apex code as an extension class for our Account standard controller, though you can also build custom controllers as well.
public with sharing class MyApexClass {
@RemoteAction
public static Account createAccount(String accountName) {
Account a = new Account(Name = accountName);
insert a;
return a;
}
}
Create a Visualforce page with your Apex controller and a skuid:page component¶
After annotating the Apex code, you need to add a reference to its controller/extension classes in your Visualforce page using standard Visualforce markup. You must also directly embed your Skuid page using the skuid:page Visualforce component, as opposed to the Redirect method that is frequently used to override standard Salesforce object views.
<apex:page docType="html-5.0" readOnly="true"
standardController="Account" extensions="MyApexClass">
<skuid:page page="MySkuidPage"/>
</apex:page>
Write JavaScript that references the Apex code¶
Now that the Apex is available for use within a Skuid page, you can use it via JavaScript. Here’s an example of a snippet, which end users can access from a button via the Action Framework
1 2 3 4 | MyApexClass.createAccount('Acme Generators', function(result, event){
var acctId = result.Id;
alert('New Account Id: ' + acctId);
});
|