We've updated the ServiceNow Community Code of Conduct, adding guidelines around AI usage, professionalism, and content violations. Read more

Automated Follow On Task assignment in Scripted audits

Daniel Borkowi1
Mega Sage

Hi community,

 

I tried to implement some scripted CMDB Compliance audits. It works fine to great Follow On Task when a test failed. 

What's not working is the assignment of the Follow-On task with the settings made in the audit record:

find_real_file.png

All tasks are unassigned. How can I improve my code below, that this assignment will be done automatically by the certification processor with the field values defined in the audit record? Any ideas?

var certProcessor;
if (gs.getCurrentScopeName().includes("global"))
    certProcessor = new SNC.CertificationProcessing();
else {
    certProcessor = new CertificationProcessing();
}
certProcessor.runAudit(current.sys_id, current.template.filter.sys_id, current.template.sys_id);
var taskMsg = 'See the audit results below for the discrepancies that must be addressed';


var grWindowsServer = new SNC.CertificationProcessing().getFilterRecords(current.filter);

// Loop over all records defined by the filter
while (grWindowsServer.next()) {
    var win_sys_id = grWindowsServer.getUniqueValue();
    var expectedEnvironment = grWindowsServer.getValue('environment');
    var grIpAdresses = new GlideRecord('cmdb_ci_ip_address');
    grIpAdresses.addQuery('nic.cmdb_ci', win_sys_id);
    grIpAdresses.query();
    var pass = true;
    while (grIpAdresses.next()) {
        var networkEnvironment = grIpAdresses.getValue('environment');
        if (expectedEnvironment != networkEnvironment) {
            pass = false;
            var followOnTask = new SNC.CertificationProcessing().createFollowOnTask(current.sys_id, win_sys_id, '', '', taskMsg);
            new SNC.CertificationProcessing().logAuditResultFail(current.sys_id, win_sys_id, followOnTask, 'environment', '', expectedEnvironment, networkEnvironment, true);
        }
    }
    if (pass) {
        new SNC.CertificationProcessing().logAuditResultPass(current.sys_id, win_sys_id, true);
    }


}

 

Thanks in advance.

Greets Daniel

1 ACCEPTED SOLUTION

Daniel Borkowi1
Mega Sage

Found a solution with manual script:

var certProcessor;
if (gs.getCurrentScopeName().includes("global"))
    certProcessor = new SNC.CertificationProcessing();
else {
    certProcessor = new CertificationProcessing();
}
certProcessor.runAudit(current.sys_id, current.template.filter.sys_id, current.template.sys_id);
var taskMsg = 'See the audit results below for the discrepancies that must be addressed';


var grWindowsServer = new SNC.CertificationProcessing().getFilterRecords(current.filter);

// Loop over all records defined by the filter
while (grWindowsServer.next()) {
    var assign_user = "";
    var assign_group = "";
	//TODO assign to empty == Don't create task berücksichtigen
    if (current.assignment_type == 'User Field') {
        assign_user = grWindowsServer.getValue(current.getValue('assign_to'));
    }
    if (JSUtil.nil(assign_user) || current.assignment_type == 'Specific User') {
        assign_user = current.getValue('user');
    }
    if (current.assignment_type == 'Group Field') {
        assign_group = grWindowsServer.getValue(current.getValue('assign_to_group'));
    }
    if (JSUtil.nil(assign_group) || current.assignment_type == 'Specific Group') {
        assign_group = current.getValue('group');
    }
    var win_sys_id = grWindowsServer.getUniqueValue();
    var expectedEnvironment = grWindowsServer.getValue('environment');
    var grIpAdresses = new GlideRecord('cmdb_ci_ip_address');
    grIpAdresses.addQuery('nic.cmdb_ci', win_sys_id);
    grIpAdresses.query();
    var pass = true;
    while (grIpAdresses.next()) {
        var networkEnvironment = grIpAdresses.getValue('environment');
        if (expectedEnvironment != networkEnvironment) {
            pass = false;
			gs.log(assign_user + " " + assign_group, "");
            var followOnTask = new SNC.CertificationProcessing().createFollowOnTask(current.sys_id, win_sys_id, assign_user, assign_group, taskMsg);
            new SNC.CertificationProcessing().logAuditResultFail(current.sys_id, win_sys_id, followOnTask, 'environment', '', expectedEnvironment, networkEnvironment, true);
        }
    }
    if (pass) {
        new SNC.CertificationProcessing().logAuditResultPass(current.sys_id, win_sys_id, true);
    }


}
/*
/////////////////////////////////////////////////////
/// This script works with Data Center Zones filter //
/////////////////////////////////////////////////////

var desiredFloorSpaceUsage = 30;		// Value to audit against
var assignToUser = '46d44a23a9fe19810012d100cca80666';	 // Beth Anglin
var assignToGroup = '8a5055c9c61122780043563ef53438e3';	// Hardware group
var taskMsg = 'See the audit results below for the discrepancies that must be addressed';

// API call to retrieve records based on the filte
var gr = new SNC.CertificationProcessing().getFilterRecords(current.filter);

// Loop over all records defined by the filter
while(gr.next()) {
	var sysId = gr.getValue('sys_id');	// Sys ID of audited record
	var floorSpaceInUse = gr.getValue('floor_space_in_use');	// Value to audit

	// Determine if certification condition passes or fails
	if (floorSpaceInUse < desiredFloorSpaceUsage) {
		var columnNameSpace = gr.floor_space_in_use.getLabel();	// String value of column audited against

		// Call create Follow on Task API and save the returned sys_id for use in logging audit result fail
		// Params: 
		// auditId - Sys id of the audit record executed
		// ciId Sys - id of the configuration item.  Empty string if not a cmdb ci
		// assignedTo - Sys id of user to assign task to.  Can be empty
		// assignmentGroup - Sys id of group to assign task to.  Can be empty
		// shortDescr - Short description for the Follow On Task.  Can be empty
		// Return value: Sys id of the created follow on task
		var followOnTask = new SNC.CertificationProcessing().createFollowOnTask(current.sys_id, sysId, assignToUser, '', taskMsg);

		// Call log failed result API
		// Params:
		// auditId - Sys id of audit record executed
		// auditedRecordId - Sys id of the record audited
		// followOnTask - Sys id of the follow on task associated with the audited record(@see auditedRecordId). Can be empty
		// columnDisplayName - Label of the column audited(ex. Disk space (GB)).  Can be empty
		// operatorLabel - Label of the operator used to audit the column(ex. is not empty, greater than). Can be empty
		// desiredValue - Desired value of the column.  Can be empty
		// discrepancyValue - Discrepancy value.  Can be empty
		// isCI - True, if audited record is a CI. False, otherwise.
		// domainToUse - Sys domain of the "cert_audit" record.  Can be empty
		new SNC.CertificationProcessing().logAuditResultFail(current.sys_id, sysId, followOnTask, columnNameSpace, 'greater than', desiredFloorSpaceUsage, floorSpaceInUse, true);
	} else { // If certification condition pass, write a Audit Result Pass via API
		// Params:
		// auditId - Sys id of audit record executed
		// auditedRecordId - Sys id of the record audited
		// isCI - True, if audited record is a CI. False, otherwise.  Can be empty.
		// domainToUse - Sys domain of the "cert_audit" record.  Can be empty.
		new SNC.CertificationProcessing().logAuditResultPass(current.sys_id, sysId, true);
	}
}
*/

View solution in original post

3 REPLIES 3

Sandeep90
ServiceNow Employee

I am not completely sure but you can try passing in the assignedTo, assignmentGroup to the createFollowOnTask as 3rd and 4th paramter.

Create a follow on task
Params:
auditId – Sys id of the audit record executed
ciId – Sys id of the configuration item. Empty string if not a cmdb ci
assignedTo – Sys id of user to assign task to. Can be empty
assignmentGroup – Sys id of group to assign task to. Can be empty
shortDescr – Short description for the Follow On Task

This is what I did, but doesn't work.

Daniel Borkowi1
Mega Sage

Found a solution with manual script:

var certProcessor;
if (gs.getCurrentScopeName().includes("global"))
    certProcessor = new SNC.CertificationProcessing();
else {
    certProcessor = new CertificationProcessing();
}
certProcessor.runAudit(current.sys_id, current.template.filter.sys_id, current.template.sys_id);
var taskMsg = 'See the audit results below for the discrepancies that must be addressed';


var grWindowsServer = new SNC.CertificationProcessing().getFilterRecords(current.filter);

// Loop over all records defined by the filter
while (grWindowsServer.next()) {
    var assign_user = "";
    var assign_group = "";
	//TODO assign to empty == Don't create task berücksichtigen
    if (current.assignment_type == 'User Field') {
        assign_user = grWindowsServer.getValue(current.getValue('assign_to'));
    }
    if (JSUtil.nil(assign_user) || current.assignment_type == 'Specific User') {
        assign_user = current.getValue('user');
    }
    if (current.assignment_type == 'Group Field') {
        assign_group = grWindowsServer.getValue(current.getValue('assign_to_group'));
    }
    if (JSUtil.nil(assign_group) || current.assignment_type == 'Specific Group') {
        assign_group = current.getValue('group');
    }
    var win_sys_id = grWindowsServer.getUniqueValue();
    var expectedEnvironment = grWindowsServer.getValue('environment');
    var grIpAdresses = new GlideRecord('cmdb_ci_ip_address');
    grIpAdresses.addQuery('nic.cmdb_ci', win_sys_id);
    grIpAdresses.query();
    var pass = true;
    while (grIpAdresses.next()) {
        var networkEnvironment = grIpAdresses.getValue('environment');
        if (expectedEnvironment != networkEnvironment) {
            pass = false;
			gs.log(assign_user + " " + assign_group, "");
            var followOnTask = new SNC.CertificationProcessing().createFollowOnTask(current.sys_id, win_sys_id, assign_user, assign_group, taskMsg);
            new SNC.CertificationProcessing().logAuditResultFail(current.sys_id, win_sys_id, followOnTask, 'environment', '', expectedEnvironment, networkEnvironment, true);
        }
    }
    if (pass) {
        new SNC.CertificationProcessing().logAuditResultPass(current.sys_id, win_sys_id, true);
    }


}
/*
/////////////////////////////////////////////////////
/// This script works with Data Center Zones filter //
/////////////////////////////////////////////////////

var desiredFloorSpaceUsage = 30;		// Value to audit against
var assignToUser = '46d44a23a9fe19810012d100cca80666';	 // Beth Anglin
var assignToGroup = '8a5055c9c61122780043563ef53438e3';	// Hardware group
var taskMsg = 'See the audit results below for the discrepancies that must be addressed';

// API call to retrieve records based on the filte
var gr = new SNC.CertificationProcessing().getFilterRecords(current.filter);

// Loop over all records defined by the filter
while(gr.next()) {
	var sysId = gr.getValue('sys_id');	// Sys ID of audited record
	var floorSpaceInUse = gr.getValue('floor_space_in_use');	// Value to audit

	// Determine if certification condition passes or fails
	if (floorSpaceInUse < desiredFloorSpaceUsage) {
		var columnNameSpace = gr.floor_space_in_use.getLabel();	// String value of column audited against

		// Call create Follow on Task API and save the returned sys_id for use in logging audit result fail
		// Params: 
		// auditId - Sys id of the audit record executed
		// ciId Sys - id of the configuration item.  Empty string if not a cmdb ci
		// assignedTo - Sys id of user to assign task to.  Can be empty
		// assignmentGroup - Sys id of group to assign task to.  Can be empty
		// shortDescr - Short description for the Follow On Task.  Can be empty
		// Return value: Sys id of the created follow on task
		var followOnTask = new SNC.CertificationProcessing().createFollowOnTask(current.sys_id, sysId, assignToUser, '', taskMsg);

		// Call log failed result API
		// Params:
		// auditId - Sys id of audit record executed
		// auditedRecordId - Sys id of the record audited
		// followOnTask - Sys id of the follow on task associated with the audited record(@see auditedRecordId). Can be empty
		// columnDisplayName - Label of the column audited(ex. Disk space (GB)).  Can be empty
		// operatorLabel - Label of the operator used to audit the column(ex. is not empty, greater than). Can be empty
		// desiredValue - Desired value of the column.  Can be empty
		// discrepancyValue - Discrepancy value.  Can be empty
		// isCI - True, if audited record is a CI. False, otherwise.
		// domainToUse - Sys domain of the "cert_audit" record.  Can be empty
		new SNC.CertificationProcessing().logAuditResultFail(current.sys_id, sysId, followOnTask, columnNameSpace, 'greater than', desiredFloorSpaceUsage, floorSpaceInUse, true);
	} else { // If certification condition pass, write a Audit Result Pass via API
		// Params:
		// auditId - Sys id of audit record executed
		// auditedRecordId - Sys id of the record audited
		// isCI - True, if audited record is a CI. False, otherwise.  Can be empty.
		// domainToUse - Sys domain of the "cert_audit" record.  Can be empty.
		new SNC.CertificationProcessing().logAuditResultPass(current.sys_id, sysId, true);
	}
}
*/