In ServiceNow Ebonding how to update worknotes and state dynamically in incident

Sourabh Saha1
Tera Contributor

I am able to create an incident in one instance from another instance in ServiceNow.

The issue is with dynamic update of STATE and WORKNOTES. I have create a scripted REST API in the target instance and when I use REST API explorer to update incident, it works fine. If I copy the corresponding ServiceNow script and paste it in the source instance 'after' business rule, it works. Issue occurs when I try to post STATE and WORKNOTES dynamically. 

please see my code below for 'after' on 'UPDATE' 'business rule:

(function executeRule(current, previous /*null when async*/ ) {

var request = new sn_ws.RESTMessageV2();
request.setEndpoint('https://<instanceName>.service-now.com/api/196943/api_with_script_include/INC0010047');
request.setHttpMethod('PATCH');

var state = current.state;
var note = current.work_notes.getJournalEntry(1);
gs.log("<<vote>>",note);
gs.log("<<state>>",state);

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

request.setBasicAuth(user,password);
request.setRequestHeader("Accept","application/json");
request.setRequestHeader('Content-Type','application/json');
request.setRequestBody("{\"state\":\""+state+"\""+",\"work_notes\":\""+note+"\"}"); - the ISSUE happens with this line when I do not hardcode it and try to fetch value from source instance
var response = request.execute();
gs.log(response.getBody());

})(current, previous);

The error that I get in my source instance:

{"error":{"message":"java.lang.IllegalArgumentException: Cannot decode: java.io.StringReader@f7747c","detail":""},"status":"failure"}

 

Request for help 

1 ACCEPTED SOLUTION

Kunal Varkhede
Tera Guru

Hi,

 

If you are using after business rule then for work note you need to glide sys_journal_field table, get the recent work note.Why we need this because of when work note is inserted and form is updated so it is stored in html format in sys_journal_field table, so thats why we can not access in after business rule.  We can access current work note using before business rule before form submission but as per your requirement you are using After business rule so that you need to glide the sys_journal_field table and access the current work note.  

 

Updated code:

After Business Rule

(function executeRule(current, previous /*null when async*/ ) {

var gr=new GlideRecord('sys_journal_field');
gr.addQuery('element', 'work_notes');
gr.addQuery('element_id', current.sys_id);
gr.orderByDesc('sys_created_on');
gr.setLimit(1);
gr.query();
while (gr.next()) {
note = gr.value.toString();
}


var request = new sn_ws.RESTMessageV2();
request.setEndpoint('https://<instanceName>.service-now.com/api/196943/api_with_script_include/INC0010047');
request.setHttpMethod('PATCH');

var state = current.state;

//var note = current.work_notes.getJournalEntry(1);//comment this line

gs.log("<<vote>>",note);
gs.log("<<state>>",state);

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

request.setBasicAuth(user,password);
request.setRequestHeader("Accept","application/json");
request.setRequestHeader('Content-Type','application/json');
request.setRequestBody("{\"state\":\""+state+"\""+",\"work_notes\":\""+note+"\"}");

 

- the ISSUE happens with this line when I do not hardcode it and try to fetch value from source instance

 


var response = request.execute();
gs.log(response.getBody());

})(current, previous);

 

 

Please Mark Correct/Helpful answer if it help you in any way.

Thanks,

Kunal

View solution in original post

6 REPLIES 6

Tony Chatfield1
Kilo Patron

Hi, perhaps easier if you encapsulate with double quotes "" and use singles '' within the line so you don't have to \escape everywhere? Maybe try something like this (untested).
("{'state: ' + state + ' work_notes : ' + note}");

Kunal Varkhede
Tera Guru

Hi,

 

If you are using after business rule then for work note you need to glide sys_journal_field table, get the recent work note.Why we need this because of when work note is inserted and form is updated so it is stored in html format in sys_journal_field table, so thats why we can not access in after business rule.  We can access current work note using before business rule before form submission but as per your requirement you are using After business rule so that you need to glide the sys_journal_field table and access the current work note.  

 

Updated code:

After Business Rule

(function executeRule(current, previous /*null when async*/ ) {

var gr=new GlideRecord('sys_journal_field');
gr.addQuery('element', 'work_notes');
gr.addQuery('element_id', current.sys_id);
gr.orderByDesc('sys_created_on');
gr.setLimit(1);
gr.query();
while (gr.next()) {
note = gr.value.toString();
}


var request = new sn_ws.RESTMessageV2();
request.setEndpoint('https://<instanceName>.service-now.com/api/196943/api_with_script_include/INC0010047');
request.setHttpMethod('PATCH');

var state = current.state;

//var note = current.work_notes.getJournalEntry(1);//comment this line

gs.log("<<vote>>",note);
gs.log("<<state>>",state);

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

request.setBasicAuth(user,password);
request.setRequestHeader("Accept","application/json");
request.setRequestHeader('Content-Type','application/json');
request.setRequestBody("{\"state\":\""+state+"\""+",\"work_notes\":\""+note+"\"}");

 

- the ISSUE happens with this line when I do not hardcode it and try to fetch value from source instance

 


var response = request.execute();
gs.log(response.getBody());

})(current, previous);

 

 

Please Mark Correct/Helpful answer if it help you in any way.

Thanks,

Kunal

Sourabh Saha1
Tera Contributor

I have another query regarding selecting an incident dynamically. 

In the above question endpoint is hardcoded:

request.setEndpoint('https://<instanceName>.service-now.com/api/196943/api_with_script_include/INC0010047');

I would like to change the incident number dynamically. I have a correlation id where I have populated the incident number which has been created in another instance but when I use the field value like

request.setEndpoint('https://<instanceName>.service-now.com/api/196943/api_with_script_include/field_name');

it does not update the incident. In the log it shows the endpoint as undefined.

please find the code below

(function executeRule(current, previous /*null when async*/ ) {

var coid = current.correlation_display;
gs.log('correlation display='+coid);

var gr=new GlideRecord('sys_journal_field');
gr.addQuery('element', 'work_notes');
gr.addQuery('element_id', current.sys_id);
gr.orderByDesc('sys_created_on');
gr.setLimit(1);
gr.query();
while (gr.next()) {
note = gr.value.toString();
//gs.log('<<current worknote: >>',note);
}


var request = new sn_ws.RESTMessageV2();
var endpoint = request.setEndpoint('https://<instance_name>/api/196943/api_with_script_include/coid');
gs.log('endpoint is'+endpoint);
request.setHttpMethod('PATCH');

var state = current.state;

//var note = current.work_notes.getJournalEntry(1);//comment this line

gs.log("<<vote>>",note);
gs.log("<<state>>",state);

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

request.setBasicAuth(user,password);
request.setRequestHeader("Accept","application/json");
request.setRequestHeader('Content-Type','application/json');
request.setRequestBody("{\"state\":\""+state+"\""+",\"work_notes\":\""+note+"\"}");

var response = request.execute();
gs.log(response.getBody());

})(current, previous);

 

Please help

Hi,

 

Updated code-

 

(function executeRule(current, previous /*null when async*/ ) {

var gr=new GlideRecord('sys_journal_field');
gr.addQuery('element', 'work_notes');
gr.addQuery('element_id', current.sys_id);
gr.orderByDesc('sys_created_on');
gr.setLimit(1);
gr.query();
while (gr.next()) {
note = gr.value.toString();
}


//var request = new sn_ws.RESTMessageV2();
request.setEndpoint('https://<instanceName>.service-now.com/api/196943/api_with_script_include/INC0010047'); //comment this line and try below line

 

 

var sys_id_of_inc = current.sys_id;

request.setEndpoint('https://<instanceName>.service-now.com/api/196943/api_with_script_include/' +sys_id_of_inc);

 

 


request.setHttpMethod('PATCH');

var state = current.state;

//var note = current.work_notes.getJournalEntry(1);//comment this line

gs.log("<<vote>>",note);
gs.log("<<state>>",state);

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

request.setBasicAuth(user,password);
request.setRequestHeader("Accept","application/json");
request.setRequestHeader('Content-Type','application/json');
request.setRequestBody("{\"state\":\""+state+"\""+",\"work_notes\":\""+note+"\"}");

 

var response = request.execute();
gs.log(response.getBody());

})(current, previous);

 

Thanks,

Kunal