Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Set Assigment Group and Assignet to fields as read only

SebCastro
Tera Contributor

Hello, 

 

I was trying to set Assignment group and Assigned to fields as read-only depending on the role using a client script (this is for a specific task and has to be a client script), but I have found that I can't make it work with any of the fields using 

g_form.setDisabled or g_form.setMandatory.  
 
this is a simple code I wrote just to try and make it read only but no luck
 
function onLoad() {
    var role = g_user.hasRole('incident_manager');
    g_form.setDisabled('assignment_group', true);
    g_form.setDisabled('assigned_to', true);
    g_form.setReadOnly('company', true);
}
1 ACCEPTED SOLUTION

GopikaP
Mega Sage

Hi @SebCastro , a UI Policy is overriding your Client Script.

search for a UI policy - 'Make fields read-only on close'. If you disable it and run your client script, it will work. But since it is OOB and it is not recommended to disable it. 

I recommend using a UI policy to make fields/columns read-only instead of client scripts.

View solution in original post

6 REPLIES 6

Arun_Manoj
Mega Sage

Hi @SebCastro ,

 

(function executeRule(current, previous /*null when async*/) {

// Check if the user has a specific role (replace 'your_role' with the actual role)

// add your conditions needed
if (g_user.hasRole('your_role')) {

// Make 'Assignment Group' and 'Assigned To' read-only
g_form.setReadOnly('assignment_group', true);
g_form.setReadOnly('assigned_to', true);

}
else {
// Optionally, you can make them editable if the role condition is not met
g_form.setReadOnly('assignment_group', false);
g_form.setReadOnly('assigned_to', false);
}

})(current, previous);

 

 

please mark it as helpful, solution is fine.

sunil maddheshi
Tera Guru

@SebCastro 

Please try below code:

function onLoad() {
    // Check if user has the required role
    if (!g_user.hasRole('incident_manager')) {
        g_form.setReadOnly('assignment_group', true);
        g_form.setReadOnly('assigned_to', true);
    }
}

Please mark helpful/correct, if this helps you

GopikaP
Mega Sage

Hi @SebCastro , a UI Policy is overriding your Client Script.

search for a UI policy - 'Make fields read-only on close'. If you disable it and run your client script, it will work. But since it is OOB and it is not recommended to disable it. 

I recommend using a UI policy to make fields/columns read-only instead of client scripts.

Thanks a lot for reading my question carefully and answering what I was asking for, I could confirm the UI policy you mentioned was the one causing me issues, and for the sake of the task in hand (it's a practice task so I NEED to do it via Client script). Appreciate it.