Client script to set field read only based on assignment group

booher04
Tera Guru

We have a need to set the "Assigned To" field to read-only if the logged in user is not in the current assignment group.  We do not want individuals to be able to assign SCTASKs/INCIDENTS to users unless they are already in the current assignment group.  I have a client script that is working correctly for onChange but the onLoad one I have is not working correctly.  If I change the assignment group to something else, and I'm not in that group, the assigned to field is read only.  When I first open a record(SCTASK or INCIDENT) I can change the Assigned To even if it's an assignment group that I am not a member of.

 

Here is the client script for onChange that is working:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}

//Type appropriate comment here, and begin script below

var thisUser = g_user.userID;

var ga = new GlideAjax('findAG'); //the script include
ga.addParam('sysparm_name','getUser'); //the function
ga.addParam('sysparm_user', thisUser);
ga.addParam('sysparm_grp', newValue); //the new AG
ga.getXML(getTheUser);
}

function getTheUser(response) {
var myAnswer = response.responseXML.documentElement.getAttribute("answer");
if(myAnswer == 'no') {
g_form.setReadOnly('assigned_to', true);
} else if(myAnswer == 'yes') {
g_form.setReadOnly('assigned_to', false);
}
}

 

Here is the onLoad that is NOT working:

function onLoad() {
//Type appropriate comment here, and begin script below
var thisUser = g_user.userID;

var ga = new GlideAjax('findAG'); //the script include
ga.addParam('sysparm_name','getUser'); //the function
ga.addParam('sysparm_user', thisUser);
ga.addParam('sysparm_grp', newValue); //the new AG
ga.getXML(getTheUser);
}

function getTheUser(response) {
var myAnswer = response.responseXML.documentElement.getAttribute("answer");
if(myAnswer == 'no') {
g_form.setReadOnly('assigned_to', true);
} else if(myAnswer == 'yes') {
g_form.setReadOnly('assigned_to', false);
}
}

 

 

8 REPLIES 8

dvp
Mega Sage
Mega Sage

In onLoad client script you donot have newValue

Use g_form.getValue method instead

function onLoad() {
//Type appropriate comment here, and begin script below
var thisUser = g_user.userID;

var ga = new GlideAjax('findAG'); //the script include
ga.addParam('sysparm_name','getUser'); //the function
ga.addParam('sysparm_user', thisUser);
ga.addParam('sysparm_grp', g_form.getValue('assignment_group')); //the new AG
ga.getXML(getTheUser);
}

function getTheUser(response) {
var myAnswer = response.responseXML.documentElement.getAttribute("answer");
if(myAnswer == 'no') {
g_form.setReadOnly('assigned_to', true);
} else if(myAnswer == 'yes') {
g_form.setReadOnly('assigned_to', false);
}
}

 

Hi DVP - thank you for the response.  I'm still not great at scripting as you can see.  Where in the script would I add the newValue?  

Please use the script provided by me in earlier comment. I have replace newValue with g_form.getValue

function onLoad() {
//Type appropriate comment here, and begin script below
var thisUser = g_user.userID;

var ga = new GlideAjax('findAG'); //the script include
ga.addParam('sysparm_name','getUser'); //the function
ga.addParam('sysparm_user', thisUser);
ga.addParam('sysparm_grp', g_form.getValue('assignment_group')); //the new AG
ga.getXML(getTheUser);
}

function getTheUser(response) {
var myAnswer = response.responseXML.documentElement.getAttribute("answer");
if(myAnswer == 'no') {
g_form.setReadOnly('assigned_to', true);
} else if(myAnswer == 'yes') {
g_form.setReadOnly('assigned_to', false);
}
}

benn23
ServiceNow Employee
ServiceNow Employee

No need to make a call back out to the server on load.

Use a 'display' business rule to evaluate and an onLoad client script to check the result.

Business Rule:

When: Display
Condiion: !RP.isPopup() && !current.isNewRecord()
Script:

(function executeRule(current, previous /*null when async*/) {
g_scratchpad.iAmAMember = gs.getUser().isMemberOf(current.assignment_group);
})(current, previous);

 

Client Script:
Type: onLoad
Script:

function onLoad() {
if(g_scratchpad.iAmAMember != undefined && !g_scratchpad.iAmAMember){
g_form.setReadOnly('assignment_group',true);
}
}

An FYI, there's an OOB UI Policy, named "Make fields read-only on close," that conflicts with this method. Typically companies do not allow users to re-open Incidents, so there's really no need for the 'reverse if false' flag to be set.