Create Workflow Approvals for a Catalog item

Joshuu
Kilo Sage

Hi All,

 

I have a catalog item called "XXX Application Access". And we have a variable on the form of type "List collector" where users will be able to select one/more applications from cmdb_ci application table.

 

Once the request is submitted, the approval should be triggered from the service offering table.

 

So, for every CI we have a relationship in the CI relationships table. And here Parent is the CI/Application and the Child is mapped to Service Offerings table. Please find the below screen shot for reference.

 

priyarao_0-1672723935886.png

 

priyarao_1-1672724052123.png

 

So, based on this mapping we need to trigger the approval to the Approval Group from the Offerings table for the selected applications on the catalog form.

 

For example, a user has selected 6 applications in the catalog form.

 

4 application are having same approval group and the remaining 2 are having some other approval group in the service Offering table.

 

In this case, we should trigger only 2 approvals in the request for all 6 applications. 

 

one approval is for the first 4 applications since it is having the same group in the service offering table.

and the second approval is for the remaining 2 applications.

 

And it should complete all the approvals before creating catalog tasks.

 

Kindly please help me with this flow.

 

Appreciate your help.

 

Thanks & Regards,

Priya.

 

 

2 ACCEPTED SOLUTIONS

@Joshuu Please try below code. It should work.

 

var answer = [];

var applications = (current.variables.application_access.toString()).split(",");

for (var i = 0; i < applications.length; i++) {
    var cirel = new GlideRecord('cmdb_rel_ci');
    cirel.addQuery('parent', applications[i]);
    cirel.addQuery('type=5a17d06787f551100c80ec683cbb355e');
    cirel.addQuery("child.sys_class_name=service_offering");
    cirel.query();
   
    while (cirel.next()) {
        if(answer.indexOf(cirel.child.change_control.toString()) < 0){
        answer.push(cirel.child.change_control.toString());
        }
    }
}
 
Please mark as correct answer if this solves your issue.
Please mark the answer as correct or helpful based on impact
ServiceNow Community Rising Star, Class of 2023

View solution in original post

@Joshuu Remove "Catalog task" activity and add "Run script" activity and add below updated code to "Run script" activity.

 

var groups = [];
var appObject = {};
var appIdObject = {};

var applications = (current.variables.application_access.toString()).split(",");
 

for (var i = 0; i < applications.length; i++) {
var cirel = new GlideRecord('cmdb_rel_ci');
cirel.addQuery('parent', applications[i]);
cirel.addQuery('type=5a17d06787f551100c80ec683cbb355e');
cirel.addQuery("child.sys_class_name=service_offering");
cirel.query();
 

while (cirel.next()) {
    if (groups.indexOf(cirel.child.u_access_group.toString()) < 0) {
        groups.push(cirel.child.u_access_group.toString());
    }
 

    if(appObject[cirel.child.u_access_group.toString()]){
appObject[cirel.child.u_access_group.toString()].push(cirel.parent.getDisplayValue());
    }else{
        appObject[cirel.child.u_access_group.toString()] = [];
        appObject[cirel.child.u_access_group.toString()].push(cirel.parent.getDisplayValue());
    }

    if(appIdObject[cirel.child.u_access_group.toString()]){
appIdObject[cirel.child.u_access_group.toString()].push(cirel.parent.toString());
    }else{
        appIdObject[cirel.child.u_access_group.toString()] = [];
        appIdObject[cirel.child.u_access_group.toString()].push(cirel.parent.toString());
    }
}
}

for(var j=0; j<groups.length; j++){
var scTask = new GlideRecord("sc_task");
scTask.initialize();
scTask.request = current.request.toString();
scTask.request_item = current.sys_id.toString();
scTask.assignment_group = groups[j];
scTask.description = (appObject[groups[j]].toString()).replaceAll(",","\n");;
scTask.short_description = "SAAF access";
var taskId = scTask.insert();

var affectedApps = appIdObject[groups[j]];

for(var k=0; k<affectedApps.length; k++){
    var affectedCI = new GlideRecord("task_ci");
    affectedCI.initialize();
    affectedCI.task = taskId;
    affectedCI.ci_item = affectedApps[k];
    affectedCI.insert();
}
}
 
This will add the each application on new line in description
And add applications to Affected CIs related list as well
 
Please mark as correct answer if this solves your issue.
Please mark the answer as correct or helpful based on impact
ServiceNow Community Rising Star, Class of 2023

View solution in original post

34 REPLIES 34

Joshuu
Kilo Sage

Hi @jaheerhattiwale ,

 

Starting a new Reply.

 

Yes, We should create multiple catalog tasks based on number of applications selected on the catalog form.

 

Just like approvals, we need to do grouping here too. If we have the same access group in offering table for 2 3 applications, one task should be enough. If we have different access groups in Offering table for the selected applications then we need to trigger individual catalog tasks for each of the applications.

 

Could you please assist.

 

Thanks,

Priya.

Joshuu
Kilo Sage

Hi @jaheerhattiwale ,

 

Is it possible to add applications line by line in the description?

As of now the output is :

priyarao_0-1672847908204.png

 

Also, can we add these applications under Affected CIs tab at the bottom of catalog tasks.

 

priyarao_1-1672847962259.png

 

Thanks & Regards,

Priya.

@Joshuu Remove "Catalog task" activity and add "Run script" activity and add below updated code to "Run script" activity.

 

var groups = [];
var appObject = {};
var appIdObject = {};

var applications = (current.variables.application_access.toString()).split(",");
 

for (var i = 0; i < applications.length; i++) {
var cirel = new GlideRecord('cmdb_rel_ci');
cirel.addQuery('parent', applications[i]);
cirel.addQuery('type=5a17d06787f551100c80ec683cbb355e');
cirel.addQuery("child.sys_class_name=service_offering");
cirel.query();
 

while (cirel.next()) {
    if (groups.indexOf(cirel.child.u_access_group.toString()) < 0) {
        groups.push(cirel.child.u_access_group.toString());
    }
 

    if(appObject[cirel.child.u_access_group.toString()]){
appObject[cirel.child.u_access_group.toString()].push(cirel.parent.getDisplayValue());
    }else{
        appObject[cirel.child.u_access_group.toString()] = [];
        appObject[cirel.child.u_access_group.toString()].push(cirel.parent.getDisplayValue());
    }

    if(appIdObject[cirel.child.u_access_group.toString()]){
appIdObject[cirel.child.u_access_group.toString()].push(cirel.parent.toString());
    }else{
        appIdObject[cirel.child.u_access_group.toString()] = [];
        appIdObject[cirel.child.u_access_group.toString()].push(cirel.parent.toString());
    }
}
}

for(var j=0; j<groups.length; j++){
var scTask = new GlideRecord("sc_task");
scTask.initialize();
scTask.request = current.request.toString();
scTask.request_item = current.sys_id.toString();
scTask.assignment_group = groups[j];
scTask.description = (appObject[groups[j]].toString()).replaceAll(",","\n");;
scTask.short_description = "SAAF access";
var taskId = scTask.insert();

var affectedApps = appIdObject[groups[j]];

for(var k=0; k<affectedApps.length; k++){
    var affectedCI = new GlideRecord("task_ci");
    affectedCI.initialize();
    affectedCI.task = taskId;
    affectedCI.ci_item = affectedApps[k];
    affectedCI.insert();
}
}
 
This will add the each application on new line in description
And add applications to Affected CIs related list as well
 
Please mark as correct answer if this solves your issue.
Please mark the answer as correct or helpful based on impact
ServiceNow Community Rising Star, Class of 2023

Hi @jaheerhattiwale ,

 

I have removed the catalog task and added run script. 

I can see only one catalog task is created for all the applications selected with empty assignment group. Please find the below screen shot. Could you please check. And all 3 applications were added in the Affected CIs tab.

 

priyarao_0-1672902196475.png

Thanks,

Priya.

@Joshuu For all selected applications the access group might be same.

and assignment group should not be empty, so please check by adding logs to the code and check if how many groups added to the array and how many time the for loop is working.

Please check by adding logs.

Please mark the answer as correct or helpful based on impact
ServiceNow Community Rising Star, Class of 2023