Name

sn_grc.GRCSignatureAPI

Description

No description available

Script

var GRCSignatureAPI = Class.create();
GRCSignatureAPI.prototype = {

  initialize: function() {
      this.PREFIX = "data:image/png;base64,";
  },

  acceptSignatureImage: function(table, document, image, imageData, byVendor, isDrawing) {
  	// DEF0317073: security check.
  	byVendor = !gs.hasRole('snc_internal');
  	
      var gr = new GlideRecord('sn_grc_esignature');
      gr.initialize();
      gr.setValue('table', table);
      gr.setValue('user', gs.getUserID());
      gr.setValue('document', document);

      gr.setValue('is_drawing', isDrawing);
      if (isDrawing)
          gr.setValue('data', 'output = ' + imageData);
      else
          gr.setValue('signed_name', imageData);

      gr.setValue('by_vendor', byVendor);
      gr.setValue('signed_on', new GlideDateTime());
  	gr.setValue('signed_by', gs.getUserDisplayName());
      gr.insert();
  	
      var attachmentID = this.insertImage(image.substring(this.PREFIX.length), gr);
  	gr.setValue('signature_image', attachmentID);
  	gr.update();
  	
  	if(table === 'asmt_assessment_instance') {
  		instance = new GlideRecord('asmt_assessment_instance');
  		if(instance.get(document)) {
  			if(byVendor)
  				instance.setValue('sn_vdr_risk_asmt_vendor_signature', gr.getUniqueValue());
  			else 
  				instance.setValue('sn_vdr_risk_asmt_reviewer_signature', gr.getUniqueValue());
  			instance.update();
  		}			
  	}
  	return gr;
  },

  insertImage: function(png, gr) {
      var attachment = new GlideSysAttachment();
      var fileName = "esignature.png";
      var attachmentID = attachment.writeBase64(gr, fileName, 'image/png', png);
  	return attachmentID;
  },

  retrieveSignature: function(table, document, user, byVendor) {
      var gr = new GlideRecord('sn_grc_esignature');
      if (user)
          gr.addQuery('user', user);
      gr.addQuery('table', table);
      gr.addQuery('document', document);
      gr.addQuery('by_vendor', byVendor);
      gr.addActiveQuery();
      gr.orderByDesc('signed_on');
      gr.query();
      if (gr.next() && gr.is_drawing && gr.canRead()) {
          var data = gr.getValue('data');
          if (data)
              return data.substring(data.indexOf("["));
      } else
          return "";
  },

  retrieveSignatureImage: function(table, document, user, byVendor) {
      var gr = new GlideRecord('sn_grc_esignature');
      if (user)
          gr.addQuery('user', user);
      gr.addQuery('table', table);
      gr.addQuery('document', document);
      gr.addQuery('by_vendor', byVendor);
      gr.addActiveQuery();
      gr.orderByDesc('signed_on');
      gr.query();

      if (gr.next() && gr.canRead())
          return this.getAttachementFromSignature('sn_grc_esignature', gr.getUniqueValue());
  },

  getAttachementFromSignature: function(table, sys_id) {
      var gr = new GlideRecord('sys_attachment');
      gr.addQuery('table_name', table);
      gr.addQuery('table_sys_id', sys_id);
      gr.query();
      if (gr.next() && gr.canRead())
          return gr.getUniqueValue();
      else
          return "Cannot find image!";
  },

  type: 'GRCSignatureAPI'
};

Sys ID

0f005f5587b900103058d1a936cb0be3

Offical Documentation

Official Docs: