Duplicate Options Displaying Despite Script Selection in Table

NikhithaNikki
Tera Contributor

We are currently facing an issue where duplicate options are being displayed in a dropdown/select field, even though the values are being set through a combination of Script Include and Client Script from a specific table. The expectation is that once an option is selected or populated via script, it should not appear again as a duplicate in the list.
client script:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || !newValue) return;

    var ga = new GlideAjax('QuestDataDomainHelper');
    ga.addParam('sysparm_name', 'getChildCIsByDomains');
    ga.addParam('sysparm_parentSysId', newValue);

    ga.getXMLAnswer(function(response) {
        var result = JSON.parse(response);
        var fieldMap = {
            va: 'what_is_the_location_of_the_individuals_whose_data_is_in_the_platform_select_all_that_apply',
            ta: 'what_categories_of_data_are_collected_stored_or_processed_by_the_platform_scroll_to_see_all_options_',
            qa: 'what_is_the_data_storage_location_select_all_that_apply',
            ka: 'what_is_the_data_processing_location_select_all_that_apply',
            la: 'is_data_shared_internally_within_quest_or_its_affiliates_or_externally_outside_of_quest_if_both_sele',
            wa: 'is_data_accessed_internally_by_employees_of_quest_or_its_affiliates_or_externally_by_non_quest_emplo',
            oa: 'indicate_if_data_in_the_platform_is_identifiable_and_or_what_type_of_deidentification_is_in_place_se',
            aa: 'select_all_types_of_quest_corporate_data_that_are_collected_stored_or_processed_in_the_platform',
            ba: 'is_artificial_intelligence_and_or_machine_learning_used_in_the_platform_if_both_select_both',
            ca: 'where_is_the_location_of_the_external_individuals',
            za: 'what_types_of_individuals_have_data_within_the_platform_scroll_to_see_all_options_select_all_that_ap',
            da: 'who_is_data_transferred_to_shared_with_or_accessed_by'
        };

        for (var key in fieldMap) {
            if (result[key] && result[key].length > 0) {
                g_form.setValue(fieldMap[key], result[key].join(', '));
            }
        }
    });
}
script include:
var QuestDataDomainHelper = Class.create();
QuestDataDomainHelper.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getChildCIsByDomains: function() {

        var parentSysId = this.getParameter('sysparm_parentSysId');

        var domainMap = {
            va: QuestCMDBConstants.DATA_DOMAIN_LOCATION_OF_INDIVIDUALS ,
            ta: QuestCMDBConstants.DATA_DOMAIN_CATEGORIES_OF_DATA,
            qa: QuestCMDBConstants.DATA_DOMAIN_DATA_STORAGE_LOCATION,
            ka: QuestCMDBConstants.DATA_DOMAIN_INTERNAL_DATA_PROCESSING,
            la: QuestCMDBConstants.DATA_DOMAIN_DATA_SHARING,
            wa: QuestCMDBConstants.DATA_DOMAIN_DATA_ACCESS,
            oa: QuestCMDBConstants.DATA_DOMAIN_TYPE_OF_DATA,
            aa: QuestCMDBConstants.DATA_DOMAIN_QUEST_CORPORATE_DATA,
            ba: QuestCMDBConstants.DATA_DOMAIN_AI_MACHINE_LEARNING,
            ca: QuestCMDBConstants.DATA_DOMAIN_EXTERNAL_DATA,
            da: QuestCMDBConstants.DATA_DOMAIN_TRANSFER_SHARE_ACCESS,
            za: QuestCMDBConstants.DATA_DOMAIN_INDIVIDUALS,

        };

        var allResults = {};

        for (var key in domainMap) {
            var domainId = domainMap[key];
            var results = [];

            var relGR = new GlideRecord("cmdb_rel_ci");
            relGR.addQuery("parent", parentSysId);
            relGR.addEncodedQuery("child.ref_cmdb_ci_information_object.sn_apm_data_domain=" + domainId);
            relGR.query();

            while (relGR.next()) {
                var displayValue = relGR.child.getDisplayValue();
                var da = relGR.child.getValue();
                gs.log("Nikhitha" + displayValue + " " + relGR.child.getValue("sys_id"));

                results.push(displayValue);
            }

            allResults[key] = results;
        }

        return JSON.stringify(allResults);
    },

    type: 'QuestDataDomainHelper'
});
please help me


1 ACCEPTED SOLUTION

Murthy Ch
Giga Sage

Hi @NikhithaNikki 

The main issue is here because of setting the displayValue instead of the record sys_id. Updated the while loop below, try this:

 while (relGR.next()) {
     var recordSysID = relGR.child.getValue('sys_id');
     gs.info("Nikhitha" + displayValue + " " + recordSysID);
     results.push(recordSysID);
 }

After testing you can remove the comments and can directly push the value into results array.

Also, remember whenever you're inserting the values into list_collector via script you need to make sure to set bothe the value (sys_id) and display value of the record. Refer below screenshot:

MurthyCh_0-1755667566508.png

Hope this resolves your issue

Thanks,
Murthy

View solution in original post

6 REPLIES 6

NikhithaNikki_0-1756658239692.png

it returns only one value like this i want 2 if 2 values was there

Ankur Bawiskar
Tera Patron
Tera Patron

@NikhithaNikki 

so it's list collector with choices?

Script Include: check before adding if already present

var QuestDataDomainHelper = Class.create();
QuestDataDomainHelper.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getChildCIsByDomains: function() {

        var parentSysId = this.getParameter('sysparm_parentSysId');

        var domainMap = {
            va: QuestCMDBConstants.DATA_DOMAIN_LOCATION_OF_INDIVIDUALS,
            ta: QuestCMDBConstants.DATA_DOMAIN_CATEGORIES_OF_DATA,
            qa: QuestCMDBConstants.DATA_DOMAIN_DATA_STORAGE_LOCATION,
            ka: QuestCMDBConstants.DATA_DOMAIN_INTERNAL_DATA_PROCESSING,
            la: QuestCMDBConstants.DATA_DOMAIN_DATA_SHARING,
            wa: QuestCMDBConstants.DATA_DOMAIN_DATA_ACCESS,
            oa: QuestCMDBConstants.DATA_DOMAIN_TYPE_OF_DATA,
            aa: QuestCMDBConstants.DATA_DOMAIN_QUEST_CORPORATE_DATA,
            ba: QuestCMDBConstants.DATA_DOMAIN_AI_MACHINE_LEARNING,
            ca: QuestCMDBConstants.DATA_DOMAIN_EXTERNAL_DATA,
            da: QuestCMDBConstants.DATA_DOMAIN_TRANSFER_SHARE_ACCESS,
            za: QuestCMDBConstants.DATA_DOMAIN_INDIVIDUALS,

        };

        var allResults = {};

        for (var key in domainMap) {
            var domainId = domainMap[key];
            var results = [];

            var relGR = new GlideRecord("cmdb_rel_ci");
            relGR.addQuery("parent", parentSysId);
            relGR.addEncodedQuery("child.ref_cmdb_ci_information_object.sn_apm_data_domain=" + domainId);
            relGR.query();

            var valuesSeen = {};

            while (relGR.next()) {
                var value = relGR.getValue('child');
                var displayValue = relGR.child.getDisplayValue();
                if (!valuesSeen[value]) { // Deduplication
                    results.push({
                        value: value,
                        label: displayValue
                    });
                    valuesSeen[value] = true;
                }
            }

            allResults[key] = results;
        }

        return JSON.stringify(allResults);
    },

    type: 'QuestDataDomainHelper'
});

Client Script: No change

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