
- 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-12-2019 08:03 PM
I see, you best bet then if an onChange script. the only caveat is when a user is assigned to multiple CI's, which then do you use as default?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-12-2019 08:10 PM
Can you try this on your onChange Script under Requested For field:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '')
return;
var gr = new GlideRecord('cmdb_ci_computer');
gr.addQuery('assigned_to', newValue);
gr.addQuery('sys_class_name', 'cmdb_ci_computer');
gr.query();
if(gr.next()){
g_form.setValue('u_cmdb_ci',gr.sys_id);
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-12-2019 09:13 PM

- 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-16-2019 10:09 PM
thank you so much, that got it! just needed to make some tweaks to the form values for my variable names.