- 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-11-2022 08:58 PM
Hi,
you can do this
1) add file to record
2) then copy file from record to image field
3) then delete file attached to record
How to set a default image on an image field (not knowledge)
How to add an image to a record via a background script?
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-11-2022 09:08 PM
Hello Ankur,
Thanks for the quick reply. I have a follow up question
1). By "Add file to record" do you perhaps mean that it is already attached to the record? If it is, I think this part is completed. As the script mentioned above already adds attachment files for every record based from the imported set.
2). However, I'm confused as to how I can implement and copy/assign the attached file from the record to the image field. May I ask for help as I am not sure as to why the script is not working.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-11-2022 09:17 PM
Hi,
update line 14 as this
attachment.addQuery('table_sys_id', target.sys_id);
Also ensure you give the correct file name when you are updating it
attachment.file_name = fileName;
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-11-2022 09:35 PM
Hello Ankur,
Unfortunately it still won't work
I have also tried attachment.file_name = fileName; but it also didn't work. I just reverted it back to Image since I was basing it on this thread.
Is there any other script I could try to use to process the attachment and assign it to the image field?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-11-2022 09:48 PM
Hi,
comment line 11 to 20
update script as this since you are commenting 11 to 20
GlideSysAttachment.copy(target.sys_class_name, target.sys_id, "ZZ_YY" + target.sys_class_name, target.sys_id);
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader