Calling a flow from a UI Action

Mahesh_Krishnan
Giga Guru

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

9 REPLIES 9

Hi Mahesh,

You are not using the gliderecord object instead using the current object. Can you just try putting the gr in the inputsobject:

like below: and added info message whether its getting inside or not:

 

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'] = gr; // 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);

gs.addInfoMessage('@inside run flow'+ gr.sys_id);
// 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,

CB

Can you show me the client side code of the UI ACTION?
Regards,
Muhammad

Mahesh_Krishnan
Giga Guru

Sharjeel:

Here is the full script:

function fieldvalid() {

    if (g_form.hasField("opportunity_id")) {

        g_form.hideAllFieldMsgs();

        

        if (g_form.getValue("opportunity_id") == "") {

            g_form.showFieldMsg('opportunity_id', getMessage('Opportunity ID is required.'), 'error', false);

        }

        

        return false;

    }

 

    gsftSubmit(null, g_form.getFormElement(), 'invite_to_apply');

 

}

 

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

    }

 

}

 

So, the last server call also actually works.  The issue I think is with the following statement:

gsftSubmit(null, g_form.getFormElement(), 'invite_to_apply');

Not sure what because i see examples where it has been used and others where the above statement is not being used.

Again, really appreciate your help!

Mahesh

The Way client and server side UI action works is, when user click on Button Client side code executes [code in fieldValue() Function] after that below line executes which call the UI action again and skip the client side code and this time executes the Server side code of the UI action.

gsftSubmit(null, g_form.getFormElement(), 'invite_to_apply');

Now, if the above line has an issue then server side code will never execute. 

However, code looks good to me. For debugging try placing a log or info message inside invite() function and see if the server side code is running

gs.addInfoMessage("SERVER CODE IS RUNNING!!"); // place inside invite() function.

 

Thanks & Regards,

Sharjeel

Regards,
Muhammad

VaranAwesomenow
Mega Sage

There is a way to trigger a flow synchronously as well

https://developer.servicenow.com/dev.do#!/reference/api/orlando/server/sn_fd-namespace/ScriptableFlowAPI#ScriptableFlow-startFlow?navFilter=flow