Getting Error : Violations Found. Code is not saved ServiceNow

akshaybarapatre
Tera Contributor
I am getting Error : Violations Found. Code is not saved ServiceNow in below code while saving the Widget form.

(
function() {
    data.pageSize = 500;
    var dep = new GlideRecord('cmn_department');    

    if (input.id) {
        //Get selected departments;
        var sys_ids = [];
        var labels = [];
        var label = '';
        var selected =  new GlideRecord('cmn_department');  
        selected.addQuery('sys_id','IN',input.selected).addOrCondition('sys_id','IN',input.id);
        selected.orderBy('name');
        selected.query();
       
        while (selected.next()) {
            label = selected.name.toString();
            label = label.replace(/,/g,"&#44"); //Replace all commas with character code "&#44" so we can make a comma seperated list

            labels.push(label);
            sys_ids.push(selected.sys_id.toString());
        }
        data.sys_ids = sys_ids.join();
        data.labels = labels.join();
       
        return;
    }
   
    if (input.page) {
        data.treedata = [];
        var filter = input.filter.toLowerCase();
       
        if (filter)
                        data.pageSize = 500;

        var page = input.page - 1; //We want to be 0 indexed
        var start = (page * data.pageSize);
        var end = ((page + 1) * data.pageSize);

        dep.addNullQuery('parent');
        dep.orderBy('name');
        dep.chooseWindow(start, end,true);
        dep.query();
       
        if (!filter)
            data.count = dep.getRowCount();
        else
            data.count = 0;
        //console.log("Count " + data.count + " / " + data.pageSize);
        while (dep.next()) {
            var sys_id = dep.getValue('sys_id');
            var name = dep.getValue('name');

            var gr = new GlideRecord('cmn_department');
       
                //Only show if a child down below contains the search string in the name
                if (filter) {
                    var hasChildrenWithName = hasChildWithName(dep.sys_id,filter);
                    if (!hasChildrenWithName)
                            continue;
                }

            gr.get(sys_id);
   
            var node = {};
            node.label = name;
            node.id = sys_id;
            node.depID = gr.getValue('id');
            node.children = findChildren(gr,filter,false);
            if (!filter && name.indexOf('Topdanmark') != 0) //Collapse tree if showing full tree view
                node.collapsed = true;
            data.treedata.push(node);
            data.count++;
        }
    }

    function inObject(objects,property,toSearch) {
        var found = false;
        if (objects.length == 0)
            return false; //No need to search an empty object
        for(var i=0; i<objects.length; i++) {
            for(var key in objects[i]) {
                if(objects[i][property][key].indexOf(toSearch)!=-1) {
                    found = true;
                }
            }
        }
        return found;
    }
   
    function getTopLevel(child_id) {
        var dep = new GlideRecord('cmn_department');
        dep.get(child_id);
        if (dep.parent.parent == '')
            return dep.parent;
        return getTopLevel(dep.parent);
    }
   
    function hasChildWithName(depID,filter) {
        var hasChilds = false;
       
        var dep = new GlideRecord('cmn_department');
        dep.get(depID);
       
        var child = new GlideRecord('cmn_department');
        child.addQuery('parent',dep.sys_id);
        child.query();
        hasChilds = child.hasNext();
       
        if (!hasChilds)
            return false;
       
        child.initialize(); //reset gr
        child.addQuery('parent',dep.sys_id);
        child.orderBy('name');
        child.query();
       
        while (child.next()) {
            if (child.name.toLowerCase().indexOf(filter) >= 0)
                return true;
           
            if (hasChildWithName(child.sys_id,filter))
            return true;
        }
        return false;
    }
   
    function findChildren(parent,filter,expand) {
        var result = [];

        var child = new GlideRecord('cmn_department');
        child.addQuery('parent',parent.sys_id);
        child.orderBy('name');
        child.query();  

        while (child.next()) {
            var filterMatch;
            if (filter)
                filterMatch = child.name.toLowerCase().indexOf(filter);
            //Only show if a this child or child down below contains the search string in the name
            if (filter && filterMatch == -1) {
                var hasChildrenWithName = hasChildWithName(child.sys_id,filter);
                if (!hasChildrenWithName)
                    continue;
            }

            var node = {};
            node.label= child.getValue('name');
            node.id = child.getValue('sys_id');
            node.depID = child.getValue('id');
            if (!filter && !expand) //Collapse tree if showing full tree view
                node.collapsed = true;

            if (filterMatch >=0)
                node.children = findChildren(child,'',true); //Show all childs if this child matches the filter
            else
                node.children = findChildren(child,filter,false);
            result.push(node);
        }
        return result;
    }
})();


5 REPLIES 5

J Siva
Tera Sage

Hi @akshaybarapatre 

Currently you've written all your children functions inside the main function, so move all the children functions outside the main function. Also, while calling the child function use this keyword in front of the function name.

Ex:

var hasChildrenWithName = this.hasChildWithName(child.sys_id,filter);

Regards,

Siva

 

Not working !

Ankur Bawiskar
Tera Patron
Tera Patron

@akshaybarapatre 

try this

function getAllChildren(parentId, filter) {
    var result = [];
    var toProcess = [parentId];

    while (toProcess.length > 0) {
        var currentParentId = toProcess.pop();
        var childGR = new GlideRecord('cmn_department');
        childGR.addQuery('parent', currentParentId);
        childGR.orderBy('name');
        childGR.query();

        while (childGR.next()) {
            var childName = childGR.getValue('name');
            if (!filter || childName.toLowerCase().indexOf(filter) >= 0) {
                result.push({
                    label: childName,
                    id: childGR.getValue('sys_id'),
                    depID: childGR.getValue('id'),
                    collapsed: true // or false, as needed
                });
                // Add this child's sys_id to the stack to process its children
                toProcess.push(childGR.getValue('sys_id'));
            }
        }
    }
    return result;
}

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

Not working, Basically the code which I have pasted in question. The same code running correctly in my PDI but in my Dev instance that is not working.