Calling apex function

Bottom line… I’m glad it worked for you.

Could we access the webservice objects within javascript too? for example this:

global&nbsp;class&nbsp;TestInvocable<br />{<br />&nbsp;&nbsp;&nbsp;global&nbsp;class&nbsp;DispatchInfo&nbsp;<br />&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;webservice&nbsp;Date&nbsp;dispatchDate;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;webservice&nbsp;String&nbsp;shippingCompany;<br />&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;webservice&nbsp;static&nbsp;String&nbsp;testInvocableMethod(DispatchInfo&nbsp;dispatch)<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System&#46;debug('Dispatch&nbsp;info');<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System&#46;debug('date:&nbsp;'&nbsp;+&nbsp;dispatch&#46;dispatchDate);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System&#46;debug('shipping&nbsp;company:&nbsp;'&nbsp;+&nbsp;dispatch&#46;shippingCompany);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;'Ok';<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}

I need to access the ‘DispatchInfo’ object within my code snippet. I tried doing something like this:

var dipatchInfo = sforce&#46;Xml('DispatchInfo'); 


but this doesnt work as dipatchInfo was always undefined. Any help on this would be appreciated. Thanks.

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&nbsp;class&nbsp;TestInvocable
{<br>&nbsp;&nbsp;&nbsp;private&nbsp;class&nbsp;DispatchInfo&nbsp;<br>&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Date&nbsp;dispatchDate;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;shippingCompany;<br>&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;webservice&nbsp;static&nbsp;String&nbsp;testInvocableMethod(String&nbsp;dispatchJSON)<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DispatchInfo dispatch = (DispatchInfo) JSON.deserialize(dispatchJSON,DispatchInfo.class);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.debug('date:&nbsp;'&nbsp;+&nbsp;dispatch.dispatchDate);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.debug('shipping&nbsp;company:&nbsp;'&nbsp;+&nbsp;dispatch.shippingCompany);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;'Ok';<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>} 

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);


This is what I needed…thanks.

hii guys i tried to do that opportunity example i paste the code in snippet and in a button i select snippet name but its not working for me means that on click event nothing happens what wrong did i do can anyone tell me

Two questions: 

  1. Does the new action framework action “Run Custom Apex Action” change any of this?
  2. If we pursue option 2 and expose APEX methods as a REST service, can we then build a skuid REST API model against that service?

bump.

  1. If you expose your apex logic as Apex Invocable Methods, then you can call the logic through the “Run Custom Apex Action”. This approach is good if the Apex logic has a general purpose, such as “Send SMS via Twilio”, and thus should be available to call from any Skuid Page. You can lock down access to Invocable Apex Classes via Profile / Permission Sets, but not by execution context (e.g. VF Page), so If you need to lock down access to this Apex to be from a particular execution context, then the Remote Action approach would be better.
    2. REST Models are a great alternative, yes! The nice thing about this approach is that you can then use regular Skuid Model API methods to, in effect, influence Apex code. Highly recommended.

Hey Zach,
    I used the 4th option that you suggested. Embedded the Skuid Page in my visualforce page. I used the following script in my JavaScript Snippet
alert(‘hi’);
var newAccount = TestAccountEntrySkuid.createAccount();
alert('New Account Id: ’ + newAccount.Id);

When I click on the button for which I have configured the above Java Script Snippet, it only shows the first alert and post that nothing happens.
Am I missing anything here???
My Controller contains a blank constructor and a method to insert an account record. Method annotation is @RemoteAction .

Are there any errors in the JavaScript Console when you run this?

Hey Gautam,

I ran into same issue few days ago, on console you should have an error, saying that remoting error for VF remoting - missing parameters or something similar.

Try to use the function call like below and see. (it always expects callback function, so i modified like below and works for me)


TestAccountEntrySkuid.createAccount(function(result){

alert('New Account Id: ’ + result.Id);
});


Hello Zach, 
       Uncaught ReferenceError: TestAccountEntrySkuid is not defined . This is the exception that I encounter in the console. Is there anything that I need to do specifically? The method is static, so I assumed that ‘TestAccountEntrySkuid.createAccount();’ should take care of calling the method directly.

Regards,
Gautam.

Hey Kvin, 
   Following is the error in the console that I am getting:-
 Uncaught ReferenceError: TestAccountEntrySkuid is not defined ,

Hey Gautam,

I believe you are trying to preview the Skuid page alone and trying to use your class to work as extension.

Did you add this class as an extension to your VF page as Zach explained in above initial posts ?

<apex:page docType=“html-5.0” readOnly=“true” standardController=“Account” extensions=“TestAccountEntrySkuid”>

<skuid:page page=“MySkuidPage”/>

</apex:page>

Try to preview this VF page once created, that should resolve the issue.

Are there any tutorials for building a REST model to access a webservice in apex?

We have been successfully using option 1 mentioned by Zach at the top of this post i.e the AJAX toolkit’s sforce.apex.execute method. But we are receiving errors when we try to expose the same skuid page in the salesforce community. I see an error in the console that indicates it may be an error related to the External resource /soap/ajax/29.0/apex.js that is added to the skuid page. Here is the console error:
apex.js:48 Uncaught TypeError: Cannot read property ‘push’ of undefined
at sforce.Apex.execute (https://stagefull-afl.cs52.force.com/soap/ajax/37.0/apex.js:48:61)
at https://stagefull-afl.cs52.force.com/Customer/resource/1504866722000/AFLInquiryOrderResourceWorkflow…
at Object.callback (eval at (https://stagefull-afl.cs52.force.com/Customer/resource/1495240238000/skuid__JQueryJS:2:13735), :150:64)
at Object.K.aa.when.all.then.c (https://stagefull-afl.cs52.force.com/Customer/resource/1496757765000/skuid__SkuidJS:8:17252)
at Object. (https://stagefull-afl.cs52.force.com/Customer/resource/1495240238000/skuid__JQueryJS:3:7502)
at j (https://stagefull-afl.cs52.force.com/Customer/resource/1495240238000/skuid__JQueryJS:3:6134)
at Object.fireWith [as resolveWith] (https://stagefull-afl.cs52.force.com/Customer/resource/1495240238000/skuid__JQueryJS:3:6946)
at Object.e.(anonymous function) [as resolve] (https://stagefull-afl.cs52.force.com/Customer/resource/1495240238000/skuid__JQueryJS:3:7926)
at Object. (https://stagefull-afl.cs52.force.com/Customer/resource/1496757765000/skuid__SkuidJS:2:6047)
at Object. (https://stagefull-afl.cs52.force.com/Customer/resource/1495240238000/skuid__JQueryJS:3:7502)
sforce.Apex.execute @ apex.js:48

Any ideas?

Just to let everyone know calling apex from skuid in a salesforce community using sforce.apex.execute is not an issue. The above issue was caused by an undefined variable being passed to the apex class. The error message returned was very ambiguous. Hopefully this will save somebody a lot of frustration.

Is there documentation on using classes exposed with REST with Skuid? I must not be setting authorization correctly.

still haven’t been able to get REST to work

Hi Zach, I want to pass list of Ids from skuid to salesforce webservices static method.
Is this possible, if yes please give me suggession.

I want to convert array to list and need to send that list of ids as a parameters.

Thanks,
Sreeram