Client script to make a field read only based on a condition
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-30-2020 11:07 PM
Hi,
I have a field called as business service(reference field)on my incident form,Whenever business service is Unify(one of the values in the field) .I have to make the field read only after submission of the form.
ie.whenever the business service is Unify then when i save the form the field business service should be read only.
I wrote the onsubmit client script as below.
function onSubmit() {
//Type appropriate comment here, and begin script below
var bs = g_form.getValue('business_service');
if (bs == '4ba556dc1b04a4101f99db15ec4bcb0b') {
g_form.setReadonly('business_service', true);
}
}
but it isnt working.
when i click on save it becomes read only once the form is saved it is not shown as read only
Please can anyone help me
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-30-2020 11:18 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-30-2020 11:23 PM
Hi,
Since you want it after submission
you should use onLoad and not onSubmit and check if this is not new record
function onLoad() {
//Type appropriate comment here, and begin script below
var bs = g_form.getValue('business_service');
if (bs == '4ba556dc1b04a4101f99db15ec4bcb0b' && !g_form.isNewRecord()) {
g_form.setReadonly('business_service', true);
}
}
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-30-2020 11:32 PM
Hi Ankur,
I tried the script which you have suggested but its still the same I am not able to see the field as read only .I have one query on why do we have to use the condition !g_form.isNewRecord().
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-30-2020 11:36 PM
Hi,
it checks if this is not a new record i.e. this is an existing record
Basically you want this
1) if an existing record is opened and if it has Unify business Service then that field should be readonly
the above script should work but please check any other UI policy is present on that field which is making it editable
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader