
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-16-2024 09:47 PM
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?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-16-2024 10:22 PM
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:
- Capture the inner HTML value and assign it to g_scratchpad:
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
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;
}
});​

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-16-2024 11:32 PM
Now will give a try with glideajax