- 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-10-2023 12:23 PM
You cannot dot-walk with a getReference - you only have access to the fields on the referenced table (sys_user) but not fields related to a field on that table. You'll have to use a GlideAjax call for this. If you're not familiar, here is an excellent guide:
The good news is starting with the Utah release you will be able to populate other variables based on a reference variable simply within a new tab on the variable definition - no scripting required. And dot-walking works!
I will point out too that this configuration is a bit odd in that the onChange script is triggered by the change of the Department variable, which is read only, then you're getting the value of name. It would be a bit more straightforward to change the trigger to onChange of the name variable, then you can populate Department and Deparment ID from the same Script Include function.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-11-2023 02:06 PM - edited 03-11-2023 02:49 PM
Hi @Brad Bowman
I was trying to create something new, but my script is not working for me, i put an alert to see what the answer is and its coming back as undefined.
Client script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ga = new GlideAjax('DepartmentUtils');
ga.addParam('sysparm_deptid', 'getDepartmentID');
ga.addParam('sysparm_name', g_form.getValue('department'));
ga.getXMLAnswer(updateDept);
}
function updateDept(answer) {
var clearvalue; // Stays Undefined
if (answer) {
var returneddata = JSON.parse(answer);
g_form.setValue('department_code1', returneddata.id, returneddata.name);
} else {
alert(returneddata);
g_form.setValue('department_code1', clearvalue);
}
}
Script include:
var DepartmentUtils = Class.create();
DepartmentUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getDepartmentID: function() {
var dept = this.getParameter('sysparm_deptid');
var id = new GlideRecord('cmn_department');
if (id.get(dept)) {
var departmentID = new GlideRecord('cmn_department');
if (departmentID.get(id.parent)) {
var results = {
"id": departmentID.getValue("id"),
"name": departmentID.getValue("name")
};
return JSON.strigify(results);
}
} else {
return null;
}
}
});
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-13-2023 02:40 PM - edited 03-13-2023 02:41 PM
In the Client Script, the 2 addParam lines are a bit jumbled
ga.addParam('sysparm_name', 'getDepartmentID');
ga.addParam('sysparm_deptid', g_form.getValue('department'));
In the Script Include, in addition to Paul's corrections, you can add some gs.addInfoMessage lines to confirm that the function is reached, the value of dept, if the GR returns a/the correct record, etc so you'll see where it's going wrong. Another thing I like to do is declare var answer = ''; at the beginning of the function, then set the value of answer = JSON.stringify(results); in this case, then return answer; at the end of the function, outside of any loops or if blocks, so that you're always returning something. This way when your alert is null or undefined, you know there's trouble getting to the SI or back, not the GR/content of the SI.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-13-2023 07:19 PM
Thanks Brad, going to give this a shot.