How to enable RESTMessageV2 in ServiceNow New York?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-06-2019 11:12 AM
Greetings,
I apologize for asking, what seems to me, a silly question.
I have been unable to find any documentation which would explain why I am seeing what I am seeing.
I am trying to write a simple RESTMessageV2 request. I've created an outbound REST Message, I've created a client-script onLoad, I have it associated with my form. I even used the "Preview REST Message script usage" to generate the code to make the call.
However, the JavaScript console indicates: "[Log] Can't find variable: sn_ws (x_402457_choreszer_chores.do, line 505)"
I am assuming that I am missing some sort of plugin, or permission, somewhere; however, I have no idea which one.
Could someone please provide me some guidance?
Thank you very much.
Best Regards,
Casey

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-09-2019 03:44 PM
can you post your code which consumes external REST API?
You can use below sample code to consume REST API.
https://developer.servicenow.com/app.do#!/api_doc?v=madrid&id=c_RESTMessageV2API
Regards,
Sachin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-09-2019 04:07 PM
Sachin,
Thank you very much for jumping on this when it FINALLY popped into the forums.
It took the moderators 4 days to get this into the forum, between then and now, I learned that RESTMessageV2API cannot be called from the "Client Side" and must instead be all kinds of wonkily created and called using GlideAjax.
Truly deplorable design.
My findings:
Create An Application:
Main Dashboad->Creare & Deploy->System Applications->Create Application->Start From Scratch (or dealers choice).
Create a Client Script (tied to your new app) (in this case on onChange script):
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var ga = new GlideAjax("GLIDEAjaxObject");
ga.addParam('sysparm_name', 'callAPI');
ga.getXML(function(response){
var result = response.responseXML.getElementsByTagName("result");
var message = JSON.parse(result[0].getAttribute("message"));
//check for message attribute and alert user
if(message)
{
console.log(message);
g_form.clearOptions("category");
g_form.addOption("category",'','-- None --');
message["chores"].forEach(function(e,i,a){
g_form.addOption("category",e,e);
});
}
});
console.log(control.options[newValue]);
console.log("State Changed!", oldValue, newValue);
//Type appropriate comment here, and begin script below
}
Create a System Definition -> Script Include (Tied to your new app/accessible to all):
var GLIDEAjaxObject = Class.create();
GLIDEAjaxObject.prototype = Object.extendsObject(global.AbstractAjaxProcessor,{
callAPITest: function(){
var result = this.newItem("result");
result.setAttribute("message","Hello World This is The End!");
return result;
},
callAPI: function(){
try
{
var r = new sn_ws.RESTMessageV2('x_402457_choreszer.ChoresFetch', 'Default GET');
//override authentication profile
//authentication type ='basic'/ 'oauth2'
//r.setAuthenticationProfile(authentication type, profile name);
//set a MID server name if one wants to run the message on MID
//r.setMIDServer('MY_MID_SERVER');
//if the message is configured to communicate through ECC queue, either
//by setting a MID server or calling executeAsync, one needs to set skip_sensor
//to true. Otherwise, one may get an intermittent error that the response body is null
//r.setEccParameter('skip_sensor', true);
var response = r.execute();
var responseBody = JSON.parse(response.getBody());
var httpStatus = response.getStatusCode();
//console.log("Words and things!", responseBody);
//jslog("Words and things!", responseBody);
var result = this.newItem("result");
result.setAttribute("message",responseBody["body"]);
}
catch(ex)
{
var message = ex.message;
var result = this.newItem("result");
result.setAttribute("message",message);
//console.log(message);
//jslog(message,"***ERROR***");
}
},
type: "GLIDEAjaxObject"
});
--Thus, you are able to make AJAX Calls, sn_ws is NOT accessible via client scripts it would seem.
https://5kmj3av0zl.execute-api.us-west-2.amazonaws.com/prod
https://www.youtube.com/watch?v=p0UQBTrTdzI

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-09-2019 04:56 PM
try below
var GLIDEAjaxObject = Class.create();
GLIDEAjaxObject.prototype = Object.extendsObject(global.AbstractAjaxProcessor,{
callAPI: function(){
var r = new sn_ws.RESTMessageV2('x_402457_choreszer.ChoresFetch', 'Default GET');
var response = r.execute();
var responseBody = response.getBody();
var parser = new JSONParser();
var parameterArr = parser.parse(responseBody);
return responseBody;
},
type: "GLIDEAjaxObject"
});
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var ga = new GlideAjax("GLIDEAjaxObject");
ga.addParam('sysparm_name', 'callAPI');
ga.getXMLAnswer(function(answer){
var response = JSON.parse(answer);
if(response.message){
g_form.clearOptions("category");
g_form.addOption("category",'','-- None --');
for (var i = 0; i < response.message.length; i++) {
g_form.addOption('category', response.message[i].id, response.message[i].name);
}
}
});
}