I Was facing the issue to Query the variables on the RITM to the script include

sekhar6
Tera Contributor

Hai ,

I Have written the script include and i want to Query the variables data which was filled . i was facing the challenge so can you please help me on this .

from the attached image , i want get query  those two variables data
SCRIPT INCLUDE : 

var ProjectAccess = Class.create();
ProjectAccess.prototype = {
    initialize: function() {},

    createJson: function(ritmID) {
        var requestJson = {};

        // Retrieve the sc_req_item record by its ID
        var ritmGR = new GlideRecord('sc_req_item');
        if (ritmGR.get(ritmID)) {
            // Add Ritm_number to the JSON object
            requestJson.Ritm_number = ritmGR.number.getDisplayValue();

            // Initialize array to store AD groups
            var ad_group = [];

            // Retrieve contract number and roles from the RITM record variables
            var contractNumber = ritmGR.variables.contract_number;
            var roles = ritmGR.variables.roles; // Assuming `roles` contains the roles to query

            if (contractNumber && roles) {
                // Split roles into an array if it's a comma-separated string
                var roleArray = roles.toString().split(',');

                // Query `u_automation_variables_lookups` for each role
                var avl = new GlideRecord('u_automation_variables_lookups');
                avl.addQuery('u_access_contract_number', contractNumber); // Filter by contract number
                avl.addQuery('u_access_display_role', 'IN', roleArray); // Match roles dynamically
                avl.query();

                // Collect AD groups from the matched records
                while (avl.next()) {
                    if (avl.u_access_ad_group) {
                        ad_group.push(avl.u_access_ad_group.toString());
                    }
                }
            } else {
                gs.log('Contract number or roles are missing in RITM: ' + ritmID);
            }

            // Add the AD groups array to the JSON object
            requestJson.Ad_group = ad_group;

            // Add other properties to the JSON object
            requestJson.Requestor = ritmGR.opened_by.getDisplayValue();
            requestJson.Action = ritmGR.u_classification.getDisplayValue();

            // Check for the associated task assigned to Ansible Tower
            var ansiblesTower = '26554ff3fbf55250e42af741aeefdca9'; // sys_id of Ansible Tower
            var taskGR = new GlideRecord('sc_task');
            taskGR.addQuery('request_item', ritmID);
            taskGR.addQuery('assigned_to', ansiblesTower);
            taskGR.query();

            // Add SCTASK number if found
            if (taskGR.next()) {
                requestJson.Sctask_number = taskGR.number.getDisplayValue();
            }

            // Convert the JSON object to a JSON string
            var requestBody = new global.JSON().encode(requestJson);

            // Return the JSON string
            return requestBody;
        } else {
            // Log an error if the RITM record is not found
            gs.log('No record found for RITM ID: ' + ritmID);
            return null;
        }
    },

    type: 'ProjectAccess'
};
1 REPLY 1

Ankur Bawiskar
Tera Patron
Tera Patron

@sekhar6 

try this. I hope this script include function is called from server side and you are sending RITM sysId

try running the function in background script by passing RITM sysId and verify the results

var ProjectAccess = Class.create();
ProjectAccess.prototype = {
    initialize: function() {},

    createJson: function(ritmID) {
        var requestJson = {};

        // Retrieve the sc_req_item record by its ID
        var ritmGR = new GlideRecord('sc_req_item');
        if (ritmGR.get(ritmID)) {
            // Add Ritm_number to the JSON object
            requestJson.Ritm_number = ritmGR.number.getDisplayValue();

            // Initialize array to store AD groups
            var ad_group = [];

            // Retrieve contract number and roles from the RITM record variables
            var contractNumber = ritmGR.variables.contract_number;
            var roles = ritmGR.variables.roles; // Assuming `roles` contains the roles to query

            if (contractNumber && roles) {
                // Split roles into an array if it's a comma-separated string
                var roleArray = roles.toString().split(',');

                // Query `u_automation_variables_lookups` for each role
                var avl = new GlideRecord('u_automation_variables_lookups');
                avl.addQuery('u_access_contract_number', contractNumber); // Filter by contract number
                avl.addQuery('u_access_display_role', 'IN', roleArray); // Match roles dynamically
                avl.query();

                // Collect AD groups from the matched records
                while (avl.next()) {
                    if (avl.u_access_ad_group) {
                        ad_group.push(avl.u_access_ad_group.toString());
                    }
                }
            } else {
                gs.log('Contract number or roles are missing in RITM: ' + ritmID);
            }

            // Add the AD groups array to the JSON object
            requestJson.Ad_group = ad_group.toString();

            // Add other properties to the JSON object
            requestJson.Requestor = ritmGR.opened_by.getDisplayValue();
            requestJson.Action = ritmGR.u_classification.getDisplayValue();

            // Check for the associated task assigned to Ansible Tower
            var ansiblesTower = '26554ff3fbf55250e42af741aeefdca9'; // sys_id of Ansible Tower
            var taskGR = new GlideRecord('sc_task');
            taskGR.addQuery('request_item', ritmID);
            taskGR.addQuery('assigned_to', ansiblesTower);
            taskGR.query();

            // Add SCTASK number if found
            if (taskGR.next()) {
                requestJson.Sctask_number = taskGR.number.getDisplayValue();
            }

            // Convert the JSON object to a JSON string
           
            // Return the JSON string
            return JSON.stringify(requestBody);
        } else {
            // Log an error if the RITM record is not found
            gs.log('No record found for RITM ID: ' + ritmID);
            return null;
        }
    },

    type: 'ProjectAccess'
};

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