How to create multiple approvals for multiple CIs on a change request?

YenGar
Mega Sage

Hello,

I have a request to create multiple approvals (App Owner and Back up App Owner) for multiple Configuration Items selected in a change request.

The scenario is the following:

A user opens up a change request and selects multiple CIs from a List Collector.

app.PNG

Each CI has two fields in the form, one for App owner and one for Backup App Owner.

app 2.PNG

The workflow (via script) should identify the CIs, query the two fields and create an approval request for each user found for the CIs.

I have attempted the below script but I am not having luck.

var applications = current.u_configuration_items.toString();

var allapplications = applications;

var shortdesc = current.short_description;

var rules = current.u_firewall_rules;

var array = allapplications.split(",");

for (var i=0; i < array.length; i++) {

    var apps = new GlideRecord('cmdb_ci_appl');

    apps.addQuery ('sys_id', array[i]);

    apps.query();

    while(apps.next()){

          //answer.push(apps.u_it_application_owner);

          var newObj = new GlideRecord('sysapproval_approver');

          newObj.initialize();

          //Application Name

          //newObj.cmdb_ci = apps.sys_id;

          //Change Request (Parent) Number

          newObj.parent = current.sys_id;

          newObj.approver = apps.u_it_application_owner;

          //newObj.approver = apps.u_backup_application_owner;

          newObj.state = 'Requested';

          newObj.short_description = shortdesc;

          newObj.insert();  

          //workflow.debug("application Name:" + apps.name);

    }

}

I would like to know if this is even possible to accomplish and how? I have no clue on how to get it done... any help and advice is highly appreciated!

Thank you,

Yeny

1 ACCEPTED SOLUTION

Hi dvp,



I think I got it working now! Thank you so much for your assistance!



I noticed I was missing the answer value on the code. After I added it, it worked! It created all the approval requests



Script:


answer = [];


var applications = current.u_configuration_items.toString();


var allapplications = applications;


var shortdesc = current.short_description;


var groupApps = allapplications.split(",");


for (var i=0; i < groupApps.length; i++) {


    var apps = new GlideRecord('cmdb_ci_appl');


    apps.addQuery('sys_id', groupApps[i]);      


    apps.query();


    while(apps.next()){


    // Use the below for loop if you have multiple owners if not then use answer.push(apps.u_it_application_owner.toString());


  var approver_ids = apps.managed_by;


    answer.push(approver_ids);//[j].toString());


    }


}



Thank you so much for your assistance!


Yeny


View solution in original post

8 REPLIES 8

Chuck Tomasi
Tera Patron

Hi Yeny,



Your approach is off to a good start (assuming u_configuration_items is a list field.) Often times CIs are listed in the related list (Affected CIs) so you may need to get your list from task_ci where task is the current.sys_id.



Approvals are best done in a workflow, not injecting approval records directly in to sysapproval_approver. You can use much of the same script as above to generate an array of sys_ids and store it in the 'answer' variable (see the example in the approval activity script.)



Reference:


Workflow Concepts - ServiceNow Wiki


Creating a Workflow - ServiceNow Wiki


Using Workflow Activities - ServiceNow Wiki


Thank you Chuck! These are very helpful! They helped me get to my resolution!



Thank you,


Yeny


You are very welcome. If I have answered your question, please mark my response as correct so that others with the same question in the future can find it quickly and that it gets removed from the Unanswered list.



If you are viewing this from the community inbox you will not see the correct answer button.   If so, please review How to Mark Answers Correct From Inbox View.



Thank you


dvp
Mega Sage
Mega Sage

Is the above script in Approval activity or Run script?



Try this in Approval activity



var applications = current.u_configuration_items.toString();


var allapplications = applications;


var shortdesc = current.short_description;


var rules = current.u_firewall_rules;


var array = allapplications.split(",");


for (var i=0; i < array.length; i++) {


    var apps = new GlideRecord('cmdb_ci_appl');


    apps.addQuery ('sys_id', array[i]);


    apps.query();


    while(apps.next()){


   
    // Use the below for loop if you have multiple owners if not then use answer.push(apps.u_it_application_owner.toString());
   
  var approver_ids = apps.u_it_application_owner.toString().split(",");
  for (var j=0; j < approver_ids.length; j++) {
    answer.push(approver_ids[j].toString());
    }


    }


}