Make all fields read-only using UI policy

bonsai
Mega Sage

I want to make all fields read-only when a condition is met.
I was planning to handle this with a script instead of creating UI policy actions one by one.

 

 

Identify the dictionary records present in the table in the display business rule and store the field name list in "g_scratchpad.all_field"

        var tmp_rec = new GlideRecord("sys_dictionary");
        tmp_rec.addQuery("name", current.getTableName());
        tmp_rec.addQuery("internal_type", "!=", "collection"); 
        tmp_rec.addQuery("element", "!=", "sys_id");
        tmp_rec.query();

        var filed_name_list = [];
        while (tmp_rec.next())
            filed_name_list.push(tmp_rec.getValue("element"));

        g_scratchpad.all_field = filed_name_list;

 

Use the script below in the UI policy to perform read-only processing on the field names in the list.

    for (var i = 0; i <= g_scratchpad.all_field.length; i++) {
        g_form.setReadOnly(g_scratchpad.all_field[i], true);
    }

 

 

For some reason, some fields were not made read-only. . .
Is there a setting to make it read-only in bulk other than this method?

2 ACCEPTED SOLUTIONS

Harish KM
Kilo Patron
Kilo Patron

if a field is set as Mandatory, you cannot set it to readOnly. Are those fields mandatory?

Regards
Harish

View solution in original post

RAMANA MURTHY G
Mega Sage
Mega Sage

Hello @bonsai ,

You can achieve this using client script. Find the below code

var fields = g_form.getEditableFields();
for(var i=0; i<fields.length; i++){
       g_form.setReadOnly(fields[i],true);
}

 

Please mark my answer correct & helpful, if it helps you

Thank you

 

 

Please mark my answer helpful  & correct if it helps you
Thank you

G Ramana Murthy
ServiceNow Developer

View solution in original post

7 REPLIES 7

Harish KM
Kilo Patron
Kilo Patron

if a field is set as Mandatory, you cannot set it to readOnly. Are those fields mandatory?

Regards
Harish

There is an issue where two types of fields, required and non-required, are not set to read-only.

Required fields are not blank.

can you share the screenshot of those fields which are not set to readonly?

Regards
Harish

I'm sorry, but I cannot share the capture as it includes business details.