Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

How to trigger a flow from a UI Action

Mahesh_Krishnan
Giga Guru

All:

I have created the following UI Action to execute a flow but it is not working.

(function() {

try {
var rec_id = g_form.getUniqueValue();
var gr = new GlideRecord('x_cpts_onboarding_cpts_referral_data');
var inputs = {};
inputs['current'] = gr.get(rec_id); // 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);
}

})();

The trigger condition I have set in the flow is for a record update and not create.

Per an earlier community post

https://community.servicenow.com/community?id=community_question&sys_id=d25c8167db00149c23f4a345ca9619d5

I have also set the When to Run the Flow value to "Run for non-Interactive Session", but the flow is still not triggering.

Appreciate any assistance from this group.

Mahesh

1 ACCEPTED SOLUTION

MrMuhammad
Giga Sage

Hi, 

Try this. This is server side code snippet so please make sure you run this on the server side of UI action.

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

})();

 

Thanks & Regards,

Sharjeel

Regards,
Muhammad

View solution in original post

8 REPLIES 8

MrMuhammad
Giga Sage

Hi, 

Try this. This is server side code snippet so please make sure you run this on the server side of UI action.

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

})();

 

Thanks & Regards,

Sharjeel

Regards,
Muhammad

Thank you very much Sharjeel!  I guess I was using some client-side functions in a server-side script.

I am just getting familiar with scripting and was not aware of this.

Again, thank you very much!

Happy to Help 🙂

FYI, there were two things that I fixed. First you guessed it right, you had used g_form which is client side API and cannot be used on server side. Second was the value you were setting for inputs['current'], it requires a GlideRecord object instead you were setting the sys_id of the record. In UI action server side code we have current as a GlideRecord Object for the table UI action created for.

I hope this helps!

 

Thanks & Regards,

Sharjeel

Regards,
Muhammad

Interesting point on the second error.  On snguru.com there was a list of gliderecord functions he does through and the gr.get(sys_id) used after the GlideRecord statement I though fetched the entire record from the table.

Clearly, that is not what was happening or I just used it incorrectly.

Thanks a bunch again!