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

TJW2
Mega Guru

Try using current.request.sys_id   OR current.request+''   in your GlideRecord Query.



In your post you say the Approval Record associated with the RITM, should you be querying for the RITM record instead of the REQ record?


'gr.get('sysapproval',current.sys_id)'   if the Notification is running on the RITM table.


Unfortunately current.request already evaluates to the sys_id so that doesn't change the behaviour.


Thank you for your response though!


I believe you're right in what is happening. The RITM or REQ is already inserted and triggering the notification before the approval is inserted, which causes it to be missed.


You should be able to adapt the notification to run on the sysapproval table and only fire for specific RITM's (if need be), by doing this you can avoid the race condition happening.


Hi Mark,



Thanks for your reply. I have intentionally set the notification to use the sysapproval_approver table (as per the drop down options) however it seems the context of the notification is actually the request item, and I cannot see a way around this.



From within the Approval task in the workflow I raise the event using: gs.eventQueue("approval.access_entity", current, userid....);


Unfortunately that seems to raise the event with the RITM context rather than the approval, and if current isn't the approval (from within the Approval activity), I'm not sure what else would be.