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.

insert approval record

Hafsa1
Mega Sage
if (approvers1.length > 0) {
        var gr1 = new GlideRecord('sysapproval_approver');
        for (var j = 0; j < approvers1.length; j++) {
            gs.info("Approval 1-"+approvers1);
            gr1.initialize();
            gr1.sysapproval = current.sys_id;
            gr1.document_id = current.sys_id;
            gr1.source_table = 'sn_customerservice_north_america_procurement';
            gr1.approver = approvers1[j];
            gr1.state = 'requested';
            gr1.insert();
        }
    }
 
Output:-
getting sysid of users but when approver record is added, it's blank
Approval 1-b8da035adb8aafc4227d18df4b96193d,20a26b08db97a740e4f5138b4b961998
Hafsa1_0-1738238380656.png

 

 
1 ACCEPTED SOLUTION

@Hafsa1 

what is field type for u_category_lead in table sn_customerservice_category_leader?

Seems it's a list field holding multiple sysIds and hence it's not working

If yes then script include function needs change so that it pushes the values correctly

var LeaderApproval = Class.create();
LeaderApproval.prototype = {
    initialize: function() {},

    getCategory: function(category, functions, regionValue) {
        var categoryList = [];
        var categoryValue = '';
        var functionsValue = '';
        var grn = new GlideRecord("sn_customerservice_procurement_category");
        grn.addQuery("sys_id", category);
        grn.addQuery("u_procurement_function", functions);
        grn.query();
        if (grn.next()) {
            categoryValue = grn.u_procurement_category.toString();
            functionsValue = grn.u_procurement_function.toString();
        }

        var arrayUtil = new global.ArrayUtil();
        var gr = new GlideRecord('sn_customerservice_category_leader');
        gr.addQuery('u_category', categoryValue);
        gr.addQuery('u_tower', functionsValue);
        gr.addQuery('u_region', regionValue);
        gr.query();
        while (gr.next()) {
            var arr = gr.u_category_lead.toString().split(',');
            if (arr.length > 0) {
                // if it's array then concat and store in categoryList array
                categoryList = arrayUtil.concat(categoryList, arr);
            } else {
                categoryList.push(gr.u_category_lead.toString());
            }
        }
		categoryList = arrayUtil.unique(categoryList); // get unique
        //return "sys_idIN" + categoryList;
        return categoryList;

    },

    type: 'LeaderApproval'
};

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

View solution in original post

14 REPLIES 14

@Ankur Bawiskar 

this is BR witter, getting approvals sysids using script include(LeaderApproval) and then creating multiple approvals

 

 var cat = current.u_category;
var funct= current.u_function_code;
var region= current.u_region;
var approvers1 = new LeaderApproval().getCategory(cat, funct, region);
 
if (approvers1.length > 0) {
for (var j = 0; j < approvers1.length; j++) {
var gr1 = new GlideRecord('sysapproval_approver');
gs.info("Approval 1-" + approvers1[j].toString());
gr1.initialize();
gr1.sysapproval = current.sys_id;
gr1.document_id = current.sys_id;
gr1.source_table = 'sn_customerservice_north_america_procurement';
gr1.approver = approvers1[j].toString();
gr1.state = 'requested'; gr1.insert();
} }
 

@Hafsa1 

what is field type for u_category_lead in table sn_customerservice_category_leader?

Seems it's a list field holding multiple sysIds and hence it's not working

If yes then script include function needs change so that it pushes the values correctly

var LeaderApproval = Class.create();
LeaderApproval.prototype = {
    initialize: function() {},

    getCategory: function(category, functions, regionValue) {
        var categoryList = [];
        var categoryValue = '';
        var functionsValue = '';
        var grn = new GlideRecord("sn_customerservice_procurement_category");
        grn.addQuery("sys_id", category);
        grn.addQuery("u_procurement_function", functions);
        grn.query();
        if (grn.next()) {
            categoryValue = grn.u_procurement_category.toString();
            functionsValue = grn.u_procurement_function.toString();
        }

        var arrayUtil = new global.ArrayUtil();
        var gr = new GlideRecord('sn_customerservice_category_leader');
        gr.addQuery('u_category', categoryValue);
        gr.addQuery('u_tower', functionsValue);
        gr.addQuery('u_region', regionValue);
        gr.query();
        while (gr.next()) {
            var arr = gr.u_category_lead.toString().split(',');
            if (arr.length > 0) {
                // if it's array then concat and store in categoryList array
                categoryList = arrayUtil.concat(categoryList, arr);
            } else {
                categoryList.push(gr.u_category_lead.toString());
            }
        }
		categoryList = arrayUtil.unique(categoryList); // get unique
        //return "sys_idIN" + categoryList;
        return categoryList;

    },

    type: 'LeaderApproval'
};

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

Hafsa1
Mega Sage

@Ankur Bawiskar 

u_category_lead  is list collector field

@Hafsa1 

solution shared above should work then

My guess was right so please use the script above

 

AnkurBawiskar_0-1738245459123.png

 

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

Hafsa1
Mega Sage

@Ankur Bawiskar 

yes, arrayUtil works