Email Script Isn't Working

Community Alums
Not applicable

Hey All, 

 

I have an email script that works properly when I run it via the background script, but when I preview it for the email notification, it isn't working. 

 

My email notification is running off the change_request table, but I'm using the sysapproval_approver table for my GlideRecord query. 

 

Below is my script: 

 var gr = new GlideRecord("sysapproval_approver");
    gr.get(current.sysapproval);
    template.print("This is the approver who is rejecting the change request: " + gr.approver.getDisplayValue() + " from " + gr.group.assignment_group.getDisplayValue() + " has rejected this change. Please see rejection comments below and contact " + gr.approver.getDisplayValue() + " if you have questions. ");

 

But when I preview the notification, I get this: 

SampleV3.PNG

What am I doing wrong? Am I missing something? Does the method get no longer work? I'm confused b/c it is working as expected via the background script.

1 ACCEPTED SOLUTION

Akif_Shah
Kilo Sage
Kilo Sage

If the notification is on the change request table, the "current" object will be referencing the change request record triggering the notification. So current.sysapproval is not valid since there is no sysapproval field on change request.

So you need to use something like

var gr = new GlideRecord('sysapproval_approver');

gr.addQuery('document_id', current.sys_id);

gr.addQuery("state", "rejected"); // Filter out only approved record if you want all comment this line

gr.query();
if(gr.next()){
  template.print("YOUR CONTENT GOES HERE");
}

View solution in original post

9 REPLIES 9

Peter Williams
Kilo Sage

i was able to get mine to work from the script below:

Just updated the approved to rejected and it should work

 

(function runMailScript(/* GlideRecord */ current, /* TemplatePrinter */ template,

          /* Optional EmailOutbound */ email, /* Optional GlideRecord */ email_action,

          /* Optional GlideRecord */ event) {

 

       var gr = new GlideRecord('sysapproval_approver');

    gr.addQuery('document_id', current.sys_id);

    gr.addQuery("state", "approved"); // Filter out only approved record if you want all comment this line

    gr.query();

 

    //Approval Details

 

    template.print('<div align="left">');

    template.print('<table class="MsoNormalTable" style="width: 100%;" border="0" cellpadding="0">');

    template.print('<tbody>');

    template.print('<tr>');

    template.print('<td style="width: 100%; border: none; border-bottom: solid #6F263D 1.5pt; padding: 7.5pt .75pt .75pt .75pt;" valign="bottom">');

    template.print('<h2 style="margin: 10pt 0in 0in; break-after: avoid; font-size: 11pt; font-family: Calibri, sans-serif; font-style: italic;"><strong><em><span style="color: black; font-family: Calibri; font-size: medium;"><span style="font-size: 12.0pt; color: black; background: white; font-style: normal;">Details d&rsquo;approbation / Approval Details</span></span></em></strong></h2>');

    template.print('</td>');

    template.print('</tr>');

    template.print('</tbody>');

    template.print('</table>');

    template.print('</div>');

 

    template.print('<div align="left">');

    template.print('<table class="MsoNormalTable" style="width: 100%;" border="0" cellpadding="0">');

    template.print('<tbody>');

    while (gr.next()) {

       

        template.print('<tr style="height: 12pt;">');

        template.print('<td style="padding: 0.75pt; height: 21.4489px;"><span style="font-family: arial, helvetica, sans-serif; font-size: small;"><span style="font-size: 11pt;"><span style="font-family: arial, helvetica, sans-serif; font-size: small;"><span style="font-size: 11pt;">' + gr.approver.getDisplayValue() + '</span></span>&nbsp;</span></span></td>');

        template.print('<td style="padding: 0.75pt; height: 21.4489px;"><span style="font-family: arial, helvetica, sans-serif; font-size: small;"><span style="font-size: 11pt;"><span style="font-family: arial, helvetica, sans-serif; font-size: small;"><span style="font-size: 11pt;">' + gr.state.getDisplayValue() + '</span></span>&nbsp;</span></span></td>');

        template.print('<td style="padding: 0.75pt; height: 21.4489px;"><span style="font-family: arial, helvetica, sans-serif; font-size: small;"><span style="font-size: 11pt;"><span style="font-family: arial, helvetica, sans-serif; font-size: small;"><span style="font-size: 11pt;">' + gr.sys_updated_on.getDisplayValue() + '</span></span>&nbsp;</span></span></td>');

 

        template.print('</tr>');

    }

        template.print('</tbody>');

        template.print('</table>');

        template.print('</div>');



})(current, template, email, email_action, event);

Amit Pandey
Kilo Sage

Hi @Community Alums 

 

Can you try this-

 

var gr = new GlideRecord("sysapproval_approver");
if (gr.get(current.sysapproval)) {
    template.print("This is the approver who is rejecting the change request: " + current.approver.getDisplayValue() + " from " + current.group.assignment_group.getDisplayValue() + " has rejected this change. Please see rejection comments below and contact " + current.approver.getDisplayValue() + " if you have questions.");
} else {
    gs.error("Failed to retrieve sysapproval_approver record for change request: " + current.sysapproval);
}

Regards,

Amit

Akif_Shah
Kilo Sage
Kilo Sage

If the notification is on the change request table, the "current" object will be referencing the change request record triggering the notification. So current.sysapproval is not valid since there is no sysapproval field on change request.

So you need to use something like

var gr = new GlideRecord('sysapproval_approver');

gr.addQuery('document_id', current.sys_id);

gr.addQuery("state", "rejected"); // Filter out only approved record if you want all comment this line

gr.query();
if(gr.next()){
  template.print("YOUR CONTENT GOES HERE");
}

Community Alums
Not applicable

Thank you so much! I wasn't aware of document_id. This works.

 

 

Peter Williams
Kilo Sage

i would suggest using the While and not If

 var gr = new GlideRecord('sysapproval_approver');

    gr.addQuery('document_id', current.sys_id);

    gr.addQuery("state", "approved"); // Filter out only approved record if you want all comment this line

    gr.query();

 while (gr.next()) {

  template.print("YOUR CONTENT GOES HERE");
}