Calling apex function

  • 6
  • Question
  • Updated 11 months ago
  • Answered
Is there any way to write code to call an apex function from within skuid?
Photo of Ant Belsham

Ant Belsham

  • 1,022 Points 1k badge 2x thumb

Posted 6 years ago

  • 6
Photo of Zach McElrath

Zach McElrath, Employee

  • 52,888 Points 50k badge 2x thumb
Ant, there are 4 main ways to call Apex functions from within Skuid:

1. Use the AJAX toolkit's "sforce.apex.execute()" method to execute an Apex Web Service (requires you putting your Apex logic into a Web Service)

2. Use the Salesforce Apex REST technology to expose your methods as an Apex REST Service, and then callout to this using jQuery get() or similar calls.

3. Have your Apex logic initiated by Triggers --- so that when you make data changes from within Skuid, such as changing the Status of a record, back-end Apex is kicked-off.

4. (probably what you want to do) Rather than initiating Skuid from Redirects, have your Visualforce Override pages actually include Skuid Pages through the
<skuid:page [page=pageName] [actionType=actionType objectType=objectType]>
Visualforce Component. That way, you can have a custom Apex controller / controller extension for your Visualforce Page, which contains your Apex functions, BUT you can still have your Skuid Page. We recommend making your Apex logic into JavaScript Remoting methods, so that it's easy to execute them from JavaScript. Using actionFunctions to kick off Apex logic may be problematic.

Example Visualforce Page:



<apex:page docType="html-5.0" readOnly="true"
standardController="Account" extensions="MyApexClass">

<skuid:page page="MySkuidPage"/>

</apex:page>


Example Apex Class:



public with sharing class MyApexClass {

@RemoteAction
public static Account createAccount(String accountName) {
Account a = new Account(Name = accountName);
return a;
}

}


Example use from within Skuid JavaScript code (e.g. a Snippet):



var newAccount = MyApexClass.createAccount('Acme Generators');
alert('New Account Id: ' + newAccount.Id);
Photo of Salesforce Dev007

Salesforce Dev007

  • 308 Points 250 badge 2x thumb
Could we access the webservice objects within javascript too? for example this:

global class TestInvocable
{
   global class DispatchInfo 
   {
      webservice Date dispatchDate;
      webservice String shippingCompany;
   }
    webservice static String testInvocableMethod(DispatchInfo dispatch)
    {
        System.debug('Dispatch info');
        System.debug('date: ' + dispatch.dispatchDate);
        System.debug('shipping company: ' + dispatch.shippingCompany);
        
        return 'Ok';
    }
}
I need to access the 'DispatchInfo' object within my code snippet. I tried doing something like this:

var dipatchInfo = sforce.Xml('DispatchInfo');

but this doesnt work as dipatchInfo was always undefined. Any help on this would be appreciated. Thanks.
(Edited)
Photo of Zach McElrath

Zach McElrath, Employee

  • 52,888 Points 50k badge 2x thumb
I'm not sure, would have to research, but an easier way would be to make your Apex method take a String argument, which is actually JSON, and then serialize client-side to JSON and deserialize server-side into a DispatchInfo object, e.g.

global class TestInvocable
{
   private class DispatchInfo 
   {
      Date dispatchDate;
      String shippingCompany;
   }
    webservice static String testInvocableMethod(String dispatchJSON)
    {
        DispatchInfo dispatch = (DispatchInfo) JSON.deserialize(dispatchJSON,DispatchInfo.class);
        System.debug('date: ' + dispatch.dispatchDate);
        System.debug('shipping company: ' + dispatch.shippingCompany);
        
        return 'Ok';
    }
}

And then in JavaScript you could do this (if you're using the AJAX Apex Toolkit):

var dispatch = {
    dispatchDate: new Date(),
    shippingCompany: "Acme"
};
var result = sforce.apex.execute("TestInvocable","testInvocableMethod",{ dispatchJSON: JSON.stringify(dispatch) });
console.log('the result was: ' + result);
Photo of Salesforce Dev007

Salesforce Dev007

  • 308 Points 250 badge 2x thumb
This is what I needed...thanks.