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.

Instance Scan checks to find duplicate fields on same form.

aditi__patil
Tera Expert

I am trying to write an instance scan check to find duplicate fields in same form. I am unable to get the findings for the given script for a table check : 

 

 

(function(finding, current) {
    var duplicatedViewElementAgg = new GlideAggregate('sys_ui_element');
    duplicatedViewElementAgg.addQuery('type', '');
    duplicatedViewElementAgg.addQuery('sys_ui_section.name', '!=', '');
    duplicatedViewElementAgg.groupBy('element');
    duplicatedViewElementAgg.groupBy('sys_ui_section.name');
    duplicatedViewElementAgg.groupBy('sys_ui_section.view');
    duplicatedViewElementAgg.addAggregate('COUNT');
    duplicatedViewElementAgg.addHaving('COUNT', '>', '1');
    duplicatedViewElementAgg.query();

    while (duplicatedViewElementAgg.next()) {
        var tableName = duplicatedViewElementAgg.getValue('sys_ui_section.name');
        var view = duplicatedViewElementAgg.getValue('sys_ui_section.view');
        var element = duplicatedViewElementAgg.getValue('element');
        checkFormsForView(view, tableName, element);
    }

    function checkFormsForView(view, tableName, element) {
        var formGR = new GlideRecord('sys_ui_form');
        formGR.addQuery('name', tableName);
        formGR.addQuery('view', view);
        formGR.addQuery('sys_user', '');
        formGR.query();

        while (formGR.next()) {
            var sectionIDs = getSectionIDs(formGR);
            var hasNoSections = sectionIDs.length == 0;
            if (hasNoSections) continue;

            var sectionNames = getSectionsNamesForElement(element, sectionIDs);
            var isInMultipleSections = sectionNames.length >= 2;
            if (isInMultipleSections) {
                finding.increment();
                finding.count = sectionNames.length;
                gs.print('DupField: ' + tableName + '.' + element + ', View: ' + view + ' (Sections: ' + sectionNames.join(', ') + ')');
            }
        }
    }

    function getSectionsNamesForElement(element, sectionIDs) {
        var sectionGR = new GlideRecord('sys_ui_element');
        sectionGR.addQuery('element', element);
        sectionGR.addQuery('sys_ui_section', 'IN', sectionIDs);
        sectionGR.query();
        var captions = [];

        while (sectionGR.next()) {
            var sectionCaption = sectionGR.sys_ui_section.getDisplayValue();
            if (sectionCaption == '') sectionCaption = 'DEFAULT';
            captions.push(sectionCaption);
        }

        return captions;
    }

    function getSectionIDs(formGR) {
        var sectionIDs = [];
        var formSectionGR = new GlideRecord('sys_ui_form_section');
        formSectionGR.addQuery('sys_ui_form', formGR.getUniqueValue());
        formSectionGR.query();

        while (formSectionGR.next()) {
            sectionIDs.push(formSectionGR.sys_ui_section + '');
        }

    }
})(finding, current);
0 REPLIES 0