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  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

12 REPLIES 12

@Aaviii 

so when you hit those external links the file gets auto downloaded?

any authentication required to access those links?

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

@Ankur Bawiskar No, no authentication required. It directly redirects to file which can be downloaded.

@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  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Aaviii 

Hope you are doing good.

Did my reply answer your question?

💡 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  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Ankur Bawiskar I have tried below script and it's not working. My configuration is for scoped app

 

    var url = source.u_url;
    var targetTable = 'sn_compliance_control';
    var targetSysId = target.sys_id;

    var request = new sn_ws.RESTMessageV2();
    request.setHttpMethod('GET');
    request.setEndpoint(url);
    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);