We're reclaiming inactive PDIs to keep them available for active builders. Learn what's changing, who's affected, and how to protect your work. Read More

Uploading attachment through transform Map

Aaviii
Tera Contributor

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.

1 ACCEPTED SOLUTION

@Aaviii 

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:

download and attach file from external system to target record.gif

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

View solution in original post

12 REPLIES 12

Matthew_13
Mega Sage

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:

  1. Prepare Excel File: Ensure your Excel has a column with attachment URLs.

  2. Use Import Set: Create an import set table in System Import Sets > Load Data to load the Excel data.

  3. 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');
    }
  4. 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!!

MJG

@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:-

 

    var fileName = source.u_file_name;
    var fileContentType = source.u_content_type;
    var fileData = source.u_base64_data;

    var sa = new GlideSysAttachment();
    sa.write(target, fileName, fileContentType, fileData);

Ankur Bawiskar
Tera Patron

@Aaviii 

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! 🙏

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

No, those files are not within the instance. These links to redirect to external PDF's.