How to Fetch the 2 tables data from reference qualifier using script include in a catalog item field

Ramesh1532
Tera Contributor

Hi All ,

I have a requirement in catalog item for specific field(Contact) and referenced to contact table.

if i click on lookup, then it should show all the contacts and contact relationship (there is a custom table) data, but in type specification i can give as reference 1 table(ex:contact) but there is another table to fetch the data.

could you please explain the script include for gliding another table from array.

4 REPLIES 4

ersureshbe
Giga Sage
Giga Sage

Hi, I recommend to create a db view to merge the 2 tables and use the DB view in the reference qualifier to achieve your requirement.

Regards,
Suresh.

ersureshbe
Giga Sage
Giga Sage

Hi, I recommend to create a db view to merge the 2 tables and use the DB view in the reference qualifier to achieve your requirement.

Regards,
Suresh.

can we use db view in reference field?

Abhay Kumar1
Giga Sage

@Ramesh1532 To fulfill this requirement in ServiceNow, you can use a Script Include that queries both tables (contact and your custom relationship table) and returns a combined result.

Script Include will look like below and feel free to modify as needed:

var CustomContactLookup = Class.create();

CustomContactLookup.prototype = {

    initialize: function () {},

    getCombinedContacts: function () {

        var result = [];

        // Query the 'contact' table

        var contactGR = new GlideRecord('contact');

        contactGR.query();

        while (contactGR.next()) {

            result.push({

                sys_id: contactGR.getValue('sys_id'),

                name: contactGR.getValue('name'), // Update the field names as per your table

            });

        }

        // Query the custom relationship table

        var relationshipGR = new GlideRecord('your_custom_relationship_table');

        relationshipGR.query();

        while (relationshipGR.next()) {

            result.push({

                sys_id: relationshipGR.getValue('sys_id'),

                name: relationshipGR.getValue('related_name'), // Update the field names as per your table

            });

        }

        return result;

    },

    type: 'CustomContactLookup'

};

Hope this will help you.