Populate String Variable Dynamically Based on Requested For in Custom Table
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
yesterday
Hi All,
I have a catalog item where, when a user raises a request, I need to check if the “Requested for” user exists in a custom table.
If they do, I want to populate the requester’s name into a string variable on the catalog item.
Requirement
- On catalog item load (or when “Requested for” changes), check the custom table.
- If the Requested for user exists for the specific catalog item, populate a string variable (e.g., u_requester_name) with the requester’s display name.
- Otherwise, leave it blank (or show a message)
What I tried
- Default value of the string variable:- javascript: new global.ServiceCopilotAjaxUtils().getRequest(gs.getUserID());
Issue faced
- The requester name is not populating in the string variable.
Any suggestions or examples would be highly appreciated.
Thanks in advance!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
yesterday
Hi All,
The issue here is that the Script Include you wrote is server-side, while a catalog variable’s default value is not the right place to run server logic or GlideRecord queries. That’s why the requester name never populates.
For this requirement, the clean and supported approach is to handle it on the client side and call the Script Include via GlideAjax.
High-level approach:
Use a Catalog Client Script
onLoad → handle initial form load
onChange on Requested for → handle user changes
Call a client-callable Script Include to check the custom table
If a match exists, populate the string variable with the requester’s display name
Otherwise, clear the field (and optionally show a message)
Key points to fix:
Mark the Script Include as Client callable
Use this.getParameter() inside the Script Include
Do not call the Script Include from a variable default using gs.getUserID()
In short, this can’t be done reliably with a variable default alone. A Catalog Client Script + GlideAjax is the correct and upgrade-safe way to meet the requirement.
Hope this helps.
@vikasverma1 - Please mark as Accepted Solution and Thumbs Up if you find Helpful!!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
yesterday
Just want to confirm
-> u_envalior_request_mapping table field u_requestor is reference to sys_user
-> you should use onChange catalog client script + GlideAjax to populate the string variable whenever that earlier variable changes
onChange Catalog Client Script: Requestor variable
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading) {
return;
}
if (newValue == '')
g_form.clearValue('stringVariable'); // give here string variable name
if (oldValue != newValue) {
var ga = new GlideAjax('ServiceCopilotAjaxUtils');
ga.addParam('sysparm_name', "getRequest");
ga.addParam('sysparm_userID', newValue);
ga.getXMLAnswer(function(answer) {
if (answer != '') {
g_form.setValue('stringVariable', answer); // give here string variable name
}
});
}
//Type appropriate comment here, and begin script below
}
Enhanced Script Include: the function will work when called from server side (default value) + also from GlideAjax
var ServiceCopilotAjaxUtils = Class.create();
ServiceCopilotAjaxUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getRequest: function(user) {
var requester = this.getParameter('sysparm_userID') || user;
var gr = new GlideRecord('u_envalior_request_mapping');
gr.addEncodedQuery('u_catalog_item=Request Trial Copilot License^u_requestor=' + requester);
gr.query();
if (gr.next()) {
return gr.u_requestor.getDisplayValue();
}
return '';
},
type: 'ServiceCopilotAjaxUtils'
});
💡 If my response helped, please mark it as correct ✅ and close the thread 🔒— this helps future readers find the solution faster! 🙏
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
yesterday
@Ankur Bawiskar yes its reference field
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
yesterday
then my script and approach will work for you
💡 If my response helped, please mark it as correct ✅ and close the thread 🔒— this helps future readers find the solution faster! 🙏
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
