- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-10-2023 11:54 AM
Hello all,
Im trying to get the department ID on a catalog item, but i am having trouble getting this information to display in the service portal.
Here is my form: 2 fields shown are in a variable set
I have tried a few catalog client scripts but none of them are working for me, upon searching i found that you have to use a callback function in the portal, but this callback function is still not working for me.
Catalog client script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var user = g_form.getReference('name', callBack);
function callBack(user) {
g_form.setValue('department_code1', user.department.id);
}
}
Could someone help give me some direction on this one?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-13-2023 11:08 AM
Can you try with below script,
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
// Get the user reference value from the "name" field
var userRef = g_form.getValue('name');
// Use the GlideRecord API to retrieve the user record and its associated department
var userGr = new GlideRecord('sys_user');
userGr.addQuery('sys_id', userRef);
userGr.query(function(result) {
if (result.next()) {
var departmentRef = result.getValue('department');
var departmentGr = new GlideRecord('cmn_department');
departmentGr.addQuery('sys_id', departmentRef);
departmentGr.query(function(result) {
if (result.next()) {
// Set the value of the "department_code1" field to the department ID
g_form.setValue('department_code1', result.getValue('department_code'));
}
});
}
});
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-15-2023 07:06 AM
Executing a GlideRecord within a Client Script is not supported nor considered best practice. The solution is to use GlideAjax to call a Script Include when you need to access fields related to the reference field which cannot be done with getReference. You'll need to know how to use this and troubleshoot it to get it to work even after the variable auto-populate feature in the Utah release for all of the other things you can do in a Script Include to return results for variables or as a reference qualifier.