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.

Make field Mandatory using onchange client script

Divya Rajasekar
Tera Contributor

Hi,

 

I have a query.If i give configuration item and i should get it and check in relationship table child value is same as this ci and if the application is A then i should make a field B mandatory. Its working but when i change the value of Field B that mandatory is going and it becomes as non mandatory field? How to tackle this?

3 REPLIES 3

SANDEEP28
Mega Sage

@Divya Rajasekar  Can you share your client script code here 

 if (/*isLoading ||*/ newValue === '') {
        return;
    }
    if (oldValue != newValue) {
        g_form.setValue('field b', '');
        g_form.setValue('field b1', '');
    }

    //Type appropriate comment here, and begin script below
    var gn = new GlideAjax('testutils');
    gn.addParam('sysparm_name', 'checkpartci');
    gn.addParam('sysparm_sysid', newValue);
    gn.getXML(callBackFunction);

    function callBackFunction(response) {

        var answer = response.responseXML.documentElement.getAttribute("answer");
        //alert('from script include' + answer);
        if (answer == 'true') {
            g_form.setDisplay('field b', true);
            g_form.setMandatory('field b', true);
        }
        else if(answer == 'false'){
            g_form.setDisplay('field b', false);
            g_form.setMandatory('field b', false);
        }
    }

Anand Kumar P
Giga Patron

Hi @Divya Rajasekar ,

Use below script

if (newValue === '') {
    return;
}

if (oldValue != newValue) {
    g_form.setValue('field_b', '');
    g_form.setValue('field_b1', '');
}
var gn = new GlideAjax('testutils');
gn.addParam('sysparm_name', 'checkpartci');
gn.addParam('sysparm_sysid', newValue);
gn.getXML(callBackFunction);
function callBackFunction(response) {
    var answer = response.responseXML.documentElement.getAttribute("answer");
    // alert('from script include' + answer);
    if (answer == 'true') {
        g_form.setDisplay('field_b', true);
        g_form.setMandatory('field_b', true);
    } else if (answer == 'false') {
        g_form.setMandatory('field_b', true); 
        g_form.setDisplay('field_b', false);
    }
}

Thanks,

Anand