Need help with my onAfter transform script. It is not showing the image properly on the image prompt/field. Please help.

John Clyde Ap_a
Giga Expert

I am trying to dynamically add attachments to every imported record and want to automatically attach it to the image prompt, I tried a script but I cannot seem to make the image display properly work..

 

I have tried this script:

find_real_file.png

 

This is what it shows up:

find_real_file.png

Its not an image as there is no preview for it. I'm not sure why it is not showing the image.

find_real_file.png

I wanted to display the images just like the other image(note that I manually added the image, but I wanted to dynamically add it base on my script) in the pic below but its not showing it properly.

find_real_file.png

 

 

For reference this is my imported excel table.

find_real_file.png

Here is my mapped fields:

find_real_file.png

 

Can anyone please help? It's been a few days and I am still unfortunately stuck.

 

 

1 ACCEPTED SOLUTION

Hi,

This worked for me

Remove line 10 i.e. don't copy

Add these lines as you are removing line 10

Give correct name of image field from target table.

I assume you are loading images only into the target table from import set.

var attachmentRecord = new GlideRecord("sys_attachment");
attachmentRecord.orderByDesc("sys_created_on");
attachmentRecord.addQuery("table_name", target.sys_class_name);
attachmentRecord.addQuery("table_sys_id", target.sys_id);
attachmentRecord.addQuery("file_name", fileName);
attachmentRecord.query();
if(attachmentRecord.next()){

	var sysId = new global.VariableUtil().copyAttachment(attachmentRecord.getUniqueValue(), "ZZ_YY" + target.sys_class_name, target.sys_id);
	target.u_imageFieldName = sysId; // give your field name which holds image
	target.update();

}

Regards
Ankur

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

View solution in original post

24 REPLIES 24

Hi,

Did you try with jpg or jpeg?

regards
Ankur

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

Hello Ankur,

Yes I have tried it but unfortunately It still won't work.

For reference this is the excel data

find_real_file.png

 

Here is the record, the attachment works but assigning the image on the image prompt doesn't.

find_real_file.png

 

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

	// Add your code here
// 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();
	var attachmentSysId = sa.write(target, fileName, fileContentType, fileData);

	var sysId = new global.VariableUtil().copyAttachment(sysId, "ZZ_YY" + target.sys_class_name, target.sys_id);
	target.Image = attachmentSysId; // give your field name which holds image
	target.update();
	
})(source, map, log, target);

Have you checked that those images can be downloaded from sys_attachment directly and once downloaded those can be opened in a viewer or something as valid image files?

Perhaps the base64 data is actually corrupt - e.g. the import set row table field length for images it too small.

Hello János,

 I have rechecked it and viewed them in a viewer  and indeed you are right, there seems to be a problem with the base 64 data as it shows this

find_real_file.png

 

even though it shows an image icon on the attachment find_real_file.png

Is there a workaround you can recommend? Is there something wrong with the script as to why its not handling the image correctly?

find_real_file.png

Hi,

The script I shared has worked fine for me.

Possibly as mentioned by Janos your base64 data might be corrupt based on import field length.

To test the functionality you can do this

1) add an image to record manually

2) then use this script to confirm if it gets attached to the image field

you will have to hard-code the table name and record sysId

Regards
Ankur

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