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

Hello Ankur,

It does look like it is indeed because of the base 64 data cause I even tried it opening the image on paint and it seems that its giving me this.

find_real_file.png

If the base 64 data is corrupted due to import field length, is there a way I could change that and where can I locate it? Or does it perhaps have something to do with my excel table?

find_real_file.png

The site I used for encoding the images to base 64 data is this one. https://elmah.io/tools/base64-image-encoder/

Hi,

nope you cannot correct it from your side as you don't know what's the file content since 3rd party will send it.

Possibly try to check by using csv file instead of excel.

I believe I have answered your original question.

If my response helped please mark it correct and close the thread so that it benefits future readers.

regards
Ankur

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

Hello Ankur,

I have marked it as correct as it seems like it will indeed work. I hope you will be able to see this one. I would just like to know your opinion on it and if there is a workaround for this, the base 64 data that was copied to excel was not the entire thing as I may have hit the character limit. That may have been the cause why it showed up as corrupted.

from the file

find_real_file.png

from the excel

find_real_file.png

May I ask how did you able to make it work knowing usually images converted to base 64 data have big character counts?

You could ask the source to do what SN does: break up the base64 data into chunks (in this case multiple cells. That would solve the issue on both sides: Excel and Import Set Row table. I mean just converting to CSV (which you can do in any case) might solve the problem on the CSV/Excel part, but might still run into table row size limitation on SN size. I'm sure if that would not be a problem, SN would not have decided to break up attachment data into multiple records (see table sys_attachment_doc - contains the data for an attachment in multiple records, chunks of max. 4000 chars).

Muhammad Khan
Mega Sage
Mega Sage

Hi @John Clyde Ap[as,

 

I came to think of a workaround, if you really want to keep the attachment along with the file being displayed in the image field then try creating 2 attachments of the same image file, and then modify the table_name for one of the records.

var gr = new GlideRecord('sys_attachment');
gr.addQuery('table_name', target.sys_class_name);
gr.addQuery('table_sys_id', target.sys_id);
gr.addQuery('file_name', fileName);
gr.addQuery('content_type', 'image/png');
gr.query();

if(gr.next()){
        gr.setValue('file_name', 'image');  // I believe image is the name of your image field.
        gr.setValue('table_name', 'ZZ_YY'+target.sys_class_name);
        gr.update();
}

In case, if you just want the file to appear on image field then there is not need to create/write a 2nd attachment. I have tested this workaround in my instance and it was working.

 

Hopefully, this will help you to resolve your query.