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

Varun Totakura
Kilo Explorer

Hi,

 

Check whether the user in the target instance which we are using for building "Rest Message" or creating or updating incidents have rest_service and itil roles. If not please give the roles to them so it will work as we expect with the same code that you have(Shown in the question).

 

Thanks,

Varun Totakura

Deepika Gangra1
Tera Expert

Hii Everyone, 

I want to update worknotes, comments in target instance, and make it bi-directional communication in ebonding..how can I do this.

Please Help