Calling a flow from a UI Action

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-20-2020 05:21 PM
All:
Using the following code to I am able to call the flow:
(function() {
try {
var rec_id = current.sys_id;
var gr = new GlideRecord('x_cpts_onboarding_cpts_referral_data');
if (gr.get(rec_id)) {
var inputs = {};
inputs['current'] = current; // GlideRecord of table:
inputs['table_name'] = 'x_cpts_onboarding_cpts_referral_data';
// Start Asynchronously: Uncomment to run in background.
//sn_fd.FlowAPI.startFlow('x_cpts_onboarding.invite_to_apply', inputs);
// Execute Synchronously: Run in foreground.
sn_fd.FlowAPI.executeFlow('x_cpts_onboarding.invite_to_apply', inputs);
}
} catch (ex) {
var message = ex.getMessage();
gs.error(message);
}
})();
However, when I add some client-side scripting in the beginning and then try to run the above server-side script it does not work.
What I have understood is that after the client-side script I have to insert the following code to ensure no other browser errors are detected:
if(typeof window == 'undefined')
function_name();
And then the server-side function is called.
In all the examples of other code a specific function name is given but for the above code of the flow it is encapsulated without a name. How can i modify it to work for my use case.
I am still new to java script and so am still figuring out the basics.
Thanks in advance for your help!
Mahesh
- Labels:
-
Flow Designer

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-20-2020 05:34 PM
use below sample code to call flow from UI action
try {
var flowInputs = {};
flowInputs['current'] = current;
flowInputs['table_name'] = current.getTableName();
var result = sn_fd.Flow.startAsync('SCOPENAME.FLOWNAME', flowInputs);
//The Sys ID of a flow execution (contextId)
var contextId = result.contextId;
} catch (ex) {
var message = ex.getMessage();
gs.error(message);
}
Regards,
Sachin

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-20-2020 05:53 PM
you can extract function body from self calling function and wrap that inside the server side function.
Below is your script
if(typeof window == 'undefined')
function_name();
function_name(){
try {
var rec_id = current.sys_id;
var gr = new GlideRecord('x_cpts_onboarding_cpts_referral_data');
if (gr.get(rec_id)) {
var inputs = {};
inputs['current'] = current; // GlideRecord of table:
inputs['table_name'] = 'x_cpts_onboarding_cpts_referral_data';
// Start Asynchronously: Uncomment to run in background.
//sn_fd.FlowAPI.startFlow('x_cpts_onboarding.invite_to_apply', inputs);
// Execute Synchronously: Run in foreground.
sn_fd.FlowAPI.executeFlow('x_cpts_onboarding.invite_to_apply', inputs);
}
} catch (ex) {
var message = ex.getMessage();
gs.error(message);
}
}
Thanks & Regards,
Sharjeel
Muhammad

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-20-2020 06:02 PM
Sharjeel:
I assume you just removed the brackets in the self-calling function. Because I tried doing this and it did not work. Did you make any other changes?
Mahesh

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-20-2020 06:07 PM
Sharjeel:
This is what i tried:
if(typeof window == 'undefined')
invite();
function invite(){
try {
var rec_id = current.sys_id;
var gr = new GlideRecord('x_cpts_onboarding_cpts_referral_data');
if (gr.get(rec_id)) {
var inputs = {};
inputs['current'] = current; // GlideRecord of table:
inputs['table_name'] = 'x_cpts_onboarding_cpts_referral_data';
// Start Asynchronously: Uncomment to run in background.
//sn_fd.FlowAPI.startFlow('x_cpts_onboarding.invite_to_apply', inputs);
// Execute Synchronously: Run in foreground.
sn_fd.FlowAPI.executeFlow('x_cpts_onboarding.invite_to_apply', inputs);
}
} catch (ex) {
var message = ex.getMessage();
gs.error(message);
}
}
And it does not work