- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-27-2025 05:52 AM - edited 01-27-2025 12:40 PM
Hello, i found a script include to send dashboard via email using url exporter but there is an issue with it when encoding the url beetween line 169 to 176 (before all values are correct).
If someone could help me to fix that so the dashboard is sent to email with correct recipient.
here is the script include:
/**
* @description Allows for scheduling of homepages and dashboards
* @type {Class}
*/
var DashboardScheduler = Class.create();
DashboardScheduler.prototype = {
/**
* @description The endpoint that accepts our POST
* @type {String}
*/
ENDPOINT : 'sys_confirm_whtp.do',
/**
* @description The Report location
* @type {String}
*/
REPORT : '/home.do?sysparm_view=ess',
/**
* @description Who will receive the report
* @type {String}
*/
RECIPIENTS : '',
/**
* @description The orientation
* @type {String}
*/
ORIENTATION : 'Landscape',
/**
* @description The paper size
* @type {String}
*/
PAPERSIZE : 'Letter',
/**
* @description Whether or not to use smart shrink
* @type {Boolean}
*/
SMART_SHRINK : true,
/**
* @description Whether or not to avoid page breaks
* @type {Boolean}
*/
AVOID_PAGE_BREAK : true,
/**
* @description What the zoom of the report should be
* @type {Number}
*/
ZOOM_FACTOR : 100,
/**
* @description A collection of valid paper orientations
* @type {Object}
*/
ORIENTATIONS : {
'landscape' : 'Landscape',
'portrait' : 'Portrait'
},
/**
* @description A collection of valid paper sizes
* @type {Object}
*/
PAPERSIZES : {
'letter' : 'Letter',
'a3' : 'A3',
'a4' : 'A4'
},
/**
* The value of "g_ck" from the console
* @type {String}
*/
G_CK: '',
/**
* @description Sets values vital to the running of the function
* {String} report The report name
* {String} recipients The recipients of the report
*/
initialize: function(report, recipients) {
this.setReport(report);
this.setRecipients(recipients);
this.G_CK = gs.getSession();
},
/**
* @description Gets the endpoint for making the reporting call
* @return {String} The reporting call end point
*/
getEndpoint: function() {
return "https://" + gs.getProperty('instance_name') + ".service-now.com/" + this.ENDPOINT;
},
/**
* @description Sets the name of the report
* {String} report A report or homepage name
*/
setReport: function(report) {
if (this.isValidPage(report)) {
this.REPORT = "/home.do?sysparm_view=" + report;
}
},
/**
* @description Given a page name, determines if it actually exists in the system
* {String} report A page view name
* @return {Boolean} True if it exists
*/
isValidPage: function(report) {
var page = new GlideRecord('sys_portal_page');
page.addEncodedQuery('sys_class_name=sys_portal_page^view=' + report);
page.query();
if (page.next()) {
return true;
}
return false;
},
/**
* @description Sets the recipients of the report
* {String} recipients An email address
*/
setRecipients: function(recipients) {
this.RECIPIENTS = recipients;
},
/**
* @description Sets the value for page orientation
* {String} orientation A page orientation value
*/
setOrientation: function(orientation) {
if (this.isValidOrientation(orientation)) {
this.ORIENTATION = orientation;
}
},
/**
* @description Sets the value for paper size for the report
* {String} size A paper size value
*/
setPapersize: function(size) {
if (this.isValidSizeValue(size)) {
this.PAPERSIZE = size;
}
},
/**
* @description Sets the value for using smart shrink
* {Boolean} smartShrink A boolean to set whether or not to use smart shrink
*/
setSmartShrink: function(smartShrink) {
if (typeof smartShrink == 'boolean') {
this.SMART_SHRINK = smartShrink;
}
},
/**
* @description Sets the value for avoiding page breaks
* {Boolean} avoidPageBreak A boolean to set for avoiding page breaks
*/
setAvoidPageBreakInside: function(avoidPageBreak) {
if (typeof avoidPageBreak == 'boolean') {
this.AVOID_PAGE_BREAK = avoidPageBreak;
}
},
/**
* @description Sets the zoom factor value
* {Number} zoomFactor A value for the zoom factor between 0 and 100
*/
setZoomFactor: function(zoomFactor) {
if (typeof zoomFactor == 'number' && zoomFactor > 0 && zoomFactor < 101) {
this.ZOOM_FACTOR = zoomFactor;
}
},
/**
* @description Encodes the HTTP Request body properly
* @return {String} A properly encoded HTTP request body string
*/
encodeBody: function() {
var query = JSON.stringify({"dataDocType":true,"orientation":this.ORIENTATION,"papersize":this.PAPERSIZE,"smartShrink":this.SMART_SHRINK,"avoidPageBreakInside":this.AVOID_PAGE_BREAK,"zoomFactor": this.ZOOM_FACTOR});
var total = "sysparm_ck=" + this.G_CK + "&sysparm_target=" + this.REPORT + "&email=" + this.RECIPIENTS + "&sysparm_query=" + query;
var encodedA = encodeURIComponent(total).replace(/%(\d)a/g, "%$1A");
var encodedB = encodedA.replace(/%(\d)b/g, "%$1B");
var encodedC = encodedB.replace(/%(\d)c/g, "%$1C");
var encodedD = encodedC.replace(/%(\d)d/g, "%$1D");
var encodedE = encodedD.replace(/%(\d)e/g, "%$1E");
var encodedF = encodedE.replace(/%(\d)f/g, "%$1F");
var encodedG = encodedF.replace(/%3D/gmi, "=");
return encodedG.replace(/%26/gmi, "&");
},
/**
* @description Verifies that a valid paper size was given
* {String} size A value for paper size
* @return {Boolean} True if the value is valid
*/
isValidSizeValue: function(size) {
return (size == this.PAPERSIZES.letter ||
size == this.PAPERSIZES.a4 ||
size == this.PAPERSIZES.a3) ? true : false;
},
/**
* @description Verifies that a valid orientation value has been given
* {String} orientation A value for page orientation
* @return {Boolean} True if the value is valid
*/
isValidOrientation: function(orientation) {
return (orientation == this.ORIENTATIONS.landscape ||
orientation == this.ORIENTATIONS.portrait) ? true : false;
},
/**
* @description Makes the HTTP Post to trigger the report email
* @return {Number} The status code of the request
*/
makeRequest: function() {
gs.log("@@@@@@@encodebody"+ this.encodeBody());
var message = new sn_ws.RESTMessageV2();
message.setEndpoint(this.getEndpoint());
message.setHttpMethod('post');
message.setBasicAuth('admin', 'mm^a2N4O^eNB');
message.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
message.setRequestBody(this.encodeBody());
var response = message.execute();
return response.getStatusCode();
},
'type' : 'DashboardScheduler'
};
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-27-2025 06:21 AM
can you try this function and replace and use that and see?
encodeBody: function() {
var query = JSON.stringify({
"dataDocType": true,
"orientation": this.ORIENTATION,
"papersize": this.PAPERSIZE,
"smartShrink": this.SMART_SHRINK,
"avoidPageBreakInside": this.AVOID_PAGE_BREAK,
"zoomFactor": this.ZOOM_FACTOR
});
var total = "sysparm_ck=" + encodeURIComponent(this.G_CK) +
"&sysparm_target=" + encodeURIComponent(this.REPORT) +
"&email=" + encodeURIComponent(this.RECIPIENTS) +
"&sysparm_query=" + encodeURIComponent(query);
return total;
}
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-27-2025 08:06 AM
Hi @Chris__Silva ,
Can you try to make change below and then try.
encodeBody: function() {
// Create the query string and encode only the values
var query = JSON.stringify({
"dataDocType": true,
"orientation": this.ORIENTATION,
"papersize": this.PAPERSIZE,
"smartShrink": this.SMART_SHRINK,
"avoidPageBreakInside": this.AVOID_PAGE_BREAK,
"zoomFactor": this.ZOOM_FACTOR
});
// Build the full request parameters, but don't encode 'sysparm_target' and 'email'
var total = "sysparm_ck=" + encodeURIComponent(this.G_CK) +
"&sysparm_target=" + this.REPORT + // Don't encode sysparm_target
"&email=" + this.RECIPIENTS + // Don't encode email address
"&sysparm_query=" + encodeURIComponent(query); // Only encode the JSON query part
// Return the properly encoded body
return total;
},
-------------------------------------------------------------------------
If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.
Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay
-------------------------------------------------------------------------
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-27-2025 10:37 AM
Thank you @Ankur Bawiskar , the script is working correctly now
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-27-2025 06:46 AM
this.G_CK = gs.getSession();
in method initialize and later
"sysparm_ck=" + this.G_CK + ...
in method encodeBody will not work.
You probably mean
this.G_CK = gs.getSessionToken();
in method initialize.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-27-2025 07:50 AM
Hello @-O-
I have tried with gs.getSessionToken() but im getting null but with
gs.getSession();
i get the correct value sysparm_ck=com.glide.sys.GlideSession%401836285
issue is with email when using the rest message email is empty in recipients.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-27-2025 07:58 AM
Depending on how the S.I. is triggered, the session token can be null, that's true, but gs.getSession(); is not the answer for sure.