- 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 06:45 AM
Thanks a lot Brad for your assistance, i really appreciate your time!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-11-2023 10:08 PM
There's a typo in your script include. JSON.strigify should be JSON.stringify. This is likely causing the issue where returneddata is undefined in your client script.
Try changing this line:
return JSON.strigify(results);
to:
return JSON.stringify(results);
Also, in your client script, it looks like you're trying to pass three arguments to g_form.setValue() instead of two. The function only takes two arguments, so you should remove the returneddata.name argument. Change this line:
g_form.setValue('department_code1', returneddata.id, returneddata.name);
to:
g_form.setValue('department_code1', returneddata.id);
Once you've made those changes, try running your script again and see if it's working as expected.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-13-2023 08:21 AM
Hi @Paul Deonarine3 thanks for the reply,
Made those updates, no longer returning undefined, but still not setting the value of that particular field.
- 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 06:44 AM
This turned out to be the solution that worked, with a slight modification of changing 'department_code' to 'id". Really appreciate the help, are there any resources you could share on becoming more familiar with script includes and when to use them?