Onchange client script

JP6
Tera Contributor

Hi All,

I have a function in a script include which I want to call in a on change client script to retrieve if the requested for  user has IT Hardware department as his department . And if he has IT hardware department then make few fields on the form as mandatory/read only. Please highlight the changes required in the client script.

 

FUNCTION:

getDepartmentChain: function(userOrDept, isDept, valueExtractor) {
var MAX_SAFE_LOOPS = 10000; // a department shouldn't be more than 10,000 levels deep
var departmentChain = [];
var cycle = [];
valueExtractor = valueExtractor || function(department) { return department.sys_id.toString(); };
 
var department = this._getDepartmentFromUserOrDept(userOrDept, isDept);
var isInvalidDepartment = !department || !department.isValidRecord();
if(isInvalidDepartment || department.sys_id == BranchUtility.SHARED_SERVICES) return departmentChain;
 
do {
departmentChain.unshift(valueExtractor(department));
cycle.push(department.sys_id.toString());
department = department.parent;
} while(
// Continue searching the department chain, as long as:
department && !department.nil() // the department is a valid GlideElement (haven't reached an empty department) 
&& cycle.indexOf(department.sys_id.toString()) === -1 // we haven't seen this department yet (avoid infinite loops)
&& department.sys_id != BranchUtility.SHARED_SERVICES // we've reached the top of the department hierarchy.
&& MAX_SAFE_LOOPS-- // a fail safe to avoid looping forever in case the above conditions don't exit
);
 
if(MAX_SAFE_LOOPS <= 0) return []; // something went wrong as we weren't able to safely exit the loop, so we'll return no ids in this case
 
return departmentChain;
},
 
 
On Change Client script:
var requestedFor = g_form.getValue('requested_for');
 
var userOrDept = '47ad9780db770cd0db35cef405961955';
 
var isDept = false;
 
var dep = new GlideAjax("global.BranchUtility");
        dep.addParam('sysparm_name',userOrDept);
        dep.addParam('sysparm_classname', isDept);
dep.addParam('')
        dep.getXML(getDepartmentChain);
    }
 
function getDepartmentChain(response) {
var userDeptChain = response.responseXML.documentElement.getAttribute("answer");
 
var isUserFromforDept = userDeptchain.indexOf(userOrDept) !== -1;
 
if (isUserFromforDept) {g_form.setDisplay('approval_group',true);
g_form.setValue('approval_group','e34d9ebbdb5ec210ddf5bc56f39619f5');
g_form.setMandatory('approval_group',true);
g_form.setReadOnly('approval_group',true);
g_form.setMandatory('approver',false);
g_form.setDisplay('approver',false);
g_form.setDisplay('approver_position_title',false);
 
2 REPLIES 2

Ankur Bawiskar
Tera Patron
Tera Patron

@JP6 

your script include should be client callable and also you should be sending the parameter to function in another way

I could see the function is accepting parameters like this; but it won't work for Ajax call

you need to update the syntax of Ajax call

getDepartmentChain: function(userOrDept, isDept, valueExtractor) {
 
Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Thanks Ankur 

could you help me with which line should be corrected in client script?