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

Uncle Rob
Kilo Patron

Let X = whatever field you're searching for.



Search the sys_ui_element table where element = x.


sys_ui_element.sys_ui_section tells you what section of the form its on.


sys_ui_element.sys_ui_section.name is the table.


sys_ui_element.sys_ui_section.view is the specific form view.


Thanks rfedoruk


But I want to know how can I access sections using script and fields on it. Feeling annoying. Tough Task for me


What do you want the end result of the script to be?


section.field name for table