- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-11-2022 06:33 PM
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:
This is what it shows up:
Its not an image as there is no preview for it. I'm not sure why it is not showing the image.
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.
For reference this is my imported excel table.
Here is my mapped fields:
Can anyone please help? It's been a few days and I am still unfortunately stuck.
Solved! Go to Solution.
- Labels:
-
Studio
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-11-2022 10:09 PM
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-17-2022 10:41 PM
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.
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?
The site I used for encoding the images to base 64 data is this one. https://elmah.io/tools/base64-image-encoder/
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-17-2022 11:03 PM
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-18-2022 12:58 AM
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
from the excel
May I ask how did you able to make it work knowing usually images converted to base 64 data have big character counts?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-18-2022 03:38 AM
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).
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-17-2022 09:52 PM
Hi
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.