Dashboard export to pdf sent by email

Chris__Silva
Kilo Guru

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'
};

 

 

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Chris__Silva 

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.

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

View solution in original post

11 REPLIES 11

Mark Manders
Mega Patron

I am not sure what's going wrong with the URL in your script, but ServiceNow has planned this to be OOB functionality with the Yokohama release (exporting dashboards). Or at least after they release PAE 6.0.1, because on Yokohama RTP the entire scheduled export is broken.


Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark

Ankur Bawiskar
Tera Patron
Tera Patron

@Chris__Silva 

what debugging did you try so far?

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

Ankur Bawiskar
Tera Patron
Tera Patron

@Chris__Silva 

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.

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

Hello @Ankur Bawiskar 

 

I have replaced the function with yours but its the same, when debugging the function i have this url:

 

@@@@@@@encodebodysysparm_ck=com.glide.sys.GlideSession%40361cf6e6&sysparm_target=%2Fhome.do%3Fsysparm_view%3Dess&email=testemail%40gmail.com&sysparm_query=%7B%22dataDocType%22%3Atrue%2C%22orientation%22%3A%22Landscape%22%2C%22papersize%22%3A%22Letter%22%2C%22smartShrink%22%3Atrue%2C%22avoidPageBreakInside%22%3Atrue%2C%22zoomFactor%22%3A100%7D