When Notification Mail script link is clicked it points to "record not found"

Snow Angel
Tera Expert

Hello,

I'm using the following mail script in an email notification but when the link is clicked, it shows "Record Not found" What change can I make to the script so it fetched the right sys approval record? TIA

SnowAngel_0-1683914165462.pngSnowAngel_1-1683914190111.png

 

 

 

 

(function runMailScript(/* GlideRecord */ current, /* TemplatePrinter */ template,
/* Optional EmailOutbound */ email, /* Optional GlideRecord */ email_action,
/* Optional GlideRecord */ event) {

var sysID = current.sys_id.toString();
var instance = gs.getProperty('instance_name');
var url = "https://"+instance+".servicenowservices.com/servicedesk?id=approval&table=sysapproval_approver&sys_id="+sysID;

var link = "<a href='"+url+"'>view and approve or reject this item on the Portal</a>";
template.print(link);

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

1 ACCEPTED SOLUTION

DUGGI
Giga Guru

@Snow Angel 

 

The issue is likely due to the sys_id that you're passing into the URL. The sys_id you're currently using (current.sys_id) is probably the sys_id of the record that triggered the notification, not the sys_id of the approval record. The approval records are stored in a separate table (sysapproval_approver), and each approval record has its own unique sys_id.

If you have an approval related to the current record, you will need to find the sys_id of that approval record and pass that into the URL.

Here's how you could do that:

javascriptCopy code
(function runMailScript(/* GlideRecord */ current, /* TemplatePrinter */ template,
/* Optional EmailOutbound */ email, /* Optional GlideRecord */ email_action,
/* Optional GlideRecord */ event) {

    // Find the approval record related to the current record
    var approval = new GlideRecord('sysapproval_approver');
    approval.addQuery('document_id', current.sys_id);
    approval.query();
    if (approval.next()) {
        var sysID = approval.sys_id.toString();
        var instance = gs.getProperty('instance_name');
        var url = "https://"+instance+".service-now.com/servicedesk?id=approval&table=sysapproval_approver&sys_id="+sysID;

        var link = "<a href='"+url+"'>view and approve or reject this item on the Portal</a>";
        template.print(link);
    }

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

In this script, I'm creating a new GlideRecord for the sysapproval_approver table and querying for an approval record that is related to the current record (approval.addQuery('document_id', current.sys_id)). If such an approval record is found, I'm using its sys_id for the URL.

Note: Please replace "service-now.com" with your instance's base URL.

Please also make sure to handle the case when there are multiple approvals for the same record. The script above will only use the first one it finds.

View solution in original post

2 REPLIES 2

DUGGI
Giga Guru

@Snow Angel 

 

The issue is likely due to the sys_id that you're passing into the URL. The sys_id you're currently using (current.sys_id) is probably the sys_id of the record that triggered the notification, not the sys_id of the approval record. The approval records are stored in a separate table (sysapproval_approver), and each approval record has its own unique sys_id.

If you have an approval related to the current record, you will need to find the sys_id of that approval record and pass that into the URL.

Here's how you could do that:

javascriptCopy code
(function runMailScript(/* GlideRecord */ current, /* TemplatePrinter */ template,
/* Optional EmailOutbound */ email, /* Optional GlideRecord */ email_action,
/* Optional GlideRecord */ event) {

    // Find the approval record related to the current record
    var approval = new GlideRecord('sysapproval_approver');
    approval.addQuery('document_id', current.sys_id);
    approval.query();
    if (approval.next()) {
        var sysID = approval.sys_id.toString();
        var instance = gs.getProperty('instance_name');
        var url = "https://"+instance+".service-now.com/servicedesk?id=approval&table=sysapproval_approver&sys_id="+sysID;

        var link = "<a href='"+url+"'>view and approve or reject this item on the Portal</a>";
        template.print(link);
    }

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

In this script, I'm creating a new GlideRecord for the sysapproval_approver table and querying for an approval record that is related to the current record (approval.addQuery('document_id', current.sys_id)). If such an approval record is found, I'm using its sys_id for the URL.

Note: Please replace "service-now.com" with your instance's base URL.

Please also make sure to handle the case when there are multiple approvals for the same record. The script above will only use the first one it finds.

@DUGGI 

Thank you so much! You rock! It worked 🙂