Check if User Already Approved in Workflow

gregrice
Tera Expert

In my approval workflow I need to check to see if the approval has already approved (they may have been a part of a group that has approved in an earlier step). Seems like this would be pretty straight forward but I can't get it to work. 

Created an if condition in my workflow and was trying to lookup the sysapproval record to see if said user has already approved. (Not sure how to find the user so for now I hard coded it). Any suggestions. Below is what I tried (I'm guessing I don't have the sys_id). This is on a workflow that is on insert of my record.

function ifScript() {
   var gr = new GlideRecord('sysapproval_approval');
   gr.addQuery('sysapproval',current.sys_id);
   gr.addQuery('approver','20d5bfa64fdf46007e2f0ad14210c7ed');
   gr.addQuery('state','approved');
   gr.query();
   if(gr.next()) {
     return 'yes';
    } else {return 'no';}
}

7 REPLIES 7

Mark Roethof
Tera Patron
Tera Patron

Hi there,

The script basically looks fine. Though 2 remakers:

- The table is incorrect, a spelling error. Should be: sysapproval_approver
- You are using a hardcoded sys_id for the approver. Maybe for your use case you only have one specific approver. Though can you make this dynamically? For example, if it might be a manager of a certain group? Then query for that, instead of hardcoded sys_id.

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark
2020 ServiceNow Community MVP
2020 ServiceNow Developer MVP

---

LinkedIn
Community article list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

DirkRedeker
Mega Sage

Hi

So, what in fact is your question?

Do you want to know, how to get the sysid of any given user?

Because there is just the way to "hard code" the user, if you need a specific one (whether by SysID or name to search for the SysID).

If you have the given user in some field, let me know in which one...

So, can you describe in a bit more detail, what your question is in fact?

BR

Dirk 

I was wanting to lookup the sysapprover_approver record(s) in my workflow which is an on insert workflow. I wanted to see if an approver is assigned to the workflow if he might have already approved this (being part of a different approval group). I think I have it working now; This condition calls this code and if it returns yes, will skip the approver because he has already approved. answer = function ifScript() { var gr = new GlideRecord('sysapproval_approver'); gr.addQuery('document_id',current.sys_id); gr.addQuery('approver','20d5bfa64fdf46007e2f0ad14210c7ed'); gr.addQuery('state','approved'); gr.query(); if(gr.next()) { return 'yes'; } else {return 'no';} }