Retrieving Approval record from within Request Item

ashley_townsen1
Giga Contributor

I am currently trying to write a script that will retrieve the approval record associated with a request item.

The script is run within a notification that is triggered through an event (raised in a workflow).

Within the workflow I have an Approval task that is responsible for both creating the approval records (through the inbuilt function of the activity, nothing custom here) and raising the relevant event.

 

The issue is that the script is unable to locate the approval record using sysapproval on the first instance, and it seems that this is the case because it is running BEFORE the record has actually been created in the sysapproval_approver table. Debugging through I can see that if I run the script a second time, using the previous request item sys_id, it successfully locates the approval record. Although I cannot see why the script is not locating the record first time as I have even tried to implement a wait in the workflow to ensure the event is raised AFTER the approvals are created.

 

Below is the script in question, pretty standard and I am comfortable it should work, the issue is with the timing of the event I believe so any help would be greatly appreciated.

 

var gr = new GlideRecord('sysapproval_approver');
if (gr.get('sysapproval', current.request)){
   gs.log("TEST FOUND APPROVAL RECORD - NUMBER : " + gr.sys_id);
}
else {
   gs.log("TEST DID NOT FIND APPROVAL USING RITM : " + current.request);
}
if (gr.get('sysapproval', '7e00f2316f65210093c0e1b3dd3ee401')){
   gs.log("TEST FOUND APPROVAL WITH OLD SYS NUMBER");
}
answer = 'true';

 

For reference, the wait I have within the workflow is based on the condition Approval = requested (approval=requested^EQ). Perhaps this wait is not sufficient to ensure the record has been created before firing off the event?

1 ACCEPTED SOLUTION

current in your example is the RITM that is triggering the event.   So your script should be looking for Approvals for the RITM not the REQ (NOT current.request but current.sys_id)



  1. if (gr.get('sysapproval', current.sys_id)){  

View solution in original post

7 REPLIES 7

Hi Ashley,



To pass the Sysapproval Context in the eventQueue function try this code.



var sysapproval = new GlideRecord("sysapproval_approver");


sysapproval.addQuery('sysapproval', current.getValue("sys_id"));


sysapproval.query();


if(sysapproval.next()) {


gs.eventQueue("approval.access_entity", sysapproval, userid....);


}




This should solve the context issue for you.




Thanks,
Mandar


current in your example is the RITM that is triggering the event.   So your script should be looking for Approvals for the RITM not the REQ (NOT current.request but current.sys_id)



  1. if (gr.get('sysapproval', current.sys_id)){  

Thanks Terri, great spot! Nothing to do with timing whatsoever in the end.


Bit of a red-herring when I was finding the result the second time around - not sure what that was but there we go. Lesson learned!