Approval UI Action From Custom Table

chriscorbett
Giga Contributor

Morning All -

I am working on a custom application that generates approval records via workflow. What I would like to do is have Approve and Reject buttons on my custom table that updates the state field in the sysapproval_approver table.

To that end I have made a UI Action with this code:

var currentRec = current.getValue('number');

var gr = new GlideRecord('sys_approval_approver');

gr.addQuery('approval_for',currentRec);

gr.query();

while (gr.next()) {

  gr.state = 'approved';

}

gr.update();

and it is not working. I also had currentRec set like this:

var currentRec = current.sys_id;

and it did not work with that either.

So I guess I'm confused about which field I should be querying for in the Approval table and what I should be passing into it.

Guidance on this issue would be appreciated.

Thanks!

Chris

1 ACCEPTED SOLUTION

Brad Tilton
ServiceNow Employee
ServiceNow Employee

Hi Chris,



Try something more like this:



var currentRec = current.sys_id;


var gr = new GlideRecord('sysapproval_approver');


gr.addQuery('sysapproval',currentRec);


gr.addQuery('state', 'requested');


gr.query();



while (gr.next()) {


  gr.state = 'approved';


  gr.update();


}


View solution in original post

8 REPLIES 8

Chuck Tomasi
Tera Patron

The name of the table is sysapproval_approver. Try this.



var currentRec = current.getValue('sys_id');


var gr = new GlideRecord('sysapproval_approver');


gr.addQuery('approval_for',currentRec);


gr.query();



while (gr.next()) {


  gr.state = 'approved';


}


gr.update();


Brad Tilton
ServiceNow Employee
ServiceNow Employee

Hi Chris,



Try something more like this:



var currentRec = current.sys_id;


var gr = new GlideRecord('sysapproval_approver');


gr.addQuery('sysapproval',currentRec);


gr.addQuery('state', 'requested');


gr.query();



while (gr.next()) {


  gr.state = 'approved';


  gr.update();


}


chriscorbett
Giga Contributor

Thanks guys - much appreciated!



Brad's example worked - to some extent. It updated the state field like I wanted, but instead of sending the request on to the next person in line, it generated another approval for the same person that just approved it.



Does some other field need to be changed in order for my workflow to actually recognize that an approval has taken place?



This is a new behavior I've not seen as yet.... I'm confused.


It should work the same way as if someone approved it manually. Have you tested approving it manually and seeing if it does that? Posting a screenshot of the workflow and approval activity definition could help as well.