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

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

so you need to copy file from the record to the field?

Regards
Ankur

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

Hi,

check this

How to take an attachment and put it into an image field

How to copy an attachment to an Image field within the same form?

How to set a default image on an image field (not knowledge)

Regards
Ankur

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

Hello! Yes so it will be displayed like this.

find_real_file.png

How should I proceed with it since it has a base64data? What would the script look like?

Hi,

Did you go through the links I shared above?

Regards
Ankur

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