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.

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
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

Provided value will be Table and Result should be Section and field name


Anurag Tripathi
Mega Patron
Mega Patron

Not sure what your aim is, but its highly recommended that you should not have same field on multiple forms on the one form.


-Anurag

My guess is that our friend has to clean up after someone else and needs to find all the places where the mess is.


yea....script will be messy


-Anurag

Hello Anurag this thing I know, But Sometimes happens if Lot of peoples working on same instance So I wan to know that can we access sections and fields on it via script