Name

sn_anon_rc.ARCPDFGenerationBase

Description

Base class of methods used for generating pdf to be dowloaded on anonymous case creation. Edit script include ARCPDFGeneration to override specific methods or add custom functionality.

Script

var ARCPDFGenerationBase = Class.create();
ARCPDFGenerationBase.prototype = {
  BODY: '<p style="float: left;font-weight: normal;font-size: 18px;color: #2E2E2E;font-family: arial, helvetica, sans-serif;">'+gs.getMessage("Anonymous Report Center")+' </p><p style="float: right;font-weight: 600;font-size: 14px;color: #2E2E2E;margin-top:20px;font-family: arial, helvetica, sans-serif;">${hr_service}</p>' +
      '<div style="clear: both;"></div>' +
      '<hr style="height:1px;border-width:0;color:gray;background-color:gray;padding:0px;margin:-15px 0px 10px 0px"/>' +
      '<div style="background: #FBFAE9;border: 1px solid #DFD139;box-sizing: border-box;border-radius: 3px;">' +
      '<img src="data:image/png;base64, ${info_icon}" alt="info_icon" />' +
      '<p style="padding:  0px 15px 0px 15px;font-style: normal;font-weight: normal;font-size: 12px;font-family: arial, helvetica, sans-serif;">'+gs.getMessage("Keep this information in a safe place if you want to follow up on this report or review the information you submitted. For your security and confidentiality, we can't retrieve lost report keys and report numbers.")+'</p>' +
      '</div>' +
      '</br>' +
      '<table style="width: 100%;border-spacing: 5px;margin:0px 0px 15px 0px">' +
      '<tbody>' +
      '<tr>' +
      '<td style="width: 20%; text-align: center;border: 1px solid #E5E5E5;border-radius: 2px;">' +
      '<p style="color: #4A4A4A;font-size: 10px;font-family: arial, helvetica, sans-serif;">'+gs.getMessage("Report key")+'</p>' +
      '<p style="color: #000000;font-size: 12px;font-family: arial, helvetica, sans-serif;">${secret_key}</p>' +
      '</td>' +
      '<td style="width: 20%; text-align: center;border: 1px solid #E5E5E5;border-radius: 2px;">' +
      '<p style="color: #4A4A4A;font-size: 10px;font-family: arial, helvetica, sans-serif;">'+gs.getMessage("Report number")+'</p>' +
      '<p style="color: #000000;font-size: 12px;font-family: arial, helvetica, sans-serif;">${number}</p>' +
      '</td>' +
      '<td style="width: 60%; text-align: center;border: 1px solid #E5E5E5;border-radius: 2px;">' +
      '<p style="color: #4A4A4A;font-size: 10px;font-family: arial, helvetica, sans-serif;">'+gs.getMessage("Anonymous Report Center URL")+'</p>' +
      '<p style="color: #000000;font-size: 12px;font-family: arial, helvetica, sans-serif;">${url}</p>' +
      '</td>' +
      '</tr>' +
      '</tbody>' +
      '</table>' +
      '<p style="font-weight: 600;font-size: 10px;color: #717171;font-family: arial, helvetica, sans-serif;">'+gs.getMessage("Created")+'</p>' +
      '<p style="font-weight: normal;font-size: 12px;color: #2E2E2E;font-family: arial, helvetica, sans-serif;">${sys_created_on}</p>' +
      '${description}',
  INSTANCE: gs.getProperty('glide.servlet.uri'),
  ARC_PORTAL: 'arc',

  initialize: function() {
      this.document = null;
  },

  generatePDF: function(reportId, reportTable, key) {
      var keyRecord = new GlideRecord("sn_anon_rc_report_key");
      keyRecord.addQuery('table_name', reportTable);
      keyRecord.addQuery('report', reportId);
      keyRecord.query();
      if (keyRecord.next()) {
  		var testHash = new GlideDigest().getSHA256Hex("" + key + reportId);
          if (keyRecord.getValue("key_hash") === testHash) {
  			var pdfBody = this.preparePDFBody(reportId, reportTable, key);
  			var fileName = keyRecord.getDisplayValue('report');

  			var pdfGenerationAPI = new sn_pdfgeneratorutils.PDFGenerationAPI();
  			pdfGenerationAPI.convertToPDF(pdfBody, keyRecord.getTableName(), keyRecord.getUniqueValue(), fileName);
  			return {
  				content: this.fetchPDFContent(keyRecord),
  				fileName: fileName
  			};
  		}
  		return {
  			error: gs.getMessage("Report not found for PDF generation")
  		};
      }
  },

  fetchPDFContent: function(keyRecord) {
      if (keyRecord) {
          var attachment = new GlideSysAttachment();
          var agr = attachment.getAttachments(keyRecord.getTableName(), keyRecord.getUniqueValue());
          if (agr.next()) {
              var pdfB64 = attachment.getContentBase64(agr);
              attachment.deleteAttachment(agr.sys_id);
              return pdfB64;
          }
      }
  },

  preparePDFBody: function(reportId, reportTable, key) {
      var regex = /\${([^}]*)}/g;
      var pdfBody = this.BODY;
      var matched = this.BODY.match(regex);
      pdfBody = pdfBody.replace('${secret_key}', key);
      pdfBody = pdfBody.replace('${url}', this.INSTANCE + this.ARC_PORTAL);
      var report = new GlideRecord(reportTable);
      if (report.get(reportId)) {
          for (var i in matched) {
  			var matchedStr = matched[i].match(/\${(.*)}/).pop();
              matchedStr.trim();
  			if (matchedStr != "description")
  				pdfBody = pdfBody.replace(matched[i], report.getDisplayValue(matchedStr));
  			else {
                  var description = "";
  				var qa = new GlideRecord("question_answer");
  				qa.addQuery("table_name", reportTable);
  				qa.addQuery("table_sys_id", reportId);
  				qa.addNotNullQuery("value");
  				qa.orderBy("order");
  				qa.query();
  				while (qa.next()) {
  					var question = qa.getDisplayValue("question.question_text").replace(/(\r\n|\r|\n)/g, '</br>');
  					var answer = qa.getDisplayValue("value").replace(/(\r\n|\r|\n)/g, '</br>');
  					if (answer == "true") {
  						description += '<p style="font-weight: 600;font-size: 10px;color: #717171;font-family: arial, helvetica, sans-serif;"><input type="checkbox" checked="checked" />'+question+'</p>';
  						continue;
  					}
  					description += '<p style="font-weight: 600;font-size: 10px;color: #717171;font-family: arial, helvetica, sans-serif;">'+question+'</p><p style="font-weight: normal;font-size: 12px;color: #2E2E2E;margin: 0px;font-family: arial, helvetica, sans-serif;">'+answer+'</p>';
  				}
                  pdfBody = pdfBody.replace(matched[i], description);
              }
          }
      }

      return pdfBody;
  },

  type: 'ARCPDFGenerationBase'
};

Sys ID

0c76bee7c3d230102920b8889d40ddd7

Offical Documentation

Official Docs: