Workflow Approval User Via Script

stevejarman
Giga Guru

I'm attempting to set up an approval using the "Advanced Additional Approvers Script". The script I'm using is shown below. u_capacity_managers is a User reference, and I can confirm that answer ends up being set to the sys_id of the correct user (I've printed it into a field to check via current.description = answer), BUT, the approval step just skips through as though no one is being set as the approver. Can anyone identify any problems with what I'm doing?

// Set the variable 'answer' to a comma-separated list of user ids and/or group ids or an array of user/group ids to add as approvers.

//

// For example:

//             answer = [];

//             answer.push('id1');

//             answer.push('id2');

var rs = new GlideRecord("u_provisioning_approvers");

rs.addQuery("u_company", current.opened_by.company);

rs.query();

while (rs.next())

{

      answer = String(rs.u_capacity_managers);

}

16 REPLIES 16

Kalaiarasan Pus
Giga Sage

var answer = [];


var individualApprovers = [];


var arrayLength = '';


var rs = new GlideRecord("u_provisioning_approvers");


rs.addQuery("u_company", current.opened_by.company);


rs.query();



while (rs.next()) {


individualApprovers = (rs.getValue('u_capacity_managers')+',').split(',');


for(arrayLength=0;arrayLength<individualApprovers.length;arrayLength++)


{


if(JSUtil.notNil(individualApprovers[arrayLength]))


{


      answer.push(individualApprovers[arrayLength].toString());


}


}


individualApprovers = [];


}


stevejarman
Giga Guru

Bumping this as it's still causing me problems.



Going back to my original script:



var rs = new GlideRecord("u_flexiserver_provisioning_approvers");


rs.addQuery("u_company", current.opened_by.company.toString());


rs.queryNoDomain();



if (rs.next())


      answer = rs.u_capacity_managers.toString();



I have confirmed 100% that answer ends up containing either a single sys_id, or a comma separated list of sys_ids.



Can anyone tell me why this does not work for the approval. It still gets a "Skipped" status.



Has anyone ever successfully got the "Additional Approvers Script" option to work on the Approval - User activity? If so, can you share your code that actually works? Note - working in Helsinki.



Also, I've tried pushing to an answer array - same thing. Doesn't work.


Ended up working out the issue on this. We are running a domain separated instance and hadn't taken into account that the users being added as approvers aren't in the same domain that the workflow context is running in. So it was receiving the sys_ids for the users, but when trying to look them up it would not see them. At that point it would just move on to the next step of the workflow. In production this won't be an issue. It just happened because we're testing something with ourselves as the approvers, and the customer as the requestor.



FYI - the very first script that I posted works fine, as long as this domain issue is taken into account in our case.


Daniel Oderbolz
Kilo Sage

I think the problem is that the script abilities of the approvers activity are limited.

Here is what I do:

answer = getApprovals();

function getApprovals(){

// Your code here

}

I think it has to do with this rather brittle approach to use a gloal variable.


If this answer was helpful, I would appreciate if you marked it as such - thanks!

Best
Daniel

Daniel Oderbolz
Kilo Sage

In my opinion, this is a bug in the SNOW Implementation of the Approval Activity.
BTW: the bug seems to be still there in Kingston
(I have an open HI Incident on this)

Errors are not properly handled (note how an error leads to an acceptance of the approval!), also the error message is misleading.

The only way I have seen that works is this:

* Create a "Run Script" activity before the Approval where you set a variable in the scratchpad:

workflow.scratchpad.util = new My_Script_Include_Util();

* Then in the approval activity do this (The function My_Script_Include_Util.getApprovers() returns an array of sys_ids of approvers)

answer = workflow.scratchpad.util.getApprovers(current);

This seems a little esoteric to me.

Best
Daniel

 


If this answer was helpful, I would appreciate if you marked it as such - thanks!

Best
Daniel