sforce.apex.execute not working in Lightning experience

  • 1
  • Problem
  • Updated 9 months ago
  • Not a Problem
Hi All,

sforce.apex.execute works well in classic mode with snippet, but fail in Lightning experience.

Any help is appreciated.

Photo of Kevin

Kevin

  • 572 Points 500 badge 2x thumb

Posted 12 months ago

  • 1
Photo of Mark DeSimone

Mark DeSimone, Official Rep

  • 11,040 Points 10k badge 2x thumb
Hi Kien, since this is a Salesforce command, you will probably need to look through Salesforce's documentation or discussion boards for the best information. https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_and_ajax.htm is probably a good starting point.
Photo of Zach McElrath

Zach McElrath, Employee

  • 48,984 Points 20k badge 2x thumb
Kien,

Use of sforce.apex.execute is no longer recommended --- the recommended approach, which will allow any Apex code to be "invocable" from both Classic/Visualforce and Lightning Experience, without writing any JavaScript, is to expose your Apex logic via an "InvocableMethod". Skuid then allows you to call Invocable Methods using the "Run Data Source Action" action type in the Action Framework, so that you can eliminate any JavaScript. 

See this tutorial for more details:

https://docs.skuid.com/latest/en/skuid/salesforce/apex/apex-invocable-methods.html

Any Apex logic that you currently have exposed via the WebService annotation could fairly easily be adjusted to be exposed as an Invocable Method, which would allow this logic to be invoked from any Skuid Page, running in any Salesforce environment. As an added benefit, it also enables the logic to be invoked from Process Builder / Visual Workflow.

This is what we recommend --- "sforce.apex.execute" is only available in Classic/Visualforce.
Photo of Kevin

Kevin

  • 572 Points 500 badge 2x thumb
Hi Zach,

I want to pass the Id of records when a user checks multiple rows in a table. How can I pass them to InvocableVariable?

Thanks.
Photo of Zach McElrath

Zach McElrath, Employee

  • 48,984 Points 20k badge 2x thumb
One way would be to combine them into an array and then JSON stringify them. You can create a Ui-Only Model to store the Ids and then populate a value in that Ui-Only Model's row, then use that when populating the Invocable Variable. Something like this, could be put into a snippet and then run this snippet prior as part of the action sequence, immediately prior to the "Run Data Source Action" where you invoke the Apex:

(Assuming a Ui-only Model exists with name "Ui" and a default row exists in it, and there's a field called "RowIds")

var model = skuid.$M("Ui");
var row = model.getFirstRow();

var rowIdsString = JSON.stringify(params.list.getSelectedItems().map(function(item) {
   return item.row.Id;
}));

model.updateRow(row, "RowIds", rowIdsString);

And then add an InvocableVariable to your Invocable Method whose name is "RowIds", and populate it in Skuid with the value {{$Model.Ui.data.0.RowIds}}
Photo of Kevin

Kevin

  • 572 Points 500 badge 2x thumb
Hi  Zach,

I'm using Invocable Methods and the Apex Data Source Action. In Apex class, there is a method returns a string value. Then I want to display this result on Skuid page by Popup.

How can I do it?