Approval based on condition in workflow

SHALIKAS
Tera Guru

I have a field called server which is of list collector type referencing to server table. Now if the server selected in catalog item has a change control group value not empty then approval should go to change control group. If server selected has change control group value empty then approval should got to owned by user of the server.

If suppose 2 server are selected and 1 server has change control group value and other server has owned by value then approval should go to both group and user 

How to implement this

6 REPLIES 6

Ankur Bawiskar
Tera Patron
Tera Patron

@SHALIKAS 

so you can use flow variable and handle the logic and get the final list of group or users for approvals

then use scripted approvals logic

check this link

Scripted Approvals in Flow Designer with Flow Variables 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

I have to use workflow

@SHALIKAS 

then logic will be the same, have a workflow run script and push into an array the group and user sysIds

Then use this array and set in workflow scratchpad variable

Then use this scratchpad variable in advanced script in "Approval Activity"

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

I used the below script but it is not creating approval

var server = current.variables.server_name.toString();
gs.log("server name"+server);
var approverGroups = [];
var approverUsers = [];


    var gr = new GlideRecord('cmdb_ci_server');
    gr.addQuery('sys_id','IN',server);
    gr.query();
    while(gr.next()){
        if  (gr.change_control)
        {
            var groupID=gr.change_control.toString();
            if(approverGroups.indexOf(groupID)==-1)
            approverGroups.push(groupID);
        }
          else if (gr.owned_by) {
            gs.log("in else if");
            var userID= gr.owned_by.toString();
            if(approverUsers.indexOf(userID)==-1)
            approverUsers.push(userID);
        }
       
    }

for(var i=0;i<approverGroups.length;i++){
    var approvalGroup =new GlideRecord('sysapproval_approver');
    approvalGroup.initialize();
    approvalGroup.sysapproval=current.sys_id;
    approvalGroup.group=approverGroups[i];
    approvalGroup.state='requested';
    approvalGroup.source_table=current.getTableName();
    approvalGroup.insert();
}
for(var j=0;j<approverUsers.length;j++){

    var approval =new GlideRecord('sysapproval_approver');
    approval.initialize();
    approval.sysapproval=current.sys_id;
    approval.approver=userID;
    approval.state=approverUsers[j];
    approval.source_table=current.getTableName();
    approval.insert();
}