set fields to read only when caller changes

ServNowDev
Tera Guru

I have a client script that is onChange when the caller field is changed it populates a bunch of other fields on the form like end user_ Phone , dept, address, title, location, work status etc. I want to have the client script after the AJAX is done populating to check if the fields are populated if so set them to read only and if now leave them open. 

1. issue is that the fields are not getting set to read only until the caller field is changed a second time so the inital change it is not working 

2. some field read only for instance the phone in one user is populated then if the user is changed it stays read only even if the changed users phone is not populated. 

 

here is my code so far 

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {



    var additionalInfoFields = ['user_email', 'user_phone', 'user_status', 'user_building', 'user_room'];
    additionalInfoFields.forEach(function(fieldName) {
        var fieldValue = g_form.getValue(fieldName) ? true : false;
        g_form.setReadOnly(fieldName, fieldValue);


    });





}
3 REPLIES 3

Hemanth M1
Giga Sage
Giga Sage

Hi @ServNowDev ,

 

Why don't you set fields read only during AJAX response, check if response has a value if yes update and set the field read only

 

Also check if any these fields read only on the dictionary level( By default) or any other UI Policies/client scripts/ACLs conflicting

 

You may also take care when form loads (Onload if its needed)

 

 

Accept and hit Helpful if it helps.

Thank you,
Hemanth
Certified Technical Architect (CTA), ServiceNow MVP 2024, 2025

Brian Lancaster
Tera Sage

Are these the correct field names? They do not appear to be out of the box fields for incident (I'm guessing incident as that is the only table I can this of with a caller field). Since these are not out of the box they should have u_ at the beginning of the name.

these are custom field and used as example from the real instance.