Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Creation of approval records using script

Hafsa1
Mega Sage

I have created an script include to get approver sys_id and I'm getting too.

below is the BR

var approvers = new csmCategoryApproval().getCategory(current.variables.category_cost);
gs.info("approvers);
if (approvers.length > 0) {
        var gr = new GlideRecord('sysapproval_approver');
        for (var i = 0; i < approvers.length; i++) {
            gr.initialize();
            gr.sysapproval = current.sys_id;
            gr.document_id = current.sys_id;
            gr.source_table = 'sn_customerservice_north_america_procurement';
            gr.approver = approvers.toString()[i];
            gr.state = 'requested';
            gr.insert();
        }
    }
 
Output:
gs.info("approvers); is giving sys_id of users as below
ddfce96a47465e1060c8dbab416d43d0,886fcf1b87cd6910deb275d8dabb35aa,e99c513bdbfe270063c1c7ec0b96195f,07caf55f47da5690fbbf4d89116d4352,078e26914f68b60855699efd0210c7c9,051ba91f47565690fbbf4d89116d43db,17c0030013875b8436db33228144b025,73952025db81e34063c1c7ec0b961913
 
 
But when I'm check the case there are multiple approvals are getting added with blank approver
1 ACCEPTED SOLUTION

Shruti
Mega Sage
Mega Sage
var approvers = new csmCategoryApproval().getCategory(current.variables.category_cost);
gs.info("approvers);
if (approvers.length > 0) {
        var gr = new GlideRecord('sysapproval_approver');
        for (var i = 0; i < approvers.length; i++) {
            gr.initialize();
            gr.sysapproval = current.sys_id;
            gr.document_id = current.sys_id;
            gr.source_table = 'sn_customerservice_north_america_procurement';
            gr.approver = approvers[i];
            gr.state = 'requested';
            gr.insert();
        }
    }

View solution in original post

1 REPLY 1

Shruti
Mega Sage
Mega Sage
var approvers = new csmCategoryApproval().getCategory(current.variables.category_cost);
gs.info("approvers);
if (approvers.length > 0) {
        var gr = new GlideRecord('sysapproval_approver');
        for (var i = 0; i < approvers.length; i++) {
            gr.initialize();
            gr.sysapproval = current.sys_id;
            gr.document_id = current.sys_id;
            gr.source_table = 'sn_customerservice_north_america_procurement';
            gr.approver = approvers[i];
            gr.state = 'requested';
            gr.insert();
        }
    }