
- 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 06:45 PM
Hi Kai,
You should be able to use reference qualifier in this scenario.
On the cmdb_ci field you can use this as reference qualifier:
javascript: '<assigned_to>=' + current.variables.u_requested_for;
This previous post should get you started:

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-12-2019 06:53 PM
thanks for the reply Raf
does that not then restrict the available values in that field?
i might need to give more context for my use case. This is for a catalog item for a general service request. I have a few list options for users to select from, e.g. "i need a computer or printer moved", "i need something on my computer changed", etc.
the CMDB ci field has a UI policy that makes it visible if the request is going to be related to something in the CMDB (e.g. computer, or printer).
It is also already configured to show only computers and printers.
I would like the default value to populate to whatever the user's assigned computer is from the cmdb_ci_computer table, but with the ability for the user to then change that to something else if the request is related to a different cmdb item.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-12-2019 06:59 PM
The reference qualifier would only show CI assigned to the user. You can still filter it down to specific classes like:
javascript: 'sys_class_name=cmdb_ci_computer^ORsys_class_name=cmdb_ci_printer^assigned_to=' + current.variables.u_requested_for;

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-12-2019 07:41 PM
yeah a reference qualifier isn't really what i'm looking for unfortunately. I don't want to restrict the results in the field, i want to populate it with a default value.
so current reference qualifier is sys_class_name=cmdb_ci_computer^ORsys_class_name=cmdb_ci_printer^EQ
this returns all computers and printers in the lookup. this is what i want to keep.
however, by default the field on the form is empty. i want to populate the default value with the user's assigned to computer (but still allow the user to change this to any other from the results if they need to)