- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi,
I have a requirement to upload data through excel which contains column of attachment links. I want these attachments to be attached to the records inserted or updated.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
then this sample script in your onAfter transform script should work fine
I tested with this sample external URL and file got attached to incident record
So you can enhance it so that it picks target sysId using target.sys_id
var url = 'https://s24.q4cdn.com/216390268/files/doc_downloads/test.pdf'; // your download URL
var targetTable = 'incident'; // record table
var targetSysId = '79f8565afbb936100db5f8454eefdc40'; // record sys_id
var request = new sn_ws.RESTMessageV2();
request.setHttpMethod('GET');
request.setEndpoint(url);
// This creates sys_attachment + sys_attachment_doc on the fly
var attachmentSysId = request.saveResponseBodyAsAttachment(targetTable, targetSysId, 'downloaded_file'); // name without extension
var response = request.execute();
var httpResponseStatus = response.getStatusCode();
gs.debug("http response status_code: " + httpResponseStatus);
Output:
💡 If my response helped, please mark it as correct ✅ and close the thread 🔒— this helps future readers find the solution faster! 🙏
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Not too much to go on from the above but; to upload data from Excel with attachment links and attach them to records in ServiceNow:
Prepare Excel File: Ensure your Excel has a column with attachment URLs.
Use Import Set: Create an import set table in System Import Sets > Load Data to load the Excel data.
Script to Attach Files: After loading the data, use a Business Rule or Script Include to download the files and attach them to the correct records.
Example script:
var attachment = new GlideSysAttachment(); var recordSysId = current.sys_id; // Record to attach to var attachmentUrl = current.attachment_url; // URL from Excel // Download and attach the file var response = new GlideHTTPClient().get(attachmentUrl); if (response.getStatusCode() == 200) { attachment.write('target_table', recordSysId, response.getResponseBodyAsString(), 'application/octet-stream', 'filename'); }Run the Import: After transforming the data, run the import and the attachments will be linked.
Let me know if you need more help!
@Aaviii - Please mark as Accepted Solution and Thumbs Up if you find Helpful!!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
@Matthew_13 these files are not within the instance. These links redirect to external PDF's. I have tried converting files to "base64" format and used onAfter transform script. File is getting attached but showing blank after downloading.
Below is the script:-
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
so those links are for files within the instance?
what did you start with and where are you stuck?
💡 If my response helped, please mark it as correct ✅ and close the thread 🔒— this helps future readers find the solution faster! 🙏
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
No, those files are not within the instance. These links to redirect to external PDF's.
