How to call REST Massage in Script-Background of ServiceNow

haruna naka
Tera Contributor

I made JSON on OpenWeatherMap like below and I tried to call REST Massage in Script-Background of ServiceNow by entering JSON's script below and clicking "Run Script".However I couldn't call REST Massage.

If someone knows solution, please let me know.Thanks.

[JSON]   

try {
var r = new sn_ws.RESTMessageV2('application name, 'title');
r.setStringParameterNoEscape('timezoon', '322440');
var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
}
catch(ex) {
var message = ex.message;
}

 

1 ACCEPTED SOLUTION

Marc Mouries
ServiceNow Employee
ServiceNow Employee

you need to set the end point. 

 

(function execute() {
    var restMessage = new sn_ws.RESTMessageV2();
    restMessage.setEndpoint('https://api.openweathermap.org/data/2.5/weather?zip='+ YOUR_ZIP_CODE +'&appid='+ YOUR_KEY);
    restMessage.setHttpMethod('get');
    var response = restMessage.execute();
    var httpResponseStatus = response.getStatusCode();

    if (httpResponseStatus == 200) {
       var responseBody = response.getBody();
       var responseObject = JSON.parse(responseBody);

    // do what you need to do with the response

    return body;
}
})()

View solution in original post

7 REPLIES 7

Marc Mouries
ServiceNow Employee
ServiceNow Employee

you need to set the end point. 

 

(function execute() {
    var restMessage = new sn_ws.RESTMessageV2();
    restMessage.setEndpoint('https://api.openweathermap.org/data/2.5/weather?zip='+ YOUR_ZIP_CODE +'&appid='+ YOUR_KEY);
    restMessage.setHttpMethod('get');
    var response = restMessage.execute();
    var httpResponseStatus = response.getStatusCode();

    if (httpResponseStatus == 200) {
       var responseBody = response.getBody();
       var responseObject = JSON.parse(responseBody);

    // do what you need to do with the response

    return body;
}
})()

Thank you for your help!

 

@haruna naka  As a community guideline, please mark the answer as the accepted answer.

Hitoshi Ozawa
Giga Sage
Giga Sage

Hi Haruna,

Is the url working from REST Message? Click on the "Test" link to test the url.

find_real_file.png

Following code probably won't work. Should this be "timezone"? If so, should the value be "Asia/Tokyo"? Or is the parameter suppose to be "timezone_offset"?

r.setStringParameterNoEscape('timezoon', '322440');