How to change a task's state to approved upon receiving approval

oharel
Kilo Sage

I am looking for directions on how to change a catalog task state to "approved" after receiving an ad-hoc approval for the task. The setup is as follows:

The requested item creates a series of tasks. On of the tasks needs to be approved in order to complete. I added a "request approval" button, which, when clicked, opens the approval form and also changes the state of the task to "pending approval". My problem is that when an approval is received, I am not sure how to change the task state to "approved" (or "complete" or whatever).

Ideas?

4 REPLIES 4

ryan86
Kilo Expert

Hi Harel,



If you are creating the approval from within a workflow, it will deal with this for you. Once the feedback from the approval has been received (Accepted or Rejected), have a workflow activity which changes the current.state to whatever you wish it to be?




approvalCapture.PNG



The, inside the "Approval Action" activities, have something like:



Captureapp2.PNG


Hi Ryan,



Actually, it is sort of outside the flow, and it can come in any task, per need.


The problem is that once requested, I can see the approval relating to the task number in the task, and the task state changes to pending approval, but i'd like to run a business rule/some script to update the state to "approved" or "closed complete"   once the approval is received. Because right now, even if the approval request is approved (and is shown as approved in the task itself), the state of the task stays the same. I guess I'm looking for an example script that would set me in the right direction, or a reference in Service Now documentation as to how to do it.


It would be much easier, and easier to maintain if this was put into a workflow, in my opinion.



However, I'm sure what you require can still be done.



You will need a business rule, which checks the approvals table (sysapproval_approver), and finds the approvals linked to this request/task.
Then, if the STATE of the approval is "Approved", edit the value of current.state to whatever the numeric value of your "approved" choice is.



So:



var number = current.number; // Get the current REQ/TASK number


var gr = new GlideRecord('sysapproval_approvers'); // Create Gliderecord on the approvals table


gr.addQuery('sysapproval', number); // Add query on the 'sysapproval' column using the current.number


gr.query(); // execute query



while (gr.next()) { // while we have some results (your task may have more than one approval?)


      if (gr.state = "approved") // if the state of the found approval is "approved"


      {


                  current.state = "approved"; // Set the current request/task state to "approved"


      }


}




That is very quick and you will need to work through it matching to your column names, but it's along the right tracks I think.



I've not used Business Rules very much, but I believe you will need to have this set as a "before" rule. If you have Eureka, you should be able to do this without scripting by using the condition builder.



Business Rules - ServiceNow Wiki


Hi Ryan,



Thanks for the script.


It does not change the state, and i think it has to do with the way the task is queried. Is there a way to verify the query steps, for instance? because i think it either is not querying or does not "connect" the result to the required state change (the if part).


thanks for the help so far!



var number = current.number; // Get the current REQ/TASK number


var gr = new GlideRecord('sysapproval_approver'); // Create Gliderecord on the approvals table  


gr.addQuery('sysapproval', number); // Add query on the 'sysapproval' column using the current.number  


gr.query(); // execute query  


 


while (gr.next()) { // while we have some results (your task may have more than one approval?)  


      if (gr.state == "approved") // if the state of the found approval is "approved"  


      {  


                  current.state = 6; // Set the current request/task state to "approved"  


      }  


}