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

MrMuhammad
Giga Sage

Try this

function ifScript() {
   var gr = new GlideRecord('sysapproval_approval');
   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';}
}

 

Please mark this correct & helpful if it answered your question.

Thanks & Regards,
Sharjeel

 

Regards,
Muhammad

You can use both sysapproval and document_id fields. Though, the table name is not correct. Slight typo. Should be: sysapproval_approver

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

Kunal Varkhede
Tera Guru

Hi,

 

I thought you don't need to write script for your use case it is simply possible by using if Activity condition and set Values activity. Check below Screenshot to more understanding.

Use If Activity before Approval activity and also used Set Values activity after the Approval Activity 

find_real_file.png Check If condition in below screenshot:

find_real_file.png

And check Set Value Activity :-

find_real_file.png

Please Mark Correct/Helpful answer if it help you in any way.

Thanks and Regards,

Kunal

Thanks that could be helpful but in this case I think I have to script it. We have a use case where there are several approvals. 1) A group review/approval is required. 2) Later in the flow ypur manager approval is required. If your manager happened to be a part of the initial group and he already has approved, we don't want them to have to approve again. I believe this code works now 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';} } ;;==