Scripted REST API to upload attachment into ServiceNow

bidya1
Giga Contributor

I need to create a Scripted REST API method to upload a file as attachment into SNOW. I have checked the OOTB Attachment API, but it does not satisfy the exact need we have. We have added 2 new columns in sys_attachment table to store the category and description of the file being attached. But the attachment API has not provision to send any additional field's data.

That is why I thought of creating a Scripted API. And following is the approach I am planning to take.

1. Create a post method, which will take 2 fields as query parameters, incident number as path parameter and the file to be attached as request body

2. Then using the incident number, get the sys_id from the table and construct a RESTMessageV2() object

3. Set the OOTB attachment POST method as the REST endpoint in the message.

4. Then execute the request

But the challenge that I am facing is, how to get the file object from the request and set it in the RESTMessageV2.

Please provide some suggestions.

7 REPLIES 7

bidya1
Giga Contributor

Hi Experts,



Just found that we can get the request data in binary from the RESTAPIRequest object.


How can I convert this to base64 string ? Is there any API or method available to convert binary to base64 easily ?



Thanks,


Bidya


you can use the GlideStringUtil() to convert into base64 string. Please find the sample code below. you can try below in script include to see the result.



var sa = new GlideSysAttachment();


var binData = sa.getBytes('incident', 'd71da88ac0a801670061eabfe4b28f77'); //Change the sys_id here.


var encData = GlideStringUtil.base64Encode(binData);


gs.print(binData);


gs.print(encData);