populate a Department Head’s name based on the department

shid077
Tera Contributor

Open a User form Add a new field called “Department Head”. its a custom reference field refer to"department" table.Configure this field such a way that, it should populate a Department Head’s name based on the department you selected.

please provide me how it can achieve using script include with code for both script include and on change client script.

 

1 ACCEPTED SOLUTION

AbhishekGardade
Giga Sage

Hello Shid,

Check out your another thread on community where you posted a same question and I gave you the solution.

Check out the steps :

1. Create a client script as below:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
//Type appropriate comment here, and begin script below
var deptName = g_form.getValue('department');
var si =new GlideAjax('getDepartmentHead');
si.addParam('sysparm_name','getDepartmentHead');
si.addParam('sysparm_dept',deptName);
si.getXML(setDepartmentHead);

function setDepartmentHead(response){

var deptHead = response.responseXML.documentElement.getAttribute('answer');

// Priority is depend on Imapct and Urgency.You need to set Impact and Urgency
g_form.setValue('u_department_head',deptHead); // Replace u_department_head with Field Name of Department Head.

}

2. Create a Script Include as below:

var getDepartmentHead = Class.create();
getDepartmentHead.prototype = Object.extendsObject(AbstractAjaxProcessor, {

getDepartmentHead : function(){
var dept = this.getParameter('sysparm_dept');
gs.addInfoMessage(dept);
var grDept = new GlideRecord('cmn_department');
grDept.addQuery('sys_id',dept);
grDept.query();
if(grDept.next()){
gs.addInfoMessage(""+grDept.name+":"+grDept.dept_head);
var deptHead = grDept.dept_head;
return deptHead;
}

},
type: 'getDepartmentHead'
}); 

3. Note: Department field is dependent on Company so may be you won't see any values. 

Please mark as Correct Answer/Helpful, if applicable.
Thanks!
Abhishek Gardade

 

Thank you,
Abhishek Gardade

View solution in original post

4 REPLIES 4

Alikutty A
Tera Sage

Hi,

Why dont you configure the sys_user form layout and add the Department.Department head field on your user form layout?

find_real_file.png

Thanks!

AbhishekGardade
Giga Sage

Hello Shid,

Check out your another thread on community where you posted a same question and I gave you the solution.

Check out the steps :

1. Create a client script as below:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
//Type appropriate comment here, and begin script below
var deptName = g_form.getValue('department');
var si =new GlideAjax('getDepartmentHead');
si.addParam('sysparm_name','getDepartmentHead');
si.addParam('sysparm_dept',deptName);
si.getXML(setDepartmentHead);

function setDepartmentHead(response){

var deptHead = response.responseXML.documentElement.getAttribute('answer');

// Priority is depend on Imapct and Urgency.You need to set Impact and Urgency
g_form.setValue('u_department_head',deptHead); // Replace u_department_head with Field Name of Department Head.

}

2. Create a Script Include as below:

var getDepartmentHead = Class.create();
getDepartmentHead.prototype = Object.extendsObject(AbstractAjaxProcessor, {

getDepartmentHead : function(){
var dept = this.getParameter('sysparm_dept');
gs.addInfoMessage(dept);
var grDept = new GlideRecord('cmn_department');
grDept.addQuery('sys_id',dept);
grDept.query();
if(grDept.next()){
gs.addInfoMessage(""+grDept.name+":"+grDept.dept_head);
var deptHead = grDept.dept_head;
return deptHead;
}

},
type: 'getDepartmentHead'
}); 

3. Note: Department field is dependent on Company so may be you won't see any values. 

Please mark as Correct Answer/Helpful, if applicable.
Thanks!
Abhishek Gardade

 

Thank you,
Abhishek Gardade

Thanks Abhishek..as you mention department head value is not populated.

There is any way to populate this value.

Why do people insist on writing a script when the functionality is built in the tool?  I'll never understand that.