Upload a picture to db_image by REST-API

sh83le
Tera Contributor

Dear all,

 

I created an action with one input variable:
URL = https://de.wikipedia.org/static/images/project-logos/dewiki.png

 

I created an output variable:
MyImage = (type) Image

 

 

(function execute(inputs, outputs) {
    var imageurl = inputs.URL;
    var httpClient = new GlideHTTPRequest(imageurl);
    httpClient.get();

    var httpStatusCode = httpClient.getStatusCode();
    
    if (httpStatusCode === 200) {
        var responseBody = httpClient.getResponseBody();
        var base64Image = GlideStringUtil.base64Encode(responseBody);
        outputs.image2 = base64Image;
    } else {
        gs.error('Fehler beim Herunterladen des Bildes. HTTP-Statuscode: ' + httpStatusCode);
    }
})(inputs, outputs);

 

The action will be startet in a Flow.

Action: Create Record
Table: db_image
image > Result from action (image2)
name > hardcoded at this moment

 

I'm not able to get a image to this database.
I need this for parsing some external content to integrate in the internal KnowledgeBase.

So my question > is it a simple stupid error ?
Is an other workarround possible. It should be a script not manuell upload and also not storing the images outside of servicenow.

 

sh83le_1-1687253019776.png

 

 

 

sh83le_0-1687253003226.png

 

1 ACCEPTED SOLUTION

Tony Chatfield1
Kilo Patron

Hi, I think that you will find the db_image record references a sys_attachment record, and the actual attachment is stored in sys_attachment (actually stored in chunks in sys_attachment_doc).
So I would suspect you need to upload the file to sys_attachment first and then create a db_image record pointing to sys_attachment.

 

Example from a PDI

/nav_to.do?uri=db_image.do?sys_id=687fa131eb1301003eadeb29a206fe05

/sys_attachment_list.do?sysparm_query=table_sys_id%3D687fa131eb1301003eadeb29a206fe05&sysparm_view=

View solution in original post

2 REPLIES 2

Tony Chatfield1
Kilo Patron

Hi, I think that you will find the db_image record references a sys_attachment record, and the actual attachment is stored in sys_attachment (actually stored in chunks in sys_attachment_doc).
So I would suspect you need to upload the file to sys_attachment first and then create a db_image record pointing to sys_attachment.

 

Example from a PDI

/nav_to.do?uri=db_image.do?sys_id=687fa131eb1301003eadeb29a206fe05

/sys_attachment_list.do?sysparm_query=table_sys_id%3D687fa131eb1301003eadeb29a206fe05&sysparm_view=

Thanks for your answer, it looks like I'm coming out of a dead end with the info. I let myself be blinded instead of checking the reference via the Table API.