Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

I am trying to Find Duplicate Fields on form in different section.

sachingawas
Kilo Expert

I want to find a Duplicate field on Form in different section using script,

For Example Assigned To field can be added to two sections on incident form, So I want to find How many tables have such duplicate fields added on form through Script.

1 ACCEPTED SOLUTION

Chris_Hann
ServiceNow Employee

Try this script:



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)


                gs.print('DupField: ' + tableName + '.' + element + ', View: ' + view + ' (Sections: ' + sectionNames + ')');


    }


}




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 + '');


    }




    return sectionIDs;


}


View solution in original post

18 REPLIES 18

Thank You Chris.. You are genius...


Nine years on and this is still a really nice script.  Thanks for sharing this!

Where do you run this script?

You'd put it in a background script. 
Just be careful if you're running it inside a scope because gs.print is likely to not work.
Consider swapping the gs.print for gs.info.