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 12:05 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-05-2019 12:43 PM
I just set this up for my SCTASK(sc_task) and it's making it read only even when I am a member of the assignment group.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-05-2019 01:14 PM
Sorry I apparently tested this with my admin account and another user and there for I got the result I "wanted".
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-05-2019 12:11 PM
why do you need two scripts. You can make use of On-change to work onLoad as well .
Try below.
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading) {
// do your code for 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);
}
}
Please mark my response as correct and helpful if it helped solved your question.
-Thanks