On change script to set Default value for one reference field based on another reference field

MS25
Mega Sage

I would like to create an on change client script to auto fill approver field (head of the department selected in V1 on catalog variable),  based on what value is selected under department

 

V1

What department is the new hire joining?

List of active departments

  name: department

V2

Approver for this request

List of users

Default value is head of the department selected in V1

name: approver

 

find_real_file.png

1 ACCEPTED SOLUTION

Allen Andreas
Administrator
Administrator

Hi,

For this, it's best practice to utilize GlideAjax to communicate with the server to retrieve this information and bring it back to the client. Please use this GlideAjax cheat sheet to get started: https://community.servicenow.com/community?id=community_article&sys_id=9f7ce2e1dbd0dbc01dcaf3231f961...

Basically, within your onChange client script, once the department is select, you'll pass that selection through GlideAjax to the server so that the server can query it's records for that department, find the department head, and send that information back to the client.

Then, within your client, you'll set the value of the department head with the information sent from the server and set the value using (example😞

g_form.setValue('field_name', 'sys_id', 'display name');

Since it's a reference field, we'd want to pass the sys_id and the display name, if possible, back to the client. Otherwise, just setting the sys_id in the reference field will cause the server to still go and look for the display name and bring it back causing another trip which could be avoided.

Please mark reply as Helpful/Correct, if applicable. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

View solution in original post

3 REPLIES 3

Allen Andreas
Administrator
Administrator

Hi,

For this, it's best practice to utilize GlideAjax to communicate with the server to retrieve this information and bring it back to the client. Please use this GlideAjax cheat sheet to get started: https://community.servicenow.com/community?id=community_article&sys_id=9f7ce2e1dbd0dbc01dcaf3231f961...

Basically, within your onChange client script, once the department is select, you'll pass that selection through GlideAjax to the server so that the server can query it's records for that department, find the department head, and send that information back to the client.

Then, within your client, you'll set the value of the department head with the information sent from the server and set the value using (example😞

g_form.setValue('field_name', 'sys_id', 'display name');

Since it's a reference field, we'd want to pass the sys_id and the display name, if possible, back to the client. Otherwise, just setting the sys_id in the reference field will cause the server to still go and look for the display name and bring it back causing another trip which could be avoided.

Please mark reply as Helpful/Correct, if applicable. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

New to scripts, Can you please write down for me what should be going in to Script include and what will be the client script?

Script Include: 

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

getHead: function () {

var departmentid = this.getParameter('sysparm_departmentid');

var dept = new GlideRecord('cmn_department');

if (dept.get(departmentid)) {

var head = new GlideRecord('cmn_department');

if (head.get(dept.dept_head)){

var json = new JSON();

var results = {

"sys_id": head.getValue("sys_id"),

"name": head.getValue("name")

};

return json.encode(results);

}

} else {

return null;

}

}


});

 

 

 

 

Catalog Client Script:

function onChange(control, oldValue, newValue, isLoading) {

if (isLoading || newValue == '') {

return;

}

 

var ga = new GlideAjax('dept_head');

ga.addParam('sysparm_name', 'getHead');

ga.addParam('sysparm_departmentid', g_form.getValue('department'));

ga.getXML(updateApprover);

}

 

function updateApprover(response) {

var answer = response.responseXML.documentElement.getAttribute("answer");

var clearvalue; // Stays Undefined

if (answer) {

var returneddata = answer.evalJSON(true);

g_form.setValue("approver", returneddata.sys_id, returneddata.name);

} else {

g_form.setValue("approver", clearvalue);

}

}

 

Please help where am I doing this wrong?