Client script to make a field read only based on a condition

test1231998
Tera Contributor

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

 

23 REPLIES 23

Hi Ankur,

My requirement is when i create a new incident and if business Service is Unify,and once the form is submitted it should show the field business service as read only.

I checked for ui policies also,there isnt any ui policy on the field

Hi,

try adding alerts in that onLoad client script

function onLoad() {
    //Type appropriate comment here, and begin script below
        var bs = g_form.getValue('business_service');
        if (bs == '4ba556dc1b04a4101f99db15ec4bcb0b' && !g_form.isNewRecord()) {

alert('inside');
            g_form.setReadonly('business_service', true);
        }
}

If this setup is in your personal instance and you are ok to share details url and some admin credentials then share here -> ankurb.snow@gmail.com

Regards
Ankur

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

Hi Ankur,

Thanks for the consideration,but this is not my personal instance.

I tried adding the alert.it isnt taking that as well.

if possible Could you please tell me if there is any other way I can achieve this requirement?

Hi,

you can use field level WRITE ACL on that field for Incident Table

In the Conditions give

Business Service [IS NOT] Unify

find_real_file.png

Regards
Ankur

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

Hi Ankur,

I tried the alert again

Its taking the alert inside the loop but the field not becoming mandatory.