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

Hi dvp,



Thank you so much for your suggestion.


I added the script to the approval activity but unfortunately, no approvals were generated. I got the below error message.


error.PNG


I can't pin point what the issue may be with the script though 😕



Thank you,


Yeny


I guess the issue is with variable name array in the below statement



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



Can you replace the name with something else and try


Hi dvp,



I replaced the name array with groupApps instead but I got the same error.



I was thinking that the issue would be with the actual list field for IT App owner, and after talking to my leadership, they decided to use a reference field called managed_by instead. I replaced it but still have the same error.



This is what i got from the error log.


error log.PNG



Script:


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;


  //for (var j=0; j < approver_ids.length; j++) {


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


    }


  // }


}



It says that line 1 and 8 are causing the error but I don't see how. 😕 Can you identify something?



Thank you,


Yeny


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