The CreatorCon Call for Content is officially open! Get started here.

getting null value in client script

Community Alums
Not applicable

I am checking unique CI Group in table cmdb_ci_group and always getting null

wrote onchange catalog client script on variable level_1_organization and client callable SI

Here is the code

Client script:

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

    var level1Org = g_form.getValue('level_1_organization');

    if (level1Org) {
        var ga = new GlideAjax('CheckChildDepartment');
        ga.addParam('sysparm_name', 'getChildDepCIGroup');
        ga.addParam('sysparm_level1_org', level1Org);
        ga.getXML(childDepartmentParse);

        //ga.getXMLAnswer(function(answer){

        function childDepartmentParse(response) {

            var answer = response.responseXML.documentElement.getAttribute("answer");
            alert(answer);
            if (answer == 'true') {
                g_form.setMandatory('level_2_organization', true);
            } else {
                g_form.setMandatory('level_2_organization', false);
            }

        }
    }
}

 Script Include: checked in background script gives correct output

var CheckChildDepartment = Class.create();
CheckChildDepartment.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getChildDepCIGroup: function() {
        var level1Org = this.getParameter('sysparm_level1_org');
        // var result = '';
        //  var level10rg = 'bf1043f5db05f3c0105f3e0439961968';
        if (level10rg) {

            var grlevel1 = new GlideRecord("cmn_department");
            grlevel1.addQuery("sys_id", level10rg);
            grlevel1.query();
            if (grlevel1.next()) {
                var correlationID = grlevel1.correlation_id.toString();
                var grlevel2 = new GlideRecord("cmn_department");
                grlevel2.addEncodedQuery('u_active=true^correlation_idISNOTEMPTY');
                grlevel2.addEncodedQuery('parent.correlation_id=' + correlationID);
                grlevel2.query();
                var sysIdArray = []; // Array to store sys_ids   
                while (grlevel2.next()) {
                    sysIdArray.push(grlevel2.sys_id.toString());
                }
                // gs.print('total L1 department: ' + grlevel2.getRowCount());
                // gs.print('L1 department sysIdArray: ' + sysIdArray);
                if (sysIdArray.length > 1) {
                    //gs.print('L1 department sysIdArray > 1');
                    var ciGroupArray = []; // store cmdb_ci_group sys_ids  
                    var ciGroupUniqueArray = []; // store Unique cmdb_ci_group sys_ids  
                    var grciGroup = new GlideRecord("cmdb_ci_group");
                    grciGroup.addQuery('department.sys_id', 'IN', sysIdArray);
                    grciGroup.query();
                    while (grciGroup.next()) {
                        var cigroupSysID = grciGroup.getValue('assignment_group');
                        ciGroupArray.push(cigroupSysID);
                    }
                    // gs.print('tota1 L1 Dep groups: ' + grciGroup.getRowCount());
                    //  gs.print('L1 Dep groups ciGroupArray: ' + ciGroupArray);
                    //  gs.print('L1 Dep groups cigroupSysID: ' + cigroupSysID);
                    // Function to get Unique values
                    function getUnique(array) {
                        var uniqueArray = [];

                        // Loop through array values
                        for (i = 0; i < array.length; i++) {
                            if (uniqueArray.indexOf(array[i]) === -1) {
                                uniqueArray.push(array[i]);
                            }
                        }
                        return uniqueArray;
                    }

                    ciGroupUniqueArray = getUnique(ciGroupArray); //storing Unique cmdb_ci_group sys_ids in array

                    if (ciGroupUniqueArray.length > 1) {
                        //result = 'true';
                        return true;
                    } else {
                        return false;
                    }
                } else {
                    return false;
                }
            } else {
                return false;
            }
        } else {
            return false;
        }
    },
    type: 'CheckChildDepartment'
});

// gs.print('result: ' + result);
//   gs.print('ciGroupArray: ' + ciGroupArray);
// gs.print('ciGroupUniqueArray: ' + ciGroupUniqueArray);

 

1 ACCEPTED SOLUTION

@Community Alums 

try creating new script include and function and then call it

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

11 REPLIES 11

Community Alums
Not applicable

Hi @Ankur Bawiskar , I tried with above code but still it gives null alert.

 

Amit136581_0-1695710293175.png

 

@Community Alums 

it seems it's not getting inside any IF else statement so it's returning null

did you check inside which IF or ELSE it entered?

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Community Alums
Not applicable

Hi @Ankur Bawiskar ,

 

It is entered always first if else

  if (ciGroupUniqueArray.length > 1) {
                        //result = 'true';
                        return true;
                    } else {
                        return false;

below background script screenshot always enter 1st if else if it is true or false:

Amit136581_0-1695712925146.png

 

@Community Alums 

try creating new script include and function and then call it

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Community Alums
Not applicable

Thanks @Ankur Bawiskar , As you suggest I created new script include and function, Its working fine.