Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to make field readonly using client script(scoped application)

Jay N
Tera Contributor

Hi Experts,

 

We have a requirement to make reference field (bo_reference) readonly when check box (is bo inherited) is made false and form view should be module_data.

 

Have tried below onload and onchange scripts.

Onchange:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue == '') {
return;
}
var view = getView();
//alert(view);
var isBOInherited = g_form.getValue('is_bo_inherited');
//alert(isBOInherited);
if (view == 'module_data') {
//if (isBOInherited == 'true') {
if (newValue == 'true') {
g_form.setReadOnly('bo_reference', true);
//g_form.setValue('bo_reference', '');
alert(bo_reference +'bo_reference');
// g_form.setMandatory('bo_reference', 'true');
}
else if (isBOInherited == 'false') {
g_form.setMandatory('bo_reference', 'false');
g_form.setReadOnly('bo_reference', 'true');
}
}
}

 

 

OnLoad:

function onLoad() {

var view = getView();
//alert('view is = ' + view);
var isBOInherited = g_form.getValue('is_bo_inherited');
// alert(isBOInherited);
//bo_referenceISEMPTY
if (view == 'module_data') {
if (isBOInherited == false) {
g_form.setMandatory('bo_reference', false);
g_form.setReadOnly('bo_reference', true);
} else {
g_form.setReadOnly('bo_reference', false);
g_form.setMandatory('bo_reference', true);
}
}

//Type appropriate comment here, and begin script below

}

 

.

 

JayN_0-1669200315588.png

 

Thanks Jay.

5 REPLIES 5

Gunjan Kiratkar
Kilo Patron
Kilo Patron

Hi @Jay N 

On your onchange client script your alert is making issue : Use below script

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue == '') {
        return;
    }
    var view = getView();
    //alert(view);
    var isBOInherited = g_form.getValue('is_bo_inherited');
    //alert(isBOInherited);
    if (view == 'module_data') {
        //if (isBOInherited == 'true') {
        if (newValue == 'true') {
            g_form.setReadOnly('bo_reference', true);
            //g_form.setValue('bo_reference', '');
          //  alert(bo_reference + 'bo_reference');
            // g_form.setMandatory('bo_reference', 'true');
        } else if (isBOInherited == 'false') {
            g_form.setMandatory('bo_reference', 'false');
            g_form.setReadOnly('bo_reference', 'true');
        }
    }
}

 


Please Mark My Response as Correct/Helpful based on Impact
Regards,
Gunjan Kiratkar
2X ServiceNow MVP
Community Rising Star 2022
Youtube : ServiceNow Guy

Community Alums
Not applicable

Hi @Jay N 

 

Alert is causing an issue. comment out the highlighted alert or remove the quotes for bo_reference. If you want to print the bo_reference value, use g_form.getValue('bo_reference')

 

bhavaniy_0-1669200745982.png

 

Hi bhavaniy / Gunjan,

 

Thank you for the reply!

 

we have tried by removing the alert too, but it is not working as expected.

once we check the check box it is making bo_reference field to readOnly but when we again uncheck the check box then it is not making the bo_reference field to editable.

 

Regards,

Pravalika

Use below code for onchange 

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

    if (isLoading || newValue == '') {

        return;

    }

    var view = getView();

    //alert(view);

    var isBOInherited = g_form.getValue('is_bo_inherited');

    //alert(isBOInherited);

    if (view == 'module_data') {

        //if (isBOInherited == 'true') {

        if (newValue == 'true') {

            g_form.setReadOnly('bo_reference', true);

            //g_form.setValue('bo_reference', '');

          // alert(bo_reference + 'bo_reference');

            // g_form.setMandatory('bo_reference', 'true');

        } else if (isBOInherited == 'false') {

            g_form.setMandatory('bo_reference', 'false');

            g_form.setReadOnly('bo_reference', 'false');

        }

    }

}


Please Mark My Response as Correct/Helpful based on Impact
Regards,
Gunjan Kiratkar
2X ServiceNow MVP
Community Rising Star 2022
Youtube : ServiceNow Guy