What is the best way to create a Scripted REST API to attach files?

caroll
Mega Contributor

What is the best way to create a Scripted REST API to attach files?

2 REPLIES 2

Mahathi
Mega Sage
Mega Sage

Hi @caroll,

 

I suggest you use the GlideSysAttachment API.

Check out the way to do it from the following link: 

 

https://developer.servicenow.com/dev.do#!/reference/api/washingtondc/server/no-namespace/c_GlideSysA...

Reference Links from the community with detailed explanation: 
https://www.servicenow.com/community/developer-forum/how-to-create-a-scripted-rest-api-to-attachment...

Please check the document which has the example for your requirement: 

https://docs.servicenow.com/bundle/washingtondc-api-reference/page/integrate/custom-web-services/ref...

Some Youtube References:

https://www.youtube.com/watch?v=ptPlW4tQXEw


Please mark helpful and accept as solution if this helped.

Thanks,

Mahathi

Jitendra Diwak1
Kilo Sage

Hi @caroll

 

Below is example for the same

 

(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {

    var attachment = new GlideSysAttachment();

    var tableName = 'incident'; // Example table name

    var recordSysId = request.queryParams.sys_id; // Example: sys_id of the record

    var fileName = request.queryParams.fileName;

    var fileContent = request.body.data;

 

    // Create attachment record

    var attachmentSysId = attachment.write(tableName, recordSysId, fileName, fileContent);

 

    if (attachmentSysId) {

        response.setStatus(201);

        return {

            "attachment_sys_id": attachmentSysId,

            "message": "Attachment uploaded successfully"

        };

    } else {

        response.setStatus(500);

        return {

            "error": "Failed to upload attachment"

        };

    }

})(request, response);

 

Please accept my solution if it resolves your issue and thumps 👍 up

 

Thanks

Jitendra 

Please accept my solution if it works for and thumps up.