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

Ankur Bawiskar
Tera Patron
Tera Patron

@Community Alums 

So after adding logs in script include what's your analysis?

Is the function getting called? till which place script is working fine and printing logs?

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

Community Alums
Not applicable

Hi @Ankur Bawiskar , Script include gives result = true, means it is working fine

result = 'true';

  

Community Alums
Not applicable

Hi @Ankur Bawiskar ,

 

On background script I used result variable to store return value. It gives below output.

Amit136581_0-1695709135121.png

 

@Community Alums 

try to use getXMLAnswer()

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.getXMLAnswer(function(answer){
                        alert(answer);
			if (answer.toString() == 'true') {
				g_form.setMandatory('level_2_organization', true);
			} else {
				g_form.setMandatory('level_2_organization', false);
			}
		});
	}
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

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