Interested in a ServiceNow event built for developers? Registration for now[dev]26 is officially open!

Making Field Read Only across all the sections of the Form

SatyanarayanaS
Tera Contributor

I have written a client script to make the field read only on certain conditions, it is making the field read only on the main form but I have the same field added to another section of the form, there it is editable, How to enforce the field as read only across all sections of the form?

Can anyone help me on this?

9 REPLIES 9

Aditya_hublikar
Giga Sage

Hello @SatyanarayanaS ,

 

 I have tried your usecase ,  for my practice table username field . So i have tried this dom manipulation in order to make username field readonly .  Ensure that isolated script is unchecked .

 

function onLoad() {
   
   g_form.setReadOnly('u_username',true);
   var sections = g_form.getSections();
    
    // 0 is the first section
    var targetSection = sections[1]; 
    
    if (targetSection) {
        var field = targetSection.querySelector('[id*="sys_display.u_practice.u_username"]');
        alert(field)
        if (field) {
            field.disabled = true;
            field.readOnly = true;
        }
    }
}

 

Aditya_hublikar_0-1787639248021.png

Aditya_hublikar_1-1787639284497.png

 

If this helps you  then mark it as helpful and accept as solution .

Regards,

Aditya

keshav77
Tera Expert

@SatyanarayanaS 

If the same field is added in multiple locations on a form and is made read-only using a UI Policy or Client Script, the read-only behavior will be applied to that field in all places where it appears, provided the specified condition is met. 

The field is not becoming read-only because another UI Policy or Client Script may be overriding the read-only configuration. If one script sets read-only to true and another later sets it to false, the last executed logic determines the final field state.
 

 

HarishKumar6668
Kilo Sage

Hi @SatyanarayanaS ,

This is an expected behaviour when the same field is added to a form more than once. Having duplicate instances of the same field can lead to inconsistent behaviour with Client Scripts and UI Policies, as the behaviour may not be applied consistently across all instances.

Please refer to the below article:
Duplicate fields on a form can lead to unexpected results with UI policies 

If you find this information useful, please mark the response as helpful and accept it as the solution.

Regards,
Harish

Sujit Jadhav
Tera Guru

Hello @SatyanarayanaS ,

Use a UI Policy
1.Navigate to All > System UI > UI Policies and click New.
2.Set the table name and define your conditions (e.g., matching the same conditions from your script).
3.Save the record.
4.Scroll down to the UI Policy Actions related list and click New.
5.Select your field (u_link) and set Read only to True.

OR

Update Your Client Script using DOM Querying
If you must stick to using your current asynchronous GlideAjax client script structure, you can bypass standard g_form limitations by using standard JavaScript to query all elements representing that field on the form.
Replace your g_form.setReadOnly('u_link', true); line with a query selector loop:

// Target all element inputs/controls for the 'u_link' field across all sections
var fieldElements = document.querySelectorAll('[id*="u_link"]');
fieldElements.forEach(function(element) {
element.disabled = true; // Disables input fields
element.readOnly = true; // Sets fields to read-only
});


"If you found my answer helpful, please like and mark it as an "accepted solution". It helps future readers to locate the solution easily and supports the community!"


Thank You,

Sujit Jadhav

s_p_naidu
Mega Guru

Hi @SatyanarayanaS,
You can make your field 'read only' on the Dictionary entry record.
System Defintion>Tables open your table and open your field.
Configure the form and add 'Read only' , 'Read only option' fields 
check 'Read only' field, then you can select 'Read only option'
options include:

1.Display Read Only: Only human users cannot edit it on the form. Scripts, flows, and APIs can still update it.

2.Client Script Modifiable: Human users and server scripts/APIs cannot edit it, but browser Client Scripts can.

3.Strict Read Only: Completely locked down. Neither human users, Client Scripts, nor server APIs can change it.



 

s_p_naidu_0-1787664755445.png