How to set Read Only and Mandatory when Form load in Catalog client script.

Daemon5
Tera Expert

I would like to set Read Only and Mandatory when Form load in Catalog client script.

I have tried the following script, but it only executes ReadOnly.

 

 

function onLoad() {

	g_form.setReadOnly('fieldsname', true) && g_form.setMandatory('fieldsname', true); 

}

 

 

Any script improvements would be helpful!

2 ACCEPTED SOLUTIONS

Vrushali  Kolte
Mega Sage

Hello @Daemon5 ,

 

To address your question, it's important to understand that in ServiceNow, making a field both readonly and mandatory is not allowed. This might seem illogical, as a mandatory field implies user input, but if marked as readonly, it prevents form submission.

To achieve this combination, a two-step approach is recommended. First, set the field value as needed. Then, apply the field as both readonly and mandatory. This way, you can ensure the desired behavior without conflicting restrictions on the user's ability to submit the form.

 

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

Thanks,

Vrushali

View solution in original post

Brad Bowman
Kilo Patron
Kilo Patron

It is recommended to use a Catalog UI Policy for such activities, but in a Catalog Client Script you would separate these lines:

 

function onLoad() {
    g_form.setReadOnly('fieldsname', true);
    g_form.setMandatory('fieldsname', true); 
}

 

Just be aware that if the mandatory variable is empty it will not be enforced as readonly since the form cannot be submitted without a value, but it's fine to have both in the script/policy.  When the variable is populated it will be readonly the next time the form loads.

View solution in original post

5 REPLIES 5

Vrushali  Kolte
Mega Sage

Hello @Daemon5 ,

 

To address your question, it's important to understand that in ServiceNow, making a field both readonly and mandatory is not allowed. This might seem illogical, as a mandatory field implies user input, but if marked as readonly, it prevents form submission.

To achieve this combination, a two-step approach is recommended. First, set the field value as needed. Then, apply the field as both readonly and mandatory. This way, you can ensure the desired behavior without conflicting restrictions on the user's ability to submit the form.

 

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

Thanks,

Vrushali

Prathamesh G
Kilo Sage
Kilo Sage

Hello @Daemon5 , 

 

You can only make the the field either Mandatory or Read-Only. You cannot made these same operation on a single field. So that's why your field is only execute the Read-Only functionality.

 

If my answer solves your issue please mark it as 'Accepted Solution' and marked it as 'Helpful'.

 

 

Thanks,

Brad Bowman
Kilo Patron
Kilo Patron

It is recommended to use a Catalog UI Policy for such activities, but in a Catalog Client Script you would separate these lines:

 

function onLoad() {
    g_form.setReadOnly('fieldsname', true);
    g_form.setMandatory('fieldsname', true); 
}

 

Just be aware that if the mandatory variable is empty it will not be enforced as readonly since the form cannot be submitted without a value, but it's fine to have both in the script/policy.  When the variable is populated it will be readonly the next time the form loads.

SunilKumar_P
Giga Sage

Hi @Daemon5, You cannot make a variable / field Mandatory and ReadOnly at the same time if it doesn't have any value populated. You can only make a variable / field Mandatory and ReadOnly when you have a pre filled value in that field. If the variable / field has some auto populated value then it can be read only .

 

 

Regards,

Sunil