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

antin_s
ServiceNow Employee
ServiceNow Employee

Hi,



I dont think it is a good idea to add fields to Attachment Table as it doesnt have a Form View and reporting may not add any values. I would suggest to add the fields to the business table (ex: Incident) or create 1 to 1 mapping from another table and still use OOTB Attachment API for file uploading.



Hope this helps. Mark the answer as correct/helpful based on impact.



Thanks


Antin


bidya1
Giga Contributor

Hi Antin,



Thanks for the input. But the attachment table has been configured like this from a long time and the upload attachment UI action has also been changed accordingly. So the idea is to make the REST API work in similar way as the GUI is working. Hence I need to make it work.



Thanks,


Bidya


Karthik Reddy T
Kilo Sage

Hello Panda,



I am not sure but refer the below link may helpful to you.



https://docs.servicenow.com/bundle/istanbul-servicenow-platform/page/integrate/inbound-rest/referenc...


Karthik Reddy T.
ServiceNow Commnunity MVP -2018 class.

Hello,


I need to create a Webservice API where input is: Sys ID of any record and output is: all *related* active records to that sys id. The relationships between the tables is maintained in Relationships under system definition.


Could you please guide me how to implement this? Sample code would be great.


Input: Sys ID of the record
Output: All *RELATED* active records from *VARIOUS* tables (in the following JSON format):
{
"result": [
{
"Sys ID": "5520267",
"CI Name": "Record 1",
"Table Name": "u_table_a"
},
{
"Sys ID": "5520367",
"CI Name": "Record 2",
"Table Name": "u_table_a"
},
{
"Sys ID": "8331210",
"CI Name": "Record 1",
"Table Name": "u_table_b"
},
{
"Sys ID": "8321210",
"CI Name": "Record 2",
"Table Name": "u_table_b"
},
{
"Sys ID": "3042006",
"CI Name": "Record 3",
"Table Name": "u_table_b"
},
{
"Sys ID": "4509847",
"CI Name": "Record 1",
"Table Name": ""u_table_c"
}
{
"Sys ID": "4509247",
"CI Name": "Record 2",
"Table Name": ""u_table_c"
}
]
}