Use PDIs? Take our 5-minute survey to help shape the PDI roadmap.

Email script for approval in service portal

Nicholas Hromya
Giga Guru

Hello all,

As an approver, I want to get an email that directs me to the Service Portal (not the ESC) to show me the details of a requested item to give approval or rejection.

 

I am not very good at scriptiing...

 

I did find the email script request_item_approval.  This seems to send approvers to the ESC.  My task is make this in the service portal as we have not introduced our teams to the ESC yet.

 

Thank you in advance.

Nick

1 ACCEPTED SOLUTION

 

The sys_id format appears valid, but please add the table parameter to the URL.

Change:

 
var approvalUrl =
    gs.getProperty('glide.servlet.uri') +
    'sp?id=approval&sys_id=' +
    encodeURIComponent(current.getUniqueValue());

to:

 

 
var approvalUrl =
    gs.getProperty('glide.servlet.uri') +
    'sp?id=approval&table=sysapproval_approver&sys_id=' +
    encodeURIComponent(current.getUniqueValue());

The generated URL should be:

 

 
https://<instance>/sp?id=approval
&table=sysapproval_approver
&sys_id=<approval_record_sys_id>

 

Use only one template.print() block. Remove this incomplete duplicate line:

 
template.print(
    '<div style="padding-bottom:32px"> ' +
    '<span style="margin-right: 4px;">View</span> ' +
    '<a href="' + approvalUrl + '"');

Use the complete section below:

 

 
var approvalId = current.getUniqueValue();

var approvalUrl =
    gs.getProperty('glide.servlet.uri') +
    'sp?id=approval&table=sysapproval_approver&sys_id=' +
    encodeURIComponent(approvalId);

var linkStyle =
    'color:#4F52BD;' +
    'font-size:16px;' +
    'line-height:24px;' +
    'font-family:Lato,Arial,sans-serif;' +
    'display:inline-block;';

template.print(
    '<div style="padding-bottom:32px;">' +
        '<span style="margin-right:4px;">View</span>' +
        '<a href="' + approvalUrl + '" ' +
            'style="' + linkStyle + '">' +
            gs.getMessage('approval request new') +
        '</a>' +
    '</div>');

Also confirm that the notification is configured on:

 
Approval [sysapproval_approver]

The current.getUniqueValue() value must be the sys_id of the individual approval record, not the RITM sys_id.

To validate it, copy the sys_id from the generated URL and open this as an administrator:

 

 
sysapproval_approver.do?sys_id=7dc03869476207142f667d88c26d4369
 

View solution in original post

16 REPLIES 16

Ankur Bawiskar
Tera Patron

@Nicholas Hromya 

many customers already moved to ESC portal from SP

any reason you still want to use SP?

Regards,
Ankur
Certified Technical Architect  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader

Management decision.

Suryansh Verma
Mega Sage

@Nicholas Hromya 

 

The value added to portalSuffix is causing the issue because the script then appends another ?id=ticket... query string. The portal suffix should contain only the portal path, not the complete page URL.

Since the email notification runs on sysapproval_approver, use current.sys_id for the Service Portal approval page:

 

var approvalUrl =
    gs.getProperty('glide.servlet.uri') +
    'sp?id=approval&sys_id=' +
    encodeURIComponent(current.getUniqueValue());

 

Use current.sysapproval only when linking to the underlying RITM through sp?id=ticket&table=sc_req_item. The approval page requires the approval record sys_id so that the Approval and Approval Info widgets can display the request and provide Approve/Reject actions.

 

If my response helped, please mark it as correct and close the thread, this helps future readers find the solution faster!

Thank you for replying.  I am not quite getting this.  I understand what you are meaning, but I am not sure how to add it to the script.

 

    var requestItemId = current.sysapproval;
   // var portalSuffix = new sn_ex_emp_fd.FoundationNotificationUtil().getPortalSuffix();
   // var portalSuffix = 'sp?id=approval&sys_id=';
   var portalSuffix = 'sp?id=approval';
   // var requestUrl = portalSuffix + '?id=ticket&table=sc_req_item&sys_id=' + requestItemId;
   var approvalUrl =
    gs.getProperty('glide.servlet.uri') +
    'sp?id=approval&sys_id=' +
    encodeURIComponent(current.getUniqueValue());
	// var requestUrl = portalSuffix + '?id=ticket&table&sys_id=' + requestItemId;
    var colorTheme = 'color: #4F52BD;';
    var fontSize = 'font-size: 16px;';
    var lineHeight = 'line-height: 24px;';
    var fontFamily = 'font-family: Lato, Arial, sans-serif;';
    var display = 'display: inline-block;';
    template.print('<div style="padding-bottom:32px"> <span style="margin-right: 4px;">View</span> <a href="' + requestUrl + '"');
    template.print('style="' + colorTheme + fontSize + fontFamily + display);
    template.print('">');
    template.print(gs.getMessage('approval request'));
    template.print('</a></div>');

 

@Nicholas Hromya 

 

You have added the URL correctly. The remaining issue is in this line:

 

<a href="' + requestUrl + '"

 

You created approvalUrl, but the link still references requestUrl, which is commented out and therefore undefined.

Replace the complete section with the following:

 
var approvalUrl =
    gs.getProperty('glide.servlet.uri') +
    'sp?id=approval&sys_id=' +
    encodeURIComponent(current.getUniqueValue());

var linkStyle =
    'color:#4F52BD;' +
    'font-size:16px;' +
    'line-height:24px;' +
    'font-family:Lato,Arial,sans-serif;' +
    'display:inline-block;';

template.print(
    '<div style="padding-bottom:32px;">' +
        '<span style="margin-right:4px;">View</span>' +
        '<a href="' + approvalUrl + '" ' +
            'style="' + linkStyle + '">' +
            gs.getMessage('approval request') +
        '</a>' +
    '</div>'
);

 

You no longer need these variables:

 

var requestItemId = current.sysapproval;

var portalSuffix = 'sp?id=approval';

 

The important change is:

 

 
// Incorrect
href="' + requestUrl + '"

// Correct
href="' + approvalUrl + '"

 

The generated URL should be:

 

https://<instance>.service-now.com/sp?id=approval&sys_id=<approval_record_sys_id>

 

Here, the sys_id must belong to the Approval record in sysapproval_approver, not the underlying Requested Item. ServiceNow’s Service Portal approval components display approval records from that table and provide the Approve/Reject functionality.

After updating the script, use Preview Notification or send a new test approval email and confirm that the URL contains the approval record’s sys_id.

 

If my response helped, please mark it as correct and close the thread, this helps future readers find the solution faster!