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

Stewe Lundin
Mega Guru

You don't need a script: set up a UI Action on the table: 

 

1. set the Order to 500;

2. Revers id true  (CHECKED)

find_real_file.png

1. Short description

2. Active

3. Assignment Group , Is dynamic, One Of my groups 

4. Assigned to


5. Read Only FALSE

find_real_file.png

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.  

Sorry I apparently tested this with my admin account and another user and there for I got the result I "wanted".


 

Prateek kumar
Mega Sage

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