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

Ankur Bawiskar
Tera Patron
Tera Patron

@SebCastro 

if you want fields to be readonly for incident_manager then use this

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);
    }
}

If you want fields to be readonly if logged in user doesn't have incident_manager role then use this

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);
    }
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Sourabh Tarlekr
Kilo Sage

Hi @SebCastro

 

UI Policy get applied when there is a conflicting logic with Client scripts.

I recommenced you to use UI policy instead of client script. 

It will work.

 

Regards,

Sourabh