We're reclaiming inactive PDIs to keep them available for active builders. Learn what's changing, who's affected, and how to protect your work. Read More

All records in m2m table not showing in variable set

matthew_hughes
Kilo Sage

In ServiceNow, I've created the following script include function to view all records in the u_m2m_depts_config_items table:

existing_org: function() {

        //Setup the parameter for the selected Business Application
        var busApp = this.getParameter('sysparm_app');

        //Setup the array for returning the list of child departments
        var dept_data = [];

        //GlideRecord query to check if there are any entries in the 'M2m Depts Config Items' with the selected Business Application
        var grConsumerRecordEntries = new GlideRecord('u_m2m_depts_config_items');
        grConsumerRecordEntries.addQuery('u_configuration_item', busApp);
        grConsumerRecordEntries.query();

        //whilst grConsumerRecordEntries is looking for records
        while (grConsumerRecordEntries.next()) {

            //Get the Sys ID of the current Department
            var departmentSysId = grConsumerRecordEntries.getValue('u_department');

            //Retrieve the value of the 'ID' field of the department record
            var grDepartmentRecord = new GlideRecord('cmn_department');
            var departmentID;
            if (grDepartmentRecord.get(departmentSysId)) {
                departmentID = grDepartmentRecord.getValue('id');
            }

            //Setup the variable to pass the value of the Business Unit/Function
            var businessUnitFunction;

            //Check if the current department has a parent asssociated with it
            var grParentDepartment = new GlideRecord('cmn_department');
            grParentDepartment.addQuery('sys_id', departmentSysId);
            grParentDepartment.addQuery('parent', '!=', '');
            grParentDepartment.query();

            //If there is a parent, assign the value of the parent department to 'businessUnitFunction'
            if (grParentDepartment.next()){
                businessUnitFunction = grParentDepartment.getValue('parent').toString();
            }

            //Otherwise set the current department to 'businessUnitFunction'
            else {
                businessUnitFunction = departmentSysId;
            }

            //The 'dept_data' array pushes the required data and assignes these to the three variables from the 'consumer_orgs_list' variable set
            dept_data.push({
                consumer_organisation_id: departmentID,
                consumer_organisation: departmentSysId,
                business_unit_function: businessUnitFunction
            });
        }

        return JSON.stringify(dept_data);
 
I'm using the below onChange client script when someone updates the Business Application field:
function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    //If the Business Application field is not empty, retreive the list of assigned consumer organisations associated with the Business Application from the 'existing_org' function within the 'LBGConsumOrgsCatItem' Script Include
    if (newValue != '') {  
        var gaExistingConsumers = new GlideAjax('LBGConsumOrgsCatItem');
        gaExistingConsumers.addParam('sysparm_name', 'existing_org');
        gaExistingConsumers.addParam('sysparm_app', newValue);
        gaExistingConsumers.getXML(parseScript);
    }

}

//Populate the rows in the 'consumer_orgs_list' variable set with the list of existing consumer organisation entries
function parseScript(response) {
    var answer = response.responseXML.documentElement.getAttribute("answer");
    g_form.setValue("consumer_orgs_list", answer);

}
 
However, what I've noticed is that for some Business Application records it works, but for other it doesn't even though there are records in the u_m2m_depts_config_items table:
 
Working example:
matthew_hughes_0-1782471191248.png

 

 

matthew_hughes_2-1782471218984.png

 

Non working example:

matthew_hughes_3-1782471289719.png

 

matthew_hughes_4-1782471312586.png

 

I was just wondering if anyone knows what the potential issue could be.

 

0 REPLIES 0