- 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-16-2022 10:35 PM
Hi,
Did you try with jpg or jpeg?
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 12:01 AM
Hello Ankur,
Yes I have tried it but unfortunately It still won't work.
For reference this is the excel data
Here is the record, the attachment works but assigning the image on the image prompt doesn't.
(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);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-17-2022 06:10 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-17-2022 07:35 PM
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
even though it shows an image icon on the attachment
Is there a workaround you can recommend? Is there something wrong with the script as to why its not handling the image correctly?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-17-2022 08:56 PM
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader