Client script to set field read only based on assignment group
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-05-2019 11:40 AM
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);
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-05-2019 02:05 PM
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);
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-06-2019 04:54 AM
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?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-06-2019 05:00 AM
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);
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-06-2019 05:53 AM
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.