Add comments to (incident) record via Scripted REST API

Yonka Yonkova1
Tera Contributor

Hi all, 

How can I add comments to a record, for example in the 'Incident' table, via a Scripted REST API? 

What is the right method? 

I tried PATCH and PUT and they are working for every other field. 

I read that the 'comments' field is special as it relates to another table but I still cannot find a way to post a comment. 

Thank you in advance for your help. 

 

1 ACCEPTED SOLUTION

In REST API Explorer you can create examples of REST Messages.
Here's an example where I create a PUT message that updates the specified incident with Additional comments "Hello".
Weird_0-1688453593850.pngWeird_1-1688453658798.png

Weird_2-1688453692226.png

var request = new sn_ws.RESTMessageV2();
request.setEndpoint('https://your_instance.service-now.com/api/now/table/incident/your_sys_id');
request.setHttpMethod('PUT');

//Eg. UserName="admin", Password="admin" for this code sample.
var user = 'admin';
var password = 'admin';

request.setBasicAuth(user,password);
request.setRequestHeader("Accept","application/json");
request.setRequestHeader('Content-Type','application/json');
request.setRequestBody("{\"comments\":\"Hello\"}");
var response = request.execute();


As you can see the body is set to "{\"comments\":\"Hello\"}"

There are many ways to write the REST message, but technically if you've been able to update any other fields, then comments should work just as well.

View solution in original post

3 REPLIES 3

Weird
Mega Sage

There shouldn't be anything special about it when updating.
Yes, comments and work_notes are stored in a separate table and work in a special way, but you should be able to update them with a regular put message.
How do you build your message? How does it look like?
I made this as an example with the built in REST Api Explorer with PUT method and it updated an incident without issues.

request.setRequestBody("{\"comments\":\"Hello\"}");

 

yonkata
Tera Contributor

Hi, 

Thank you for your reply. 

Where is this defined exactly? Could you please provide a screenshot? 

Thank you in advance. 

 

In REST API Explorer you can create examples of REST Messages.
Here's an example where I create a PUT message that updates the specified incident with Additional comments "Hello".
Weird_0-1688453593850.pngWeird_1-1688453658798.png

Weird_2-1688453692226.png

var request = new sn_ws.RESTMessageV2();
request.setEndpoint('https://your_instance.service-now.com/api/now/table/incident/your_sys_id');
request.setHttpMethod('PUT');

//Eg. UserName="admin", Password="admin" for this code sample.
var user = 'admin';
var password = 'admin';

request.setBasicAuth(user,password);
request.setRequestHeader("Accept","application/json");
request.setRequestHeader('Content-Type','application/json');
request.setRequestBody("{\"comments\":\"Hello\"}");
var response = request.execute();


As you can see the body is set to "{\"comments\":\"Hello\"}"

There are many ways to write the REST message, but technically if you've been able to update any other fields, then comments should work just as well.