We've updated the ServiceNow Community Code of Conduct, adding guidelines around AI usage, professionalism, and content violations. Read more

fetch value from variable in Onclick function of UI action to server script in same UI action

Not applicable

In UI Action client side function, I am capturing inner HTML of element .

But same variable in server script of same ui action showing undefined.

Any suggestion?

1 ACCEPTED SOLUTION

HIROSHI SATOH
Mega Sage

In ServiceNow, when you want to pass a value from the client-side (in your OnClick function of a UI Action) to the server-side script, you can use the g_scratchpad object or GlideAjax.

Option 1: Using g_scratchpad

    • Capture the inner HTML value and assign it to g_scratchpad:

      Client-side Script:

     

 

function onClick() {
    g_scratchpad.myVariable = document.getElementById('element_id').innerHTML;
}

 

  • Server-side Script:

    • Access the value in the server script:
     

 

var myValue = g_scratchpad.myVariable;
if (myValue === undefined) {
    gs.info("Value is undefined.");
} else {
    gs.info("Value: " + myValue);
}

 

Option 2: Using GlideAjax

  1. Client-side Script:

    • Use GlideAjax to pass the value:
     

 

function onClick() {
    var ga = new GlideAjax('MyScriptInclude');
    ga.addParam('sysparm_name', 'myFunction');
    ga.addParam('sysparm_variable', document.getElementById('element_id').innerHTML);
    ga.getXMLAnswer(function(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        alert('Server-side response: ' + answer);
    });
}

 

  • Script Include:

    • Process the value in the server-side script:
     

 

var MyScriptInclude = Class.create();
MyScriptInclude.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    myFunction: function() {
        var value = this.getParameter('sysparm_variable');
        if (value === null) {
            return 'Value is undefined';
        }
        return 'Value: ' + value;
    }
});​

 

View solution in original post

5 REPLIES 5

Not applicable

Now will give a try with glideajax