- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā02-27-2023 11:15 PM
Hi ,
we are doing inbound integration, so when we receive inbound json we are updating Incident state as in progress through REST API and we need to make the state field read-only.
how to make the state field read-only?
Please guide me
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā02-27-2023 11:31 PM
Hi @String
Make A Field Read Only With A Client Script
This is going to be the option for people who are comfortable writing code, or want to get more experience in that area.
With a few lines of code, you can easily set one or many fields read only. This can be based off of the values of other fields, who a user is, or any sort of conditional logic that you can script out.
Remember that this is a client script, so it can only be used client side. This will not work in a Business Rule or Script Include.
To make a field read-only using a client script in ServiceNow, you can use the setDisabled method of the g_form object. Here is an example of how you can use this method to make a field read-only:
- In the ServiceNow platform, navigate to the form that you want to modify.
- Click on the āCustomizeā button in the top right corner of the form.
- In the āCustomize Formā window, click on the āClient Scriptsā tab.
- Click the āNewā button to create a new client script.
- In the āScriptā field, enter the following code:
Replace āfield_nameā with the actual name of the field that you want to make read-only.
- Click the āSaveā button to save the client script.
This client script will run when the form is loaded, and it will use the setDisabled method of the g_form object to make the specified field read-only.
Alternatively, you can build some logic into this client script, to only make a field read only, under certain circumstances.
For example,
Note: You can also use the setReadOnly method of the g_form object to make a field read-only, but this method has been deprecated and is not recommended for use. The setDisabled method should be used instead.
When in doubt you should usually try to create a UI Policy/UI Policy Action, because itās easier for people to go there than to look through code.
If you have ServiceNow admins that arenāt comfortable using code, then they wonāt be able to go in and modify your scripts. But this all depends on your team and company.
Make A Field Read Only With An ACL
This is going to be the most secure way to make a field read only, by creating an ACL.
ACLās can be created to allow/prohibit CRUD operations, based on a users role. These can apply to records, fields and tables.
Remember that you have to elevate your role to āsecurity_adminā to work with ACLās.
So what is an ACL, in more detail?
An Access Control List (ACL) is a set of rules that determine whether a user or group has the ability to perform certain actions, such as reading, writing, or deleting records in a table. You can use an ACL to make a field read-only in ServiceNow by creating an ACL rule that denies write access to the field for the desired users or groups.
We can dive into the step by step process of how to create an ACL below.
Hereās how you can create an ACL rule to make a field read-only in ServiceNow:
- In the ServiceNow platform, navigate to the āSystem Security > Access Controlā menu.
- Click the āNewā button to create a new ACL rule.
- In the āNameā field, enter a name for the ACL rule.
- In the āTableā field, select the table that contains the field that you want to make read-only.
- In the āOperationā field, select āWriteā.
- In the āConditionā field, enter a condition that specifies the field that you want to make read-only. For example, if you want to make the āfield_nameā field read-only, you can enter āfield_name != NULLā in the condition field.
- In the āActionā field, select āDenyā.
- In the āRolesā field, select the roles or groups that you want to deny write access to the field.
- Click the āSubmitā button to save the ACL rule.
This ACL rule will deny write access to the specified field for the specified roles or groups, effectively making the field read-only for those users.
All access in ServiceNow is fundamentally controlled by ACLās. By default ALL data in ServiceNow is locked down. You need to create ACLās to open up and allow users to interact with this data.
Thereās a really helpful ACL or Security Debugger tool in ServiceNow, that you can turn on to debug ACLās.
Note: If you want to make a field read-only for all users, you can set the āRolesā field to āAllā when creating the ACL rule. However, be aware that this will also make the field read-only for users with the āadminā role, so use caution when applying ACL rules to the āadminā role.
ACLās can be difficult to manage and create, but thereās a lot of power in understanding how they work.
Letās talk about UI Policies and how they can be configured to make fields read only in ServiceNow.
Make A Field Read Only With A UI Policy And UI Policy Action
These are the easiest and most straightforward to configure in ServiceNow.
Since they require no scripting and are super easy to build out ā this should be your go-to when trying to figure out how to make a field readonly in a certain circumstance.
All you do is define the conditions, when a field should be read only, and then make a UI Policy Action, for each of the fields youād like to make read only. All UI Policies are either true or false, and if itās true ā the related UI Policy Actions will be activated and kick in, doing whatever was set up.
You can make fields read only, visible and read only in a UI Policy Action.
Letās talk about how you build them.
Hereās how you can create a UI Policy and UI Policy action to make a field read-only in ServiceNow:
- In the ServiceNow platform, navigate to the āSystem UI > UI Policiesā menu.
- Click the āNewā button to create a new UI Policy.
- In the āNameā field, enter a name for the UI Policy.
- In the āTableā field, select the table that contains the field that you want to make read-only.
- In the āConditionā field, enter a condition that specifies when the UI Policy should be triggered. For example, you can enter a condition that specifies a specific field value, such as āstate=Closedā.
- In the āActionsā section, click the āNewā button to create a new UI Policy action.
- In the āTypeā field, select āRead Onlyā.
- In the āFieldsā field, enter the name of the field that you want to make read-only.
- Click the āSubmitā button to save the UI Policy action.
- Click the āSubmitā button again to save the UI Policy.
This UI Policy will be triggered when the specified condition is met, and it will set the specified field to read-only using a UI Policy action.
Itās a simple as that.
Itās a great feature because itās easy to setup and also to debug, if it ever comes to that.
I hope this will helps you
Thank you
Rajesh.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā02-27-2023 11:31 PM
Hi @String
Make A Field Read Only With A Client Script
This is going to be the option for people who are comfortable writing code, or want to get more experience in that area.
With a few lines of code, you can easily set one or many fields read only. This can be based off of the values of other fields, who a user is, or any sort of conditional logic that you can script out.
Remember that this is a client script, so it can only be used client side. This will not work in a Business Rule or Script Include.
To make a field read-only using a client script in ServiceNow, you can use the setDisabled method of the g_form object. Here is an example of how you can use this method to make a field read-only:
- In the ServiceNow platform, navigate to the form that you want to modify.
- Click on the āCustomizeā button in the top right corner of the form.
- In the āCustomize Formā window, click on the āClient Scriptsā tab.
- Click the āNewā button to create a new client script.
- In the āScriptā field, enter the following code:
Replace āfield_nameā with the actual name of the field that you want to make read-only.
- Click the āSaveā button to save the client script.
This client script will run when the form is loaded, and it will use the setDisabled method of the g_form object to make the specified field read-only.
Alternatively, you can build some logic into this client script, to only make a field read only, under certain circumstances.
For example,
Note: You can also use the setReadOnly method of the g_form object to make a field read-only, but this method has been deprecated and is not recommended for use. The setDisabled method should be used instead.
When in doubt you should usually try to create a UI Policy/UI Policy Action, because itās easier for people to go there than to look through code.
If you have ServiceNow admins that arenāt comfortable using code, then they wonāt be able to go in and modify your scripts. But this all depends on your team and company.
Make A Field Read Only With An ACL
This is going to be the most secure way to make a field read only, by creating an ACL.
ACLās can be created to allow/prohibit CRUD operations, based on a users role. These can apply to records, fields and tables.
Remember that you have to elevate your role to āsecurity_adminā to work with ACLās.
So what is an ACL, in more detail?
An Access Control List (ACL) is a set of rules that determine whether a user or group has the ability to perform certain actions, such as reading, writing, or deleting records in a table. You can use an ACL to make a field read-only in ServiceNow by creating an ACL rule that denies write access to the field for the desired users or groups.
We can dive into the step by step process of how to create an ACL below.
Hereās how you can create an ACL rule to make a field read-only in ServiceNow:
- In the ServiceNow platform, navigate to the āSystem Security > Access Controlā menu.
- Click the āNewā button to create a new ACL rule.
- In the āNameā field, enter a name for the ACL rule.
- In the āTableā field, select the table that contains the field that you want to make read-only.
- In the āOperationā field, select āWriteā.
- In the āConditionā field, enter a condition that specifies the field that you want to make read-only. For example, if you want to make the āfield_nameā field read-only, you can enter āfield_name != NULLā in the condition field.
- In the āActionā field, select āDenyā.
- In the āRolesā field, select the roles or groups that you want to deny write access to the field.
- Click the āSubmitā button to save the ACL rule.
This ACL rule will deny write access to the specified field for the specified roles or groups, effectively making the field read-only for those users.
All access in ServiceNow is fundamentally controlled by ACLās. By default ALL data in ServiceNow is locked down. You need to create ACLās to open up and allow users to interact with this data.
Thereās a really helpful ACL or Security Debugger tool in ServiceNow, that you can turn on to debug ACLās.
Note: If you want to make a field read-only for all users, you can set the āRolesā field to āAllā when creating the ACL rule. However, be aware that this will also make the field read-only for users with the āadminā role, so use caution when applying ACL rules to the āadminā role.
ACLās can be difficult to manage and create, but thereās a lot of power in understanding how they work.
Letās talk about UI Policies and how they can be configured to make fields read only in ServiceNow.
Make A Field Read Only With A UI Policy And UI Policy Action
These are the easiest and most straightforward to configure in ServiceNow.
Since they require no scripting and are super easy to build out ā this should be your go-to when trying to figure out how to make a field readonly in a certain circumstance.
All you do is define the conditions, when a field should be read only, and then make a UI Policy Action, for each of the fields youād like to make read only. All UI Policies are either true or false, and if itās true ā the related UI Policy Actions will be activated and kick in, doing whatever was set up.
You can make fields read only, visible and read only in a UI Policy Action.
Letās talk about how you build them.
Hereās how you can create a UI Policy and UI Policy action to make a field read-only in ServiceNow:
- In the ServiceNow platform, navigate to the āSystem UI > UI Policiesā menu.
- Click the āNewā button to create a new UI Policy.
- In the āNameā field, enter a name for the UI Policy.
- In the āTableā field, select the table that contains the field that you want to make read-only.
- In the āConditionā field, enter a condition that specifies when the UI Policy should be triggered. For example, you can enter a condition that specifies a specific field value, such as āstate=Closedā.
- In the āActionsā section, click the āNewā button to create a new UI Policy action.
- In the āTypeā field, select āRead Onlyā.
- In the āFieldsā field, enter the name of the field that you want to make read-only.
- Click the āSubmitā button to save the UI Policy action.
- Click the āSubmitā button again to save the UI Policy.
This UI Policy will be triggered when the specified condition is met, and it will set the specified field to read-only using a UI Policy action.
Itās a simple as that.
Itās a great feature because itās easy to setup and also to debug, if it ever comes to that.
I hope this will helps you
Thank you
Rajesh.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā02-28-2023 12:30 AM
great explanation, Thanks @Rajesh Chopade1
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā01-29-2025 12:16 PM
i'd like to add a small add on to the Client Script one. You cant set a field as read only or disabled if it is required. so you will need to something like this:
g_form.setMandatory('cmdb_ci', false);
g_form.setReadOnly('cmdb_ci', true);