- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-06-2012 01:44 PM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-07-2012 10:46 AM
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());
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-08-2012 11:04 AM
You do not really need to use the scratchpad for that purpose as you can reference the manager in the If activity anyways using
current.variables.workgroup.manager.toString();
at any time.
Plus you can actually set yourself up for some problems later on. The manager for the group could actually change between the time the script runs and your approval step so you would be setting the approval to the previous manager. I might be picky here, but it could happen. This particular case may not be a good example, but just picture if you had a couple other activities between the two that took some time to complete. I just do not want you to get used to a particular way of doing things if it was not the proper way. Only use the scratchpad for things that you do not have access to, like results from previous activities.
And for your approval script, in this particular case, you do not need to create an array as there will always only be one approver. I would just use:
answer = current.variables.workgroup.manager.toString();
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-10-2012 01:15 PM
That would work if the variable was a reference type, but my variable is a lookup selectbox which is not dot-walkable.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-10-2012 07:41 PM
Gotcha.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-26-2018 06:11 AM
Did anyone ever find a solution to this? I am finding the same behavior in my instance. The script is SUPER simple but it appears that array.push is not operational... or something...
I've contacted support but they proposed 2 solutions which were nonsense and then suggested I ping the community.
Thanks in advance!
var answer = [];
var gr = new GlideRecord('sys_user');
gr.addQuery('last_name','CONTAINS','smith');
gr.query();
while(gr.next()){
var sys_id_user = gr.sys_id;
workflow.info(sys_id_user);
answer.push(sys_id_user.toString() + "");
}
====== On another note: Proven this version will function.. but this is not helpful for a dynamic approval step
var answer = [];
answer.push('ac2b84214fb38a000a54f7e18110c7a3');
answer.push('7d6353104fb24b40845ae57d0210c7ea');
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-26-2018 06:48 AM
My issue was resolved a long time ago and was just because I forgot to declare answer as an array.
I don't see anything technically wrong with your script. Have you verified the query is actually finding user records? Additionally I would add a gr.addActiveQuery() to ensure non-active accounts are being added as an approver.
I do not know if your actual query based on a person's last name or this was just an example, but if it is, another approach is to create a group and just assign the approval to the group members. This would eliminate need to modify the workflow in the future if the approver criteria changes and you would only need to modify the group membership.
