How to create the scripted rest api for requested item with attachment

keshav77
Tera Contributor

Hello everyone, 

 

Could you please help me to know how to create Scripted rest api  where I want to create  requested item through it  of particular catalog and  want to add an attachment while creating the request through the rest api  kindly share me the steps to do this?

 

Regards, 

Keshav

4 REPLIES 4

Ankur Bawiskar
Tera Patron

@keshav77 

I shared solution here few years ago, check that and enhance

Scripted rest api 

how to add file, ask 3rd party to send file name, content type, base64encoded data and then enhance it in above script only, check link below where I shared solution

Scripted Rest API to update incident with attachment 

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@keshav77 

Hope you are doing good.

Did my reply answer your question?

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

yashkamde
Kilo Sage

Hello @keshav77 ,

Use this script in your scripted rest api,

(function process(request, response) {
var body = request.body.data;
var catalogItemSysId = body.catalog_item;
var variables = body.variables || {};
var attachmentData = body.attachment;
var attachmentName = body.attachment_name;

var cart = new sn_sc.CartJS();
var cartItem = cart.addItem(catalogItemSysId, variables);

// Submit the cart
var cartSubmit = cart.submitOrder();
var requestSysId = cartSubmit.request_id;

if (attachmentData && attachmentName) {
var attachment = new GlideSysAttachment();
var decodedBytes = GlideStringUtil.base64DecodeAsBytes(attachmentData);
attachment.write("sc_request", requestSysId, attachmentName, decodedBytes);
}

response.setStatus(201);
response.setBody({
result: "success",
request_id: requestSysId,
message: "Catalog request created successfully with attachment"
});
})(request, response);

If my response helped, please mark it as correct...

Prathmeshda
Giga Guru

Hello @keshav77 

You can create a Scripted REST API in ServiceNow to submit a catalog request (RITM) for a specific catalog item and then attach a file to the created request. Below is the complete approach explained in a simple flow.

Create a Scripted REST API by navigating to System Web Services → Scripted REST APIs. Create a new API and add a POST resource (for example, /submit). This API will be used to place the catalog order.

In the resource script, use the Cart API to create the requested item. The script accepts the catalog item sys_id and variables from the request body, adds the item to the cart, sets the variables, and places the order. After placing the order, query the sc_req_item table to get the RITM sys_id and return it in the response.

(function process(request, response) {

var body = request.body.data;

var cart = new sn_sc.CartAPI();
cart.createCart();
var item = cart.addItem(body.catalog_item);

if (body.variables) {
for (var v in body.variables) {
cart.setVariable(item, v, body.variables[v]);
}
}

var requestSysId = cart.placeOrder();

var ritm = new GlideRecord('sc_req_item');
ritm.addQuery('request', requestSysId);
ritm.query();
ritm.next();

response.setStatus(201);
response.setBody({
request_sys_id: requestSysId,
ritm_sys_id: ritm.sys_id.toString()
});

})(request, response);

Attachments cannot be sent in the same JSON payload. After the RITM is created, upload the attachment using the Attachment REST API. Use the ritm_sys_id returned from the first API call.

The attachment endpoint is
POST /api/now/attachment/file

Pass the following query parameters:
table_name = sc_req_item
table_sys_id = <ritm_sys_id>
file_name = <file_name>

Send the file as multipart/form-data.

Example cURL command:

curl -X POST -u admin:password -H "Accept: application/json" -F "file=@example.pdf" "https://<instance>.service-now.com/api/now/attachment/file?table_name=sc_req_item&table_sys_id=<ritm_sys_id>&file_name=example.pdf"

Ensure the integration user has the required roles such as catalog, rest_api_explorer, and attachment. This approach is upgrade-safe and works for both UI and integration-based catalog submissionsIf this response proves useful, please mark it as Accept as Solution and Helpful. Doing so benefits both the community and me. 👍🙂