logo

NJP

Trigger approvals based on selection and create tasks based only for approved

Import · May 11, 2022 · article

This article is to trigger approvals for selected CIs from variable and create tasks only for the approved CIs

Step 1:

Workflow activity: Approval User, in case of group use Approval group and set the getValue appropriately

answer = myScript();

function myScript() {
    var apps = current.variables.variable_name.toString();
    var list = apps.split(',');
    var appList = [];
    for (var i = 0; i < list.length; i++) {
        var ci = new GlideRecord('cmdb_ci');
        if (ci.get(list[i])) {
            appList.push(ci.getValue('approver_field'));
            }
        }
        return appList;
    }

Step 2:

Create tasks only to the approved CIs

Workflow activity: Run script

var options = current.variables.variable_name.toString().split(',');
for (i=0; i < options.length; i++) {
    var ci = new GlideRecord('cmdb_ci');
    ci.get(options[i]);

    var gr = new GlideRecord("sysapproval_approver");
    gr.addQuery("sysapproval", current.sys_id);
    gr.addQuery("approver", ci.field_name_of_approving_user);
    gr.addQuery("state", "approved");
    gr.query();
    if (gr.next()) {
        var sct = new GlideRecord('sc_task');
        sct.initialize();
        sct.short_description = 'This is the short description  ' + ci.name;
        sct.assignment_group = ci.support_group;
        sct.request_item = current.sys_id;
        sct.insert();
    }
}

View original source

https://www.servicenow.com/community/itsm-articles/trigger-approvals-based-on-selection-and-create-tasks-based-only/ta-p/2306234