
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-12-2019 06:16 PM
Hi All
i'm trying to write a client script for a catalog item to populate a reference field, based on another reference field. Basically i want to take the selected user in the u_requested_for field, look up their computer from the cmdb_ci_computer table, and then populate the u_cmdb_ci field on the form with that value.
I have:
(on change client script, based on u_requested_for variable)
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var user = g_form.getValue('u_requested_for');
var gr = new GlideRecord('cmdb_ci_computer');
gr.addQuery('assigned_to', user);
gr.query();
if(gr.next()){
g_form.setValue('u_cmdb_ci',gr.sys_id);
}
}
a variation of this script works elsewhere, where we are populate an incident record's ci value based on who is in the caller field. however on this catalog item when trying to do it with the form values it's not working. In that case it's part of the record producer so clearly i'm doing it wrong here or not understanding the difference in how scripts work in different places.
any help to steer me in the right direction would be appreciated.
Solved! Go to Solution.
- Labels:
-
Service Catalog

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-12-2019 09:56 PM
Lets do GlideAjax then 🙂
Add this onChange Script in the Requested by:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
//Type appropriate comment here, and begin script below
var gajax = new GlideAjax('CMDBUtils');
gajax.addParam('sysparm_name', 'getCIDetails');
gajax.addParam('sysparm_user', newValue);
gajax.getXML(setCIDetails);
function setCIDetails(serverResponse) {
var cc = serverResponse.responseXML.documentElement.getAttribute("answer");
g_form.setValue('computer', cc);
}
}
And this script include:
var CMDBUtils = Class.create();
CMDBUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getCIDetails: function () {
var usr = this.getParameter('sysparm_user');
var gr = new GlideRecord('cmdb_ci');
gr.addQuery('assigned_to',usr);
gr.query();
if (gr.next()) {
return gr.sys_id;
}
else {
return '';
}
},
type: 'CMDBUtils'
});
Let me know how you go.
Regards,
Raf

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-13-2019 10:55 PM
So i've been playing with this a little more, and it seems like this should be done with glideajax (and i am totally new to glideajax).
so i got a very helpful article here: https://snprotips.com/blog/2016/2/6/gliderecord-client-side-vs-server-side
and i build a script include i thought would work, based on that article. No dice - every single time it returns null.
so i went back to basics, and copied verbatim the example in that article. This works.
one by one i started modifying fields to what i needed, and i found that it breaks the second you add a glide lookup to the server script.
so what i have now is this:
client script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '')
return;
var user = g_form.getValue('u_requested_for');
var ga = new GlideAjax('AjaxScriptInclude'); //This argument should be the exact name of the script include.
ga.addParam('sysparm_name', 'ciFunction'); //sysparm_name is the name of the function in the script include to call.
ga.addParam('sysparm_u_sysid', user); //This is an optional parameter which we can reference in the script include.
ga.getXML(myCallBack); //This is our callback function, which will process the response.
function myCallBack(response) { //the argument 'response' is automatically provided when the callback funciton is called by the system.
//Dig out the 'answer' attribute, which is what our function returns.
var ci = response.responseXML.documentElement.getAttribute('answer');
alert(ci); //alert the result to make sure all is well.
}
}
and server script:
//client-callable script include
var AjaxScriptInclude = Class.create(); //(auto generated)
AjaxScriptInclude.prototype = Object.extendsObject(AbstractAjaxProcessor, { //(auto generated)
ciFunction: function() { //Declare a function inside our class.
var userName = this.getParameter('sysparm_u_sysid'); //Get the parameter we passed in from the client script
var cmdbci = new glideRecord('cmdb_ci_computer');
cmdbci.addQuery('assigned_to',userName);
cmdbci.query();
return 'Hiya, ' + userName + '.'; //return a friendly greeting
}
});
This returns "null" in the alert. However the moment i comment out this bit:
var cmdbci = new glideRecord('cmdb_ci_computer');
cmdbci.addQuery('assigned_to',userName);
cmdbci.query();
so it's not doing any kind of lookup, it works again, and returns "Hiya, (sys id of user in requested for field)"
i did have
if(cmdbci.next()){ return 'Hiya, ' + userName + '.'; //return a friendly greeting }
after the glideRecord variable, but removed it during testing. it didn't make any difference.
i'm clearly doing this wrong but struggling to find how to do it correctly.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-14-2019 06:10 PM