How to define dynamic approval creation using approval activity in workflow.

Koyel Guha
Tera Contributor

Hi ,

I am facing issue to define dynamic approval in the approval activity. Please find the code.

workflow.scratchpad.approver is storing username and is defined in the runscript of the wf. If workflow.scratchpad.approver is a member of the below group, then it would go to 2 approvers. else the approval will go the variable as mentioned as primary owner.

For the below, the approval is getting skipped.

//--------------- For Item XXXXXXX ------------------
else if (item == "XXXXXXX") {
if(workflow.scratchpad.approver.isMemberOf('Group'))
{
answer = ['4f7a90c9dbb5a700ce59a155059619c6', '8df91445dbb5a700ce59a15505961922'];
}
else {
if (workflow.scratchpad.request_type == "create" || workflow.scratchpad.request_type == "delete") {
answer = [current.variables.primary_owner];
} else {
var answer = [];
answer.push(current.variables.primary_owner);
answer.push(current.variables.new_primary_owner);
}
}
}

 

Could you please help me with the above code so that the approval goes to the mentioned sysids else primary owner. 

 

KoyelGuha_0-1687434737646.png

 

8 REPLIES 8

Ankur Bawiskar
Tera Patron
Tera Patron

@Koyel Guha 

this will work; use this line

if(gs.getUser().getUserByID(workflow.scratchpad.approver).isMemberOf('Group'))
{
answer = ['4f7a90c9dbb5a700ce59a155059619c6', '8df91445dbb5a700ce59a15505961922'];
}
else {
if (workflow.scratchpad.request_type == "create" || workflow.scratchpad.request_type == "delete") {
answer = [current.variables.primary_owner];
} else {
var answer = [];
answer.push(current.variables.primary_owner);
answer.push(current.variables.new_primary_owner);
}
}

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

Hi @Ankur Bawiskar ,

The approvals are going to the mentioned sysids .However, it should proceed if one of the approver approves the request.  But it is asking for both the approvals then processing the next step . 

 

I have 2 approvers from different groups. Can we do something so that it would work dynamically  and send the approvals to both instead of putting sysids to avoid the manual approach. so that in future if the approver names get changed , there is no need to put sysid. 

 

is it possible ? 

@Koyel Guha 

So the script I shared is working fine.

Please mark my response as correct as my approach worked for you.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

@Koyel Guha 

it depends on what have you configured in the approval activity i.e. anyone approves or all approves etc

to make it dynamic you can store those sysIds in system property and then use that in your workflow script

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