Query via glide record behaving unexpectedly in mail_script

Tylerjknapp
Tera Contributor

Hi everyone, I had posted this as a reply to a different thread I posted but I figured since this is a different question than I originally asked in that thread I'd ask it as a new one. 

 

I have a requirement of sending out a notification to the requested for of a catalog item if and then the 7 BD timer for each approval gets breached. We are trying to stay away from using business rules. So I thought I had finally figured out the mail_script and that it was working and then I got to test three and it captured the approval that was not approved but the previous two tests it was capturing was the last approval that had been approved. Below is the mail script I am using. Any thoughts or ideas on why the script is behaving unexpectedly and going between capturing the last approved approval and the last triggered approval? 

 

(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
    /* Optional EmailOutbound */
    email, /* Optional GlideRecord */ email_action,
    /* Optional GlideRecord */
    event) {
 
    var gr = new GlideRecord("sysapproval_approver");
var grRITM = new GlideRecord('sc_req_item');
grRITM.get(current.sys_id); 
 
    gr.addQuery("sysapproval", current.sys_id);
    gr.orderByDesc('created');
    gr.query();
 
    template.print('Approval Cancelled Due to Approval Not Being Approved Within 7 Business Days.' + '\n');
    if (gr.next()) {
        template.print('Approval Level That Exceeded 7 Business Days Timer - ' + gr.wf_activity.getDisplayValue());
    }
})(current, template, email, email_action, event);
2 REPLIES 2

Dan Ostler
Tera Guru

The simplest way to filter out any approvals that had been approved would be to specify that you only want approvals that are still in the Requested state, or approvals that are not in the Approved state. For example:

 

// Add this line below gr.addQuery('sysapproval', current.sys_id);
gr.addQuery('state', 'requested');

Hi Dan, 

 

Thanks for your reply, so the issue I run into going down that path is that the approval state are getting change to canceled even if they have been approved once the workflow closes out as closed complete. I did just think of that I can add a timer of 30 seconds or 1 minute either right after the notification activity or the run script that we use to close out our RITMs gets triggered. Please see the screenshots below showing that the approvals get changed to cancelled even if they were approved. 

 

Screen Shot 2022-10-24 at 10.33.36 AM.png

Screen Shot 2022-10-24 at 10.52.24 AM.png