Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

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

Jim Coyne
Kilo Patron

The


var groupMgr = group.manager
bit is assigning the sys_id of the manager to your variable, which you do not really need.

Just use


answer.push(group.manager.toString());

instead as group.manager will be the sys_id.


rbertram
Kilo Expert

Removing the sys_id from the push statement did not work and still is generating the same "cannot convert null to an object" error. I had originally tried it that way, but added the sys_id to the statement based on the example on the wiki (https://wiki.servicenow.com/index.php?title=Case_Study_-_Advanced_Approval_Workflow_(Part_2)).


Jim Coyne
Kilo Patron

I think the problem is you are not setting "answer" as an array at the beginning (first line):



answer = [];
var group = new GlideRecord('sys_user_group');
group.addQuery('sys_id',current.variables.workgroup);
group.query();
while (group.next()) {
answer.push(group.manager.toString());
}


But, there is actually a simpler way of doing it instead of querying the group table. 1 line instead of 7:


answer = current.variables.workgroup.manager.toString();


Because this is running on the server, you can dot-walk any catalog reference variable (not JavaScript variables).


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());