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

Need to make all the fields editable on a table after clicking the UI action button

PayalPawar
Tera Contributor

Hello folks,

 

My requirement is, I have created a client script to make all fields on the form in read only state. Also I have created a UI action button which will only be seen on the table form to users having that particular role. When user clicks on the UI action button, I want all the form fields to be in editable state. How can I achieve that?

3 REPLIES 3

JenniferRah
Mega Sage
Mega Sage

You probably cannot do this completely. Some fields will be visible and/or read only based on a UI Policy. You can't turn off a UI Policy from a UI Action, so the UI Action would only be able to change the read only attribute for fields it can see. If you have a specific list of fields you need to be editable, you can add a line for each one like the following. 

g_form.setReadOnly(fieldname, false);

 

One way to kind of "hack" it would be for the UI Action to set a particular field on the form. Then you can create UI Policies such that if that field is set, then all the variables are editable. However, if that field is set, then anyone editing the record would see it as editable, which probably isn't the desired effect. 

 

Another possibility would be to write into your UI Policies to leave the fields editable if the user has that particular role. Then people with that role would just always see the fields as editable.

Community Alums
Not applicable

Hi @PayalPawar ,

try this code 

function onLoad() {
    // Make all fields read-only initially
    setAllFieldsReadOnly(true);
    
    // Optional: Check if user has the role and show/hide button accordingly
    if (g_user.hasRole('your_role_sys_id')) {
        // Show the edit button (if hidden by default)
        g_form.setDisplay('your_ui_action_sysid', true);
    }
}

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

Ankur Bawiskar
Tera Patron
Tera Patron

@PayalPawar 

but why?

what's your business requirement here?

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