Add custom onclick Javascript to skuid

  • 1
  • Question
  • Updated 2 years ago
  • Answered
Good morning! I am trying to add a OnClick JavaScript button we have in Salesforce to be available in skuid. The button is an Event custom list button that is available for Multi-Record Selection. I am not familiar with JavaScript so any help converting this process to Skuid is greatly appreciated! 
Below is the OnClick JavaScript code:

{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/34.0/apex.js")}

var accountId = '{!Account.Id}';
var accountTerritory = '{!Account.Territory__c}';
var accountSalesRegion = '{!Account.Sales_Region__c}';
var accountSegment = '{!Account.Account_Segment__c}';
var accountOwner = '{!Account.Account_Executive__c}';


var newURL = sforce.apex.execute(
"AutomaticOppAssigment", "OpportunityAssignment", 
{
acctIdInput:accountId,
acctTerritoryInput:accountTerritory,
acctSalesRegionInput:accountSalesRegion,
acctSegmentInput:accountSegment,
acctOwnerInput:accountOwner
});

parent.location.href = newURL;
Photo of Lauren Kemp - Procore

Posted 2 years ago

  • 1
Photo of Emily Davis

Emily Davis, Employee

  • 3,512 Points 3k badge 2x thumb
Hi, Lauren,
I believe you should be able to call this from the Skuid page. Here are some tips to get you started:

1. The sforce.apex.execute() function is a function to call Apex using the AJAX toolkit in Salesforce. In order for this to work, you will need to include the AJAX toolkit URL (/soap/ajax/30.0/connection.js) within your Skuid page. To do this, go to the JavaScript tab in the page composer, add a new resource with a Resource Location of "External", and include the URL as the Resource URL.



2. Create a new JavaScript snippet that you can execute from a button in your Skuid page. To do this, create another JavaScript resource with a Resource Location of type "In-Line (Snippet)". You can give it whatever name you prefer (Example: AssignOpportunities). Your snippet code may look something like this:
// Use the name of your Account model here
var acctModel = skuid.model.getModel('Account'),
acctRow = acctModel.getFirstRow(); // Pass field values from your account row into the Apex action
var newURL = sforce.apex.execute(
"AutomaticOppAssigment", "OpportunityAssignment", 
{
acctIdInput: acctRow.Id,
acctTerritoryInput: acctRow.Territory,
acctSalesRegionInput: acctRow.SalesRegion,
acctSegmentInput: acctRow.Segment,
acctOwnerInput: acctRow.OwnerId
});
parent.location.href = newURL;

3. Call this snippet from a Skuid button or action. You could do this either through a button/action type "Custom: Run Skuid Snippet" or through the Action Framework. To use the Actions Framework, specify a button/action type "Run Multiple Actions", and add an action of type "Run a Skuid JavaScript snippet".

Here is a post with more information on various ways to call Apex from Skuid:
https://community.skuid.com/skuid/topics/calling_apex_function

Also, here are some tutorials that may be helpful for you:
Action Framework http://help.skuid.com/m/supercharge-your-ui/l/239686-action-framework
Skuid Snippets http://help.skuid.com/m/11720/l/204699-introduction-to-skuid-snippets

I hope this is enough to get you started. Let me know if you have more questions specific to your use case.
Thanks!
Emily
Hi Emily - Thank you so much for the reply!
The JS snippet posted above, and the steps outlined are exactly what we have in place currently. The issue is that the apex runs and redirects to a URL in salesforce, but we want the process to stay in skuid- possibility through a wizard component? I am just not sure how to pass the variables to the field editor in the wizard after the apex runs.  
I created a new post that more accurately addresses the issue we are having:
https://community.skuid.com/skuid/topics/javascript-parameters-passed-to-field-in-wizard-component
Photo of Jacob Cohen

Jacob Cohen, Employee

  • 142 Points 100 badge 2x thumb
Hey Lauren,

Does your apex contain a hard coded redirect upon completion?

Also, make sure you are using apex.js not connection.js, which is included with Skuid.
Thank you for the reply Jacob - No, the apex does not contain hard coded redirect. Yes we are using apex.js. Since I posted this a month ago I figured it out on my own, this can be marked as resolved. The real issue we are having is reflected in this more recent post https://community.skuid.com/skuid/topics/javascript-parameters-passed-to-field-in-wizard-component
Photo of Jacob Cohen

Jacob Cohen, Employee

  • 142 Points 100 badge 2x thumb
Hey Lauren,

I think the cause of the redirect is actually the line:

parent.location.href = newURL;

in the js. Sorry I missed that! You can just delete it or comment it out.

I'll go check out that other post now.
(Edited)
We include that line to redirect to salesforce - if we could create a skuid component in place of the salesforce create event window (that is why the newURL line is there) we would replace that line with a redirect to a skuid page that could hopefully autofill from the apex that ran