How to overwrite an image field using a script?

John Clyde Ap_a
Giga Expert

I would like to know how to overwrite image fields using a script. I am trying to import a data with an image field and here's where I am at right now. 

(function runTransformScript(source, map, log, target /*undefined onStart*/ ) {

// Add your code here
	var fileName = source.u_file_name;
	var fileContentType = source.u_content_type; // such as image/jpeg or application/pdf
	var fileData = source.u_base64_data;
	var sa = new GlideSysAttachment();
	sa.write(target, fileName, fileContentType, fileData);
	
	
})(source, map, log, target);

The script will make it so that it will show up the attached files but I would like to replace/overwrite the image field with the attached file so there would  be no need for clicking "Click to add" manually. 

find_real_file.png

8 REPLIES 8

Hello Good day,

Yes I did, I have tried the second link but nothing works. Here is what I've done so far

find_real_file.png

find_real_file.png

(function executeRule(current, previous /*null when async*/) {

	// Add your code here
var att = new GlideRecord('sys_attachment');


  att.initialize();

  att.file_name = 'image'; //replace the image field name

  att.content_type = current.content_type;
  att.compressed = current.compressed;
  att.table_name = 'ZZ_YY'+current.table_name;
  att.size_bytes = current.size_bytes;
  att.size_compressed = current.size_compressed;
  att.table_sys_id = current.table_sys_id;

  var attRec = att.insert();
  var attDoc = new GlideRecord('sys_attachment_doc');

  attDoc.addQuery('sys_attachment', current.sys_id);
  attDoc.query();

  while(attDoc.next()){

  var attDocCopy = new GlideRecord('sys_attachment_doc');
  attDocCopy.initialize();
  attDocCopy.sys_attachment = attRec;
  attDocCopy.position = attDoc.position;
  attDocCopy.length = attDoc.length;
  attDocCopy.data = attDoc.data;
  attDocCopy.insert();
  }

})(current, previous);

I looked up at the first link but I am not sure how to interpret it. 

As for the third link, I do not think it will apply as I am not trying to populate the records with a default Image. I am populating them based on the imported image attachment file from an excel sheet. find_real_file.png

Hi,

if you have the base64 data, file name, content type, name etc then why not add it directly to the field instead of first adding to record and then adding to field?

Regards
Ankur

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

Hello Ankur,

That is what I am aiming for but lots of threads are saying I cannot embedded imported images that are coming from excel directly to the image field.

However, since you said it is possible, is there a workaround for this? Can you please give an example of a script for it?

Hi,

I had tried something similar and it worked fine at that time.

Regards
Ankur

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