Making all the fields non editable except one in Client Script

aditiojha
Tera Contributor

I am making entire form read only on a condition with a on load client script with code as below :

 

var fields = g_form.getEditableFields();

for (var x = 0; x < fields.length; x++) {

        g_form.setReadOnly(fields[x], true);

}

 

This is because i have lot of conditional hide and display on form.

 

Now the requirement is to make one single field editable on that condition.

 

Can i achieve this using same client script or creating something new but keeping this Client Script.

 

When i am adding a line to make the field mandatory in same Client Script. The Client Script is not working at all.

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@aditiojha 

make the field which you want to be editable once you end the for loop

var fields = g_form.getEditableFields();

for (var x = 0; x < fields.length; x++) {

        g_form.setReadOnly(fields[x], true);

}

g_form.setReadOnly('fieldName', false);

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

View solution in original post

7 REPLIES 7

snag_araga
Tera Contributor

Hello @aditiojha ,

Try making all the fields read only and then add an if condition make that single field editable and then mandatory.

Script:

var fields = g_form.getEditableFields();

for (var x = 0; x < fields.length; x++) {

        g_form.setReadOnly(fields[x], true);

}
if(condition){
g_form.setReadOnly("field_name", false);

g_form.setMandatory("field_name", true);

}

Community Alums
Not applicable

Hi @aditiojha ,

The Best approach is to use UI Policy here to make fields read only.

Thanks Sandeep. There were lot of fields and lots of conditional hide and display hence using UI policy was not possible. However another solution to this post helped me using the same client script. Appreciate your time and response.

Ankur Bawiskar
Tera Patron
Tera Patron

@aditiojha 

make the field which you want to be editable once you end the for loop

var fields = g_form.getEditableFields();

for (var x = 0; x < fields.length; x++) {

        g_form.setReadOnly(fields[x], true);

}

g_form.setReadOnly('fieldName', false);

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