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

Community Alums
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

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

 

Community Alums
Not applicable

Thanks. Will give it a try.

Community Alums
Not applicable

Using g_scratchpad in UI action , below error

 

com.glide.script.RhinoEcmaError: "g_scratchpad" is not defined.
sys_ui_action.4f0383d41b4856900f3452ce034bcb53.script : Line(49) column(0)
46:

This error occurs because the g_scratchpad object is not defined. g_scratchpad is typically used to pass data from server-side scripts (like business rules) to client-side scripts. The error suggests that g_scratchpad has not been properly initialized or defined before being referenced in your script. Here are some possible causes:

  1. g_scratchpad is not set in the server script: g_scratchpad must be assigned values in a server-side script before being referenced in a client script or UI action. If it hasn't been set up on the server side, referencing it on the client side will result in this error.

  2. Incorrect server script execution timing: g_scratchpad is initialized in server-side scripts, but if this happens at the wrong time, it might not be available when the client script or UI action tries to access it.

  3. Client script execution timing: If a client script or UI action attempts to use g_scratchpad before the server-side value is available, the error will occur.