Help with 'Request Additional Approvers' UI Action for Change Management

RichardSaunders
Tera Guru

Hello, 

I am currently seeking assistance with implementing dynamic approval logic within a UI Action. The goal is to address a common issue where users submit changes without specifying a Service, Region, or CI. Instead of canceling the entire change, our Change Team wishes to add the missing Service, Region, or CI, which should then trigger the appropriate approvals.

To achieve this, I've developed a UI Action that queries records from 'task_ci,' 'task_cmdb_ci_service,' and 'u_m2m_regions_tasks' tables, deriving approval groups based on certain criteria. The script is intended to generate approval records for these groups and associate them with the change.

The script is part of a UI Action named 'RequestAdditionalApprovers.' However, when executed, it returns 'No records found,' despite confirming that there are associated services.

 

These are the 3 things that could determine approvers

Affected CI's: task_ci

Impacted Solutions: task_cmdb_ci_service

Region Impacted: u_m2m_regions_tasks

 

RichardSaunders_0-1702466876730.png

 

The approval groups are then queried from the above three tables from within the Approval Group Activity in the workflow. 

RichardSaunders_1-1702467148380.png

 

Approval Group Activity

 

 

// Set the variable 'answer' to a comma-separated list of group ids or an array of group ids to add as approvers.
//
// For example:
//       var answer = [];
//       answer.push('id1');
//       answer.push('id2');
var answer = [];
var groupArray = [];
//************************************************************
// High-Emergency
// affected CIs group approval. adds the sys_ids of the groups to an array groupArray
var ci_gr = new GlideRecord('task_ci');
ci_gr.addQuery('task', current.sys_id);
ci_gr.query();
while (ci_gr.next()){
	var ciResolve = new GlideRecord('ci_resolve_class');  
	ciResolve.addQuery('table_name', ci_gr.ci_item.sys_class_name.toString());
	ciResolve.query();
	if(ciResolve.next()){
		if(ciResolve.u_level_1_change_approver != '') {
			groupArray.push(ciResolve.u_level_1_change_approver.toString()); // add level 1 to groupArray for approval
		} 
		if(ciResolve.u_level_2_change_approver != '') {
			groupArray.push(ciResolve.u_level_2_change_approver.toString()); // add level 2 to groupArray for approval
		} 
		if(ciResolve.u_level_3_change_approver != '') {
			groupArray.push(ciResolve.u_level_3_change_approver.toString()); // add level 3 to groupArray for approval
		} 
	}
}
//************************************************************
// impacted services group approval 
var svc_gr = new GlideRecord('task_cmdb_ci_service');
svc_gr.addQuery('task', current.sys_id);
svc_gr.query();
while (svc_gr.next()){
	if(svc_gr.cmdb_ci_service.u_level_1_change_approver != '') {
		groupArray.push(svc_gr.cmdb_ci_service.u_level_1_change_approver.toString()); // add level 1 to groupArray for approval
	}
	if(svc_gr.cmdb_ci_service.u_level_2_change_approver != '') {
		groupArray.push(svc_gr.cmdb_ci_service.u_level_2_change_approver.toString()); // add level 2 to groupArray for approval
	}
	if(svc_gr.cmdb_ci_service.u_level_3_change_approver != '') {
		groupArray.push(svc_gr.cmdb_ci_service.u_level_3_change_approver.toString()); // add level 3 to groupArray for approval
	}
	
	if(svc_gr.cmdb_ci_service.u_level_4_change_approver != '') {
		groupArray.push(svc_gr.cmdb_ci_service.u_level_4_change_approver.toString()); // add level 4 to groupArray for approval
	}
}
//************************************************************
// region impacted group approval
var region_gr = new GlideRecord('u_m2m_regions_tasks');
region_gr.addQuery('u_task', current.sys_id);
region_gr.query();
while (region_gr.next()){
	if(region_gr.u_company_region.u_regional_approval != '') {
		groupArray.push(region_gr.u_company_region.u_regional_approval.toString()); // add region to groupArray for approval
	}
}
//************************************************************

// set a default approval group in case no groups were found above
if (groupArray.length == 0) {
	groupArray.push(gs.getProperty('atkins.change.approval.group') + '');
}
var arrayUtil = new ArrayUtil();
answer = arrayUtil.unique(groupArray); // remove duplicate groups

 

 

 

I want to be able to add this logic into my UI Action so it queries the change record and checks the following for any new changes on the below tables (where an approver does not exist) and generates the appropriate approval record.

 

Affected CI's: task_ci

Impacted Solutions: task_cmdb_ci_service

Region Impacted: u_m2m_regions_tasks

 

I have got this far with my UI, but it keeps coming back with 'No records found'. I even ran a background script which told me there were 2 services associated with the change. 

 

I understand this is a big ask, but any help would greatly be appreciated.

 

 

// UI Action script for RequestAdditionalApprovers

// Client-side 'onclick' function
function RequestAdditionalApprovers() {
    var answer = confirm('Are you sure you want to request additional approvers?');
    if (answer) {
        // Call the UI Action and skip the 'onclick' function
        gsftSubmit(null, g_form.getFormElement(), 'request.additional.approvers');
    }
}

// Server-side logic included in the same UI action
if (typeof window == 'undefined') {
    // Server-side execution
    requestAdditionalApproversSVR();
}

function requestAdditionalApproversSVR() {
    // Check for new records in 'task_ci,' 'task_cmdb_ci_service,' and 'u_m2m_regions_tasks'
    var taskCiCount = new GlideRecord('task_ci');
    taskCiCount.addQuery('task', current.sys_id);
    var taskCiNewCount = taskCiCount.getRowCount();

    var taskCmdbCiServiceCount = new GlideRecord('task_cmdb_ci_service');
    taskCmdbCiServiceCount.addQuery('task', current.sys_id);
    var taskCmdbCiServiceNewCount = taskCmdbCiServiceCount.getRowCount();

    var regionsTasksCount = new GlideRecord('u_m2m_regions_tasks');
    regionsTasksCount.addQuery('u_task', current.sys_id);
    var regionsTasksNewCount = regionsTasksCount.getRowCount();

    gs.info('RS - taskCiCount: ' + taskCiNewCount);
    gs.info('RS - taskCmdbCiServiceCount: ' + taskCmdbCiServiceNewCount);
    gs.info('RS - regionsTasksCount: ' + regionsTasksNewCount);

    if (taskCiNewCount > 0 || taskCmdbCiServiceNewCount > 0 || regionsTasksNewCount > 0) {
        // New records found, generate approvals and associate them with the change
        gs.addInfoMessage('Requesting additional approvers...');

        // Include your existing logic for generating approvals based on the discovered records
        var svc_gr = new GlideRecord('task_cmdb_ci_service');
        svc_gr.addQuery('task', current.sys_id);
        svc_gr.query();

        while (svc_gr.next()) {
            var groupArray = [];

            if (svc_gr.cmdb_ci_service.u_level_1_change_approver != '') {
                groupArray.push(svc_gr.cmdb_ci_service.u_level_1_change_approver.toString());
            }

            if (svc_gr.cmdb_ci_service.u_level_2_change_approver != '') {
                groupArray.push(svc_gr.cmdb_ci_service.u_level_2_change_approver.toString());
            }

            if (svc_gr.cmdb_ci_service.u_level_3_change_approver != '') {
                groupArray.push(svc_gr.cmdb_ci_service.u_level_3_change_approver.toString());
            }

            if (svc_gr.cmdb_ci_service.u_level_4_change_approver != '') {
                groupArray.push(svc_gr.cmdb_ci_service.u_level_4_change_approver.toString());
            }

            // Add default approval group if no groups were found above
            if (groupArray.length == 0) {
                groupArray.push(gs.getProperty('atkins.change.approval.group') + '');
            }

            var arrayUtil = new ArrayUtil();
            var answer = arrayUtil.unique(groupArray);

            // Now, 'answer' contains the list of groups/approvers for the current 'task_cmdb_ci_service' record

            // Add your logic here to associate these approvals with the change or take any necessary actions
        }
    } else {
        gs.addInfoMessage('No new records found.');
    }
}

 

 

 

1 REPLY 1

Anil Lande
Kilo Patron

Hi @RichardSaunders ,

I did not go into your business logic. Just corrected your script [Glide Record queries]. You were missing query() method for your glide record queries and thats why it was not finding any records.

Please check below script and inline comments:

 

// UI Action script for RequestAdditionalApprovers

// Client-side 'onclick' function
function RequestAdditionalApprovers() {
    var answer = confirm('Are you sure you want to request additional approvers?');
    if (answer) {
        // Call the UI Action and skip the 'onclick' function
        gsftSubmit(null, g_form.getFormElement(), 'request.additional.approvers');
    }
}

// Server-side logic included in the same UI action
if (typeof window == 'undefined') {
    // Server-side execution
    requestAdditionalApproversSVR();
}

function requestAdditionalApproversSVR() {
    // Check for new records in 'task_ci,' 'task_cmdb_ci_service,' and 'u_m2m_regions_tasks'
    var taskCiCount = new GlideRecord('task_ci');
    taskCiCount.addQuery('task', current.sys_id);
	taskCiCount.query();  // this line was missing
    var taskCiNewCount = taskCiCount.getRowCount();

    gs.info('RS - taskCiCount: ' + taskCiNewCount);

    var taskCmdbCiServiceCount = new GlideRecord('task_cmdb_ci_service');
    taskCmdbCiServiceCount.addQuery('task', current.sys_id);
	taskCmdbCiServiceCount.query();  // this line was missing
    var taskCmdbCiServiceNewCount = taskCmdbCiServiceCount.getRowCount();

    gs.info('RS - taskCmdbCiServiceCount: ' + taskCmdbCiServiceNewCount);

    var regionsTasksCount = new GlideRecord('u_m2m_regions_tasks');
    regionsTasksCount.addQuery('u_task', current.sys_id);
	regionsTasksCount.query(); // this line was missing
    var regionsTasksNewCount = regionsTasksCount.getRowCount();

    gs.info('RS - regionsTasksCount: ' + regionsTasksNewCount);

    if (taskCiNewCount > 0 || taskCmdbCiServiceNewCount > 0 || regionsTasksNewCount > 0) {
        // New records found, generate approvals and associate them with the change
        gs.addInfoMessage('Requesting additional approvers...');

        // Include your existing logic for generating approvals based on the discovered records
        var svc_gr = new GlideRecord('task_cmdb_ci_service');
        svc_gr.addQuery('task', current.sys_id);
        svc_gr.query();

        while (svc_gr.next()) {
            var groupArray = [];

            if (svc_gr.cmdb_ci_service.u_level_1_change_approver != '') {
                groupArray.push(svc_gr.cmdb_ci_service.u_level_1_change_approver.toString());
            }

            if (svc_gr.cmdb_ci_service.u_level_2_change_approver != '') {
                groupArray.push(svc_gr.cmdb_ci_service.u_level_2_change_approver.toString());
            }

            if (svc_gr.cmdb_ci_service.u_level_3_change_approver != '') {
                groupArray.push(svc_gr.cmdb_ci_service.u_level_3_change_approver.toString());
            }

            if (svc_gr.cmdb_ci_service.u_level_4_change_approver != '') {
                groupArray.push(svc_gr.cmdb_ci_service.u_level_4_change_approver.toString());
            }

            // Add default approval group if no groups were found above
            if (groupArray.length == 0) {
                groupArray.push(gs.getProperty('atkins.change.approval.group') + '');
            }

            var arrayUtil = new ArrayUtil();
            var answer = arrayUtil.unique(groupArray);

            // Now, 'answer' contains the list of groups/approvers for the current 'task_cmdb_ci_service' record

            // Add your logic here to associate these approvals with the change or take any necessary actions
        }
    } else {
        gs.addInfoMessage('No new records found.');
    }
}
 

 

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande