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

Ankur Bawiskar
Tera Patron
Tera Patron

@miro2 

I don't think you can set variable value using g_form in workspace client script

g_form.setValue() will work to set variable value in variable editor in native

💡 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  is there any way to update variable using workspace client script?

@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

Hi @Ankur Bawiskar 
This approach works partially. I’m able to fetch the value from the client side and pass it to the Script Include.
The issue I’m facing is that I can’t set this value to a variable using 

gr.variables.reason = 'my value';
gr.update():


The variable value is stored in the question_answer table, and with this approach, it doesn’t work also.

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


        var gr = new GlideRecord('sn_hr_core_case');

        if (!gr.get(sysId)) {
            return 'Record not found';
        }

        var qAnswer = new GlideRecord('question_answer');
        qAnswer.addQuery('table_sys_id', sysId);
        qAnswer.addQuery('question.name', 'reason');
        qAnswer.query();

        if (qAnswer.next()) {
            qAnswer.value = reason;

            qAnswer.update();
            return 'success';
        } else {
            return 'error:Question answer not found';
        }

    },


Any idea what might be wrong here?