Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to set Variable from g_modal using UI Action?

miro2
Mega Sage

Hi,

I'm trying to set a variable from g_modal using a UI Action. The goal is to set the value from the button to the variable, but this doesn't work. If I try to save it to any field, then it works properly.

Any thoughts on how to save the value to a variable?
UI Action (client)

miro2_0-1762783190785.png

 

function onClick(g_form) {
    var fields = [{
        type: 'choice',
        name: 'reason',
        label: getMessage('Choose reason'),
        value: getMessage(' -- Select -- '),
        choices: [{
            displayValue: 'Missing details',
            value: 'missing_details'
        },
        {
            displayValue: 'Urgent issue',
            value: 'urgent_issue'
        }],
        mandatory: true
    }];

    g_modal.showFields({
        title: "Select your reason",
        fields: fields,
        size: 'lg'
    }).then(function(fieldValues) {
        g_form.setValue('variables.reason', fieldValues.updatedFields[0].value);
        g_form.save();
    });
}


Modal:

miro2_0-1762783295390.png


Variable editor: empty when reason is seleted

miro2_1-1762783317736.png

 


 

1 ACCEPTED SOLUTION

@miro2 

this approach will work

1) use GlideAjax in workspace client script and pass the record sysId and the variable value

2) then in script include query that table with sysId and update the variable like this

gr.variables.reason = 'your value';

gr.update():

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

10 REPLIES 10

@miro2 

it should work with this, I added setWorkflow(false) in case some Query BR is stopping HR Case from being visible.

    processReason: function() {
        var sysId = this.getParameter('sysparm_sys_id');
        var reason = this.getParameter('sysparm_reason');

        var gr = new GlideRecord('sn_hr_core_case');
        gr.addQuery('sys_id', sysId);
        gr.setWorkflow(false);
        gr.query();
        if (gr.next()) {
            gr.variables.reason = reason;
            gr.update();
            return 'success';
        } else {
            return 'Unable to update';
        }

    },

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Ankur Bawiskar it seems like scope cross access is preventing the variable update, Script include and UI Action is in scope app. I need to check if creating a Cross scope privilege solves this.

miro2_0-1762864776201.png

 

@miro2 

yes that's the issue or else it should work fine.

Your UI action and Script Include should be in HR Core Scope only

Script Include should be made Accessible From All Scopes then only GlideAjax works from workspace client script

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Ankur Bawiskareven though I've moved the scripts to the HR scope, the variable is still not getting updated.
I've checked the Question Answer table and noticed that the value updates for a moment, then disappears.

ezgif-5247d4d40183e3a9.gif

@miro2 

should not happen unless there is some other script which is clearing.

I believe I provided correct approach to use GlideAjax and it's partially working and you can debug further from here.

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader