Background script to get 2 column values from 2 different table, from which one import set table

Sayali_9822
Tera Contributor

Background script to get 2 column values from 2 different tables, from which one import set table and they dont have any reference field. one table i have named as 'project' and other is 'primary'. In list view of project table i saw project number field, but that field is not part of project table its part of primary table and primary table is import set table. I wanted to fetch the records from project table who has atleast one attachment. Can you please tell me how we can fetch project number? I am getting sysid of those records but not getting project number.

2 REPLIES 2

Ankur Bawiskar
Tera Patron
Tera Patron

@Sayali_9822 

what's your business requirement?

your question is not clear.

share some screenshots

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Ankur Bawiskar
Tera Patron
Tera Patron

@Sayali_9822 

something like this but please enhance

(function() {
    var projectGR = new GlideRecord('project');
    projectGR.addQuery('sys_class_name', 'NOT LIKE', 'project%'); // Exclude child tables if needed
    projectGR.query();

    while (projectGR.next()) {
        // Check if project has at least one attachment
        var attachmentGR = new GlideRecord('sys_attachment');
        attachmentGR.addQuery('table_sys_id', projectGR.sys_id);
        attachmentGR.query();

        if (attachmentGR.hasNext()) {
            var projectName = projectGR.getValue('name'); // or whatever field you use for matching

            // Query primary import set table for matching project number
            var primaryGR = new GlideRecord('primary'); // Replace with actual import set table name
            primaryGR.addQuery('project_number', projectName); // Adjust field names as needed
            primaryGR.query();

            if (primaryGR.next()) {
                var projectNumber = primaryGR.getValue('project_number');
                gs.info('Project: ' + projectName + ', Project Number: ' + projectNumber);
            }
        }
    }
})();

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader