Show Sub account if Account has child account else hide the field

Samiksha2
Mega Sage

Hi All,

 

I have written a script include and OnLoad client script.

OnChange it is working but the same code is not working for OnLoad.

https://www.servicenow.com/community/csm-forum/show-sub-account-if-account-has-child-account-else-hi... 

 

Script Include:
var getSubProduct = Class.create();
getSubProduct.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getSubAccount: function() {

        var subacc = this.getParameter('sysparm_account');
		gs.log('Account'+subacc);
        var acc = new GlideRecord('customer_account');
        acc.addEncodedQuery('account_parent=' + subacc);
        acc.query();
        if (acc.next()) {
			gs.log("I am in");
            var answer = 'Yes';
        } else {
			gs.log("I am out");
            answer = 'No';
        }
        return answer;
    },
    type: 'getSubProduct'
});

Client Script:
function onLoad() {
    // var subAccount = g_form.getValue("u_sub_account");
    var account = g_form.getValue("account");
	
    var ga = new GlideAjax('getSubProduct');
    ga.addParam('sysparm_name', 'getSubAccount');
    ga.addParam('sysparm_account', account);
    ga.getXML(myCallBack);

    function myCallBack(response) {
        var cc = response.responseXML.documentElement.getAttribute('answer');
        alert(cc);
        if (cc == 'Yes') {
            g_form.setMandatory("u_sub_account", true);
            //g_form.setDisplay("u_sub_account", true);
        } else {
            g_form.setMandatory("u_sub_account", false);
            // g_form.setDisplay("u_sub_account", false);
        }
    }

}

Nothing is coming in the logs

 

Thanks,

Samiksha 

3 REPLIES 3

Anil Lande
Kilo Patron

Hi,

Are you getting the alert message from Client script callback function?

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande

As you have 'Account' variable on form, is that variable containing default value when the form is loaded?

 

Also why you have created separate onLoad client script, instead you can remove the isLoading condition from the first if of your onChange Client script.

Your onChange Client script should be like :

function onChange(control, oldValue, newValue, isLoading) {
    if (newValue == '') {
        return;
    }

    var subAccount = g_form.getValue("u_sub_account");
    var account = g_form.getValue("account");

    var ga = new GlideAjax('account_details');
    ga.addParam('sysparm_name', 'getSubAccount');
    ga.addParam('sysparm_account', account);
    ga.getXML(myCallBack);
    //ga.getXMLAnswer(getAccount);
    function myCallBack(response) {
        var cc = response.responseXML.documentElement.getAttribute('answer');
        alert(cc);
        if (cc == 'Yes') {
            g_form.setMandatory("u_sub_account", true);
        }
    }

    g_form.clearValue("u_sub_account");
}
Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande

Hi @Anil Lande ,

 

Thank you for replying.

I pasted the function in different script include then it is working. I have given snc_internal role I guess this  issue is related to ACL.

 

Thanks,

Samiksha