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.

Advanced workflow user approval

rbertram
Kilo Expert

I am trying to create a user approval task using an advanced script and it is generating system errors. The user I am trying to set as the approver is the group manager. The group is set based on a lookup select box variable on a SC requested item form.

This is my script:



var group = new GlideRecord('sys_user_group');
group.addQuery('sys_id',current.variables.workgroup);
group.query();
while (group.next()) {
var groupMgr = group.manager;
gs.log('Group manager sys_id: ' + groupMgr.sys_id.toString());
answer.push(groupMgr.sys_id.toString());
}


I added a log statement to verify that the sys_id is valid on the sys_user table.

This is the warning that is generated in the system log:

org.mozilla.javascript.EcmaError: Cannot convert null to an object.
Caused by error in Script Include: 'WFActivityHandler' at line 110

107:
108: // start with answer as null so we can tell if the script changes it
109: workflow.setVariable("answer", null);
==> 110: var retVal = workflow.eval(script);
111: var answer = workflow.getVariable("answer");
112: if (answer === null)
113: answer = retVal; // legacy support for returning a value from the script

1 ACCEPTED SOLUTION

rbertram
Kilo Expert

It is now working. Thanks for your help. I took it a step further and created a Run Script action to save the group's manger to the workflow scratchpad so I could reference it in an If activity so if the group's manager is the person submitting the request it is automatically approved.

Run Script:



getGroupManager();

function getGroupManager() {
var group = new GlideRecord('sys_user_group');
group.addQuery('sys_id',current.variables.workgroup);
group.query();
while (group.next()) {
var group_manager = group.manager;
workflow.scratchpad.groupMgr = group_manager.sys_id;
}
}


If activity:


answer = ifScript();
function ifScript () {
if (workflow.scratchpad.groupMgr == current.opened_by.sys_id) {
return 'yes';
}
return 'no';
}


Approval - User:


answer = [];
answer.push(workflow.scratchpad.groupMgr.toString());



View solution in original post

11 REPLIES 11

Something I just noticed, remove the 'var' in front of answer = []

crazylogic
Kilo Expert

Thank you very much for your feedback!! I was able to fix my issue. I hope you don't mind me posting my final code in hopes that it may help others.

This does a lookup in a custom table where a ref to user records is stored. I then loop through the records to gather the user sys_ids and then store this in a string instead of an array. I then push the string into the 'answer' variable. I am not 100% certain what fixed this but I suspect your suggestions and avoiding multiple answer.push command was helpful.

 

Thanks again!

var ApproverSYSUSERsysid = '';

var cerApproverMatrixRecords = current.variables.var_cer_5KApprovers;
workflow.info('......cerApproverMatrixRecords = ............' + cerApproverMatrixRecords);

var gr = new GlideRecord('u_cer_approval_matrix');
var qq = gr.addQuery('u_approval_limit','5000'); //Approval Limit = 5000
gr.addQuery('sys_id','IN',cerApproverMatrixRecords);

gr.query();

workflow.info("..........u_cer_approval_matrix query done.......grabbing approvers next but I NEED to count this because I DOUBT I am getting any records here...");

var myCount = 0;
while (gr.next()) { //While the recordset contains records, iterate through them
myCount += 1;
workflow.info('......CERMTRXAPPRV Record Info:.... ' + gr.u_business_unit + ' | ' + gr.u_business_unit_approver.first_name + ' ' + gr.u_business_unit_approver.last_name + ' | ' + gr.u_business_unit_approver + ' \ ' + gr.u_approval_limit + ' --- ' + myCount);

if(myCount <= 1) {
ApproverSYSUSERsysid += gr.u_business_unit_approver;
}
else {
ApproverSYSUSERsysid += ',' + gr.u_business_unit_approver;
}
}
workflow.info('.........while finished...myCount: .. ' + myCount);
workflow.info('.........while finished...ApproverSYSUSERsysid: ..' + ApproverSYSUSERsysid);
answer = [ApproverSYSUSERsysid];