Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to Download Attachment from URL?

testp21
Tera Contributor

I have done a third party integration. They are sending a URL which, I store in a URL field. Now, how can I download and attach the file in existing record? 

1 ACCEPTED SOLUTION

Amit Pandey
Kilo Sage

You can write an async/update BR and use the following script-

(function executeRule(current, previous /*null when async*/ ) {

    var endpointURL = current.u_url;
   var attachmentName = "Attachment Name" + current.number + ".mp4";

    var request = new sn_ws.RESTMessageV2();
    request.setHttpMethod('GET');
    request.setEndpoint(endpointURL);

    request.saveResponseBodyAsAttachment('Table Name', current.sys_id, attachmentName);

    var response = request.execute();

    if (response.getStatusCode() == 200) {
        // Get the response body (file content)
        var responseBody = response.getBody();
    } else {
        gs.error("Failed to retrieve file. HTTP Status Code: " + response.getStatusCode());
    }
})(current, previous);

View solution in original post

2 REPLIES 2

Amit Pandey
Kilo Sage

You can write an async/update BR and use the following script-

(function executeRule(current, previous /*null when async*/ ) {

    var endpointURL = current.u_url;
   var attachmentName = "Attachment Name" + current.number + ".mp4";

    var request = new sn_ws.RESTMessageV2();
    request.setHttpMethod('GET');
    request.setEndpoint(endpointURL);

    request.saveResponseBodyAsAttachment('Table Name', current.sys_id, attachmentName);

    var response = request.execute();

    if (response.getStatusCode() == 200) {
        // Get the response body (file content)
        var responseBody = response.getBody();
    } else {
        gs.error("Failed to retrieve file. HTTP Status Code: " + response.getStatusCode());
    }
})(current, previous);

It worked! Thanks for quick support.