The CreatorCon Call for Content is officially open! Get started here.

Create attachment from javascript after ctrl + v a screenshot

smicloud
Tera Guru

We are trying to create a function so the user can paste a screenshot to a task by pressing ctrl+v. The most important browser to get this working in is IE11. We have created functionality so we can paste the actual image and base64 encode it in the javascript. Now, the problem is that we cannot sort out how to actually create the attachment and save it with the task. Here is how we want it to work:

1. If possible, add the attachment to the task as if we added it using the normal component but by using javascript to access the components function for adding the attachment. This means no saving or reloading of the task. Cannot find any such kind of functionality. Is there any javascript function we can access to fix this? The normal component must use something, is this available to use? Or the function used when dragging a file to the task may also be used if available?

2. In case we cannot solve it as above, the other option we have been looking into is using the SOAP method AttachmentCreator. I see two suggestions we would like to explore: i) Use the normal way of accessing the SOAP API as if it was an external application. However, I am not sure if this is possible, is it? (http://wiki.servicenow.com/index.php?title=AttachmentCreator_SOAP_Web_Service#gsc.tab=0) ii) The other way is if we can access the some include script using GlideAjax to post the attachment (http://wiki.servicenow.com/index.php?title=GlideAjax#gsc.tab=0). Then I guess one has to refresh to get the attachments visible in the attachment list.

3. Any suggestions you may have in addition to this is of course appreciated.

Is it possible to solve and how?

1 ACCEPTED SOLUTION

guhann
Mega Guru

Hi Niklas,



Try this small utility. This may be helpful.



1) Create an UI Script with below code.


function addPasteEvent() {


  document.onpaste = function (event) {


  // use event.originalEvent.clipboard for newer chrome versions


  var items = (event.clipboardData   || event.originalEvent.clipboardData).items;


  // find pasted image among pasted items


  var blob = null;


  for (var i = 0; i < items.length; i++) {


  if (items[i].type.indexOf("image") === 0) {


  blob = items[i].getAsFile();


  }


  }


  // load image if there is a pasted image


  if (blob !== null) {


  var reader = new FileReader();


  reader.onload = function(event) {


  console.log(event.target.result); // data url!


  attachClipboardData(event.target.result);


  };


  reader.readAsDataURL(blob);


  }


  };


}



function attachClipboardData(data){


  var recordSysID = g_form.getUniqueValue();


  var recordTable = g_form.getTableName();


  var temp = data.toString().replace(/data:/g,'').split(';');


  var contentType = temp[0];


  var fileName = 'Screen shot.'+contentType.split('/')[1];


  var content = temp[1].toString().replace(/base64,/g,'');




  showLoadingDialog();


  var attach = new GlideAjax('<script>'); // Specify the script include name after completing step 2


  attach.addParam('sysparm_name','attachScreenshot');


  attach.addParam('sysparm_tableName',recordTable);


  attach.addParam('sysparm_sys_id',recordSysID);


  attach.addParam('sysparm_content_type',contentType);


  attach.addParam('sysparm_value',content);


  attach.addParam('sysparm_filename',fileName);


  attach.getXML(getResponse);



  function getResponse(response) {


  alert('Screen shot attached to ticket!');


  window.location.reload();


  }


}



2) Create a script include with below function in it.


attachScreenshot : function() {


  var StringUtil = GlideStringUtil;


  var value = StringUtil.base64DecodeAsBytes(this.getParameter('sysparm_value'));


  var tableName = this.getParameter('sysparm_tableName');


  var sys_id = this.getParameter('sysparm_sys_id');


  var filename = this.getParameter('sysparm_filename');


  var content_type = this.getParameter('sysparm_content_type');



  var attachment = new Attachment();


  attachment.write(tableName, sys_id, filename, content_type, value);


  },



3) Call the UI Script function in an onload script of incident/problem/change/global table as required.


function onLoad() {


  addPasteEvent();


}



After doing these just take a screen shot using print screen and paste it over an incident form. You can see the attachment added after a form reload.


NOTE: Its working fine in Chrome and FF. I don't have IE 11 to test. You can give it a try.



Try this and let me know if any questions.


View solution in original post

49 REPLIES 49

Hi Graham,



Thank you for your feedback ! I thnik we have finished the job so I've published a new version on share.



Greetings


Jean-Luc


Hello Jean,

Cannot find the share anymore. Is it only me or it's deleted?

 

Thanks much for a brilliant post

Sunny

Hi Graham,



Do you know by any chance how to save a generated pdf file in servicenow?


Here is my post with more details related to the question




larstange
Mega Sage

The solution provided by Niklas is brilliant and just what we needed. Even though it is possbile to paste into an HTML field, lots of task types does not have such a field, and it does not work in record producers either.



I have take Niklas' solution and worked on improving the user friendliness and make it work on records that have not been saved yet.


It uses a UI macro instead, which adds an information area to the task form.


find_real_file.png



You can get my solution ServiceNow Share



See a demo here


This is a great solution! Any idea why when I add it to incident worknotes entered no longer post to the incident?