bringing images from 3rd party applns

Sathiskumar_D
Giga Sage

Hello,

 

I am bringing 3rd party applications images to ServiceNow. THis is the code I used. I can see attachments in the table (that extends import set row).However when i try to open, it is showing the file extension is not supported to open the file

var fetchPictures = Class.create();
fetchPictures.prototype = {
    initialize: function() {
        this.REST_MESSAGE = "d131fd8e470111107c94a368436d4300";
    },
    importPictures: function(table_name) {
        var r = new sn_ws.RESTMessageV2('Pictures', 'Lists');
        var response = r.execute();
        var responseBody = response.getBody();
        var httpStatus = response.getStatusCode();
        var tableName = 'u_testing_integration';

        if (response.getStatusCode() == '200') {
            var responseObj = JSON.parse(responseBody);
            var results = responseBody.split(',');
            var payLoad = JSON.parse(results);
            for (var i = 0; i < payLoad.length; i++) {
                var intGr = new GlideRecord(tableName);
                intGr.initialize();
                intGr.u_description_of_int = "parsing response body converted" + " " + payLoad[i].id;
                intGr.u_priority = 'high';
                //                     gr.setValue('u_description_of_int', 'Testing');
                intGr.setValue('u_payload', JSON.stringify(payLoad[i]));
                intGr.u_payload_string = JSON.stringify(payLoad[i]);
                var file = JSON.parse(intGr.u_payload);
                var fileData = file.download_url;
                //                 var fileContentType = 'image/jpeg';
                intGr.setValue('u_short_description', "Author :  " + file.author);
                var recordID = intGr.insert();
				/*
                var grSysAtt = new GlideRecord('sys_attachment');
                grSysAtt.addQuery('table_name', 'tableName'); // name of table whose record have attachment
                grSysAtt.addQuery('table_sys_id', 'recordID'); // sys_id of record having attachment
                grSysAtt.query();
*/
               
                    var gsa = new GlideSysAttachment();
//                     
                    gs.info("Download Link is  " + fileData);
					gsa.write(intGr, 'Attachment.jpeg', 'image/jpeg', fileData);
                

            }


            


        }


    },


    type: 'fetchPictures'
};

.

1 ACCEPTED SOLUTION

Maik Skoddow
Tera Patron
Tera Patron

Hi

if I interpret your code correctly then the variable fileData just represents a URL to the file on the external system.

var fileData = file.download_url;

If so, you cannot expect that your code

gsa.write(intGr, 'Attachment.jpeg', 'image/jpeg', fileData);

will load the file from the external system and write it to the attachment record.

At https://gist.github.com/jnerius/d2f28b845a64fa7e1d296c79f877a8c2 you can find an example for doing it the right way.

Maik

View solution in original post

3 REPLIES 3

Maik Skoddow
Tera Patron
Tera Patron

Hi

if I interpret your code correctly then the variable fileData just represents a URL to the file on the external system.

var fileData = file.download_url;

If so, you cannot expect that your code

gsa.write(intGr, 'Attachment.jpeg', 'image/jpeg', fileData);

will load the file from the external system and write it to the attachment record.

At https://gist.github.com/jnerius/d2f28b845a64fa7e1d296c79f877a8c2 you can find an example for doing it the right way.

Maik

Hi Maik,

 

I ended up in calling rest api calls twice and got the attachments. 

var fetchPictures = Class.create();
fetchPictures.prototype = {
    initialize: function() {
        this.REST_MESSAGE = "d131fd8e470111107c94a368436d4300";
    },
    importPictures: function(table_name) {

        var tableName = 'u_testing_integration';
        var fileName = "Attachment.jpeg";
        var r = new sn_ws.RESTMessageV2('Pictures', 'Lists');
        var response = r.execute();
        var responseBody = response.getBody();
        var httpStatus = response.getStatusCode();
       
        if (response.getStatusCode() == '200') {
            var responseObj = JSON.parse(responseBody);
            var results = responseBody.split(',');
            var payLoad = JSON.parse(results);
            for (var i = 0; i < payLoad.length; i++) {
                var intGr = new GlideRecord(tableName);
                intGr.initialize();
                intGr.u_description_of_int = "parsing response body converted" + " " + payLoad[i].id;
                intGr.u_priority = 'high';
                //                     gr.setValue('u_description_of_int', 'Testing');
                intGr.setValue('u_payload', JSON.stringify(payLoad[i]));
                intGr.u_payload_string = JSON.stringify(payLoad[i]);
                var file = JSON.parse(intGr.u_payload);
                var fileData = file.download_url;
                //                 var fileContentType = 'image/jpeg';
                intGr.setValue('u_short_description', "Author :  " + file.author);
                var recordID = intGr.insert();

                var request = new sn_ws.RESTMessageV2();
                request.setHttpMethod('get');
                request.setEndpoint(fileData);
                request.saveResponseBodyAsAttachment(tableName, recordID, fileName);
                var attResponse = request.execute();
                var httpResponseStatus = attResponse.getStatusCode();
                var httpResponseContentType = attResponse.getHeader('Content-Type');
                gs.debug("http response status_code: " + httpResponseStatus);
                gs.debug("http response content-type: " + httpResponseContentType);

               
            }





        }


    },


    type: 'fetchPictures'
};

Hi

nice to see, that your issue could be solved. But as I pointed you to the right solution I'm wondering why you  don't honor my answer.

Kind regards
Maik