The CreatorCon Call for Content is officially open! Get started here.

Issue setting field to readonly

Mark9
Giga Expert

I'm trying to toggle the read-only property of the Next Step field (u_next_step) on the Service Record screen.  I have tried two approaches (UI Policy and Client Script) and neither approach works.  I know the code is being executed as I have added log statements to the console.  I also in the same code set sys_created_on to read-only (just to see that it works) and that field is being set to read-only while Next Step is not.  Both fields are initially editable in the form design.   When I create a new SR, the form loads and the Created field is grayed out but the Next Step field is not.

Here is the client script code:

function onLoad() {
    console.log('onLoad client script triggered');
    if (g_form.isNewRecord()) {
        console.log('This is a new record. The u_next_step field will be read-only.');
        g_form.setReadOnly('u_next_step', true);
        g_form.setReadOnly('sys_created_on', true);
    } else {
        console.log('Client Script: This is an existing record. Making the u_next_step field editable.');
        g_form.setReadOnly('incident.u_next_step', false);
        g_form.setReadOnly('sys_created_on', false);
    }
}

And here are the console statements
Mark9_0-1715919312656.png

 

 

I'm sure this should be really simple, but I can't figure it out. Any help would be greatly appreciated. Thx.



14 REPLIES 14

Mark Roethof
Tera Patron
Tera Patron

Hi there,

 

Have you verified the fields are not mandatory?

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

Hi Mark,

 

No the field is not mandatory.

 

Cheers, Mark

Vrushali  Kolte
Mega Sage

Hello @Mark9 ,

 

If the Next Step field (u_next_step) has been marked as Mandatory, then the field will not become read-only.

This is the servicenow behaviour that if field is mandatory and it does not have any value then you cannot make it read-only.

If this is the case then you can first mark the field mandatory false and then make it read-only as below :

 

g_form.setMandatory('u_next_step', false);
g_form.setReadonly('u_next_step', true);

 If my answer solves your issue, please mark it as Accepted ✔️ and Helpful 👍 based on impact.

Thanks for your reply Vrushali.  It is not a mandatory field but just in case I added the line to set mandatory to false as you suggested, but it still isn't working.