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.

Checkbox doesn't always fill in as the client script suggests

Reinaldo Vieira
Tera Contributor

Hello everyone.

 

I set up an onchange client script to fill a Checkbox field ('block_gestor_inativo') in case of none of the conditions within 2 IF are met (and in fact none of the conditions are met).

 

The catalog client script involves a search/capture on the server between data from the manager and the head (manager's manager) of the user logged into the instance.

 

I press F5 on the catalog item page and sometimes the checkbox appears filled in and sometimes it doesn't appear filled in.

 

What could be causing this? How do I solve it?

 

 

 

 

 

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

    //Type appropriate comment here, and begin script below
    g_form.getReference('manager', function(manager) {
        var idManager = manager.sys_id;
        var upn_prefix = manager.u_upn.substr(0, 4);
        var level = manager.title.substr(0, 1);
        var activeManager = manager.active;
        var statusManager = manager.locked_out;

        //Condições para filtrar o gerente: se ele estiver irregular nestes aspectos, escalamos o Head (gerente do gerente)
        if (upn_prefix == 'BAJA' || isNaN(level) || parseInt(level) > 7 || statusManager == 'true' || activeManager == 'false') {
            g_form.getReference('manager_manager', function(managerManager) {
                var idManagerManager = managerManager.sys_id;
                var upn_prefixManager = managerManager.u_upn.substr(0, 4);
                var levelManager = managerManager.title.substr(0, 1);
                var activeManagerManager = managerManager.active;
                var statusManagerManager = managerManager.locked_out;

                if (upn_prefixManager == 'BAJA' || isNaN(levelManager) || parseInt(levelManager) < 5 || statusManagerManager == 'true' || activeManagerManager == 'false') {
                    g_form.setValue('block_gestor_inativo', true);
                    alert('Gestores bloqueados');
                } else {
                    g_form.clearValue('block_gestor_inativo');
                    g_form.setValue('approval_group_1', idManagerManager);
                }
            });
        } else {
            g_form.clearValue('block_gestor_inativo');
            g_form.setValue('approval_group_1', idManager);
        }
    });
}

 

 

 

 

2 REPLIES 2

AnirudhKumar
Mega Sage

I'd try the below:

- Change 

g_form.setValue('block_gestor_inativo', true);

 to

g_form.setValue('block_gestor_inativo', 'true');

 

- replace getReference blocks with a GlideAjax-Script Include calls

- Add more alert statements and trace the flow of execution...

Hello, @AnirudhKumar 

 

Thanks for your help.

 

I agree with you that making a GlideAJAX is a better alternative. However, in this specific case the creation of a GlideAJAX/Script Include is put as the last option because it is a process that depends on the authorization of a third party.

 

What I did to solve this problem was to create an onLoad Client Script, taking what was already in the onChange Client Script, and finally adding a setTimeOut to delay the execution of the code by 3 seconds. There's a lot of information to process and sometimes the page load time doesn't match the code execution time.

 

It looks like this:

 

function onLoad() {
    setTimeout(checkApprovers, 3000);

}

function checkApprovers() {

    //Analisando dados do GERENTE
    g_form.getReference('manager', function(manager) {
        var idManager = manager.sys_id;
        var upn_prefix = manager.u_upn.substr(0, 4);
        var level = manager.title.substr(0, 1);
        var activeManager = manager.active;
        var statusManager = manager.locked_out;

        //Condições para filtrar o gerente: se ele estiver irregular em um destes aspectos, escalamos o Head (gerente do gerente)
        if (upn_prefix == 'BAJA' || isNaN(level) || parseInt(level) > 7 || statusManager == 'true' || activeManager == 'false') {

            //Analisando dados do HEAD
            g_form.getReference('manager_manager', function(managerManager) {
                var idManagerManager = managerManager.sys_id;
                var upn_prefixManager = managerManager.u_upn.substr(0, 4);
                var levelManager = managerManager.title.substr(0, 1);
                var activeManagerManager = managerManager.active;
                var statusManagerManager = managerManager.locked_out;

                //Se o Head também estiver irregular em um destes aspectos, o campo "Bloquear Gestor Inativo" é preenchido.
                if (upn_prefixManager == 'BAJA' || isNaN(levelManager) || parseInt(levelManager) < 5 || statusManagerManager == 'true' || activeManagerManager == 'false') {
                    g_form.setValue('block_gestor_inativo', true);
					
                } else {
                    //Se apenas o Gerente estiver irregular, quem vai aprovar a criação de SCTASK é o Head.
                    g_form.clearValue('block_gestor_inativo');
                    g_form.setValue('approval_group_1', idManagerManager);
                }
            });
        } else {
			//Se apenas o Head estiver irregular, quem vai aprovar a criação de SCTASK é o Gerente.
            g_form.clearValue('block_gestor_inativo');
            g_form.setValue('approval_group_1', idManager);
        }
    });
}