“The best way to predict your future is to create it.”
— Abraham Lincoln
Now that we have built a REST end point that will run on a Host instance, we need to complete the function referenced in our widget to utilize the API from our potential client instance. Like many things on the Now Platform, there are a number of ways that this could be accomplished. One way would be to create a new Outbound REST Service using the input form for that purpose.
That will definitely work, but for our simple unauthenticated GET info service, that really is more work than is necessary. Using a new RESTMessageV2 object, you can essentially build the same thing from a blank slate, and of the two properties that we have to value, one of them (the URL or Endpoint) would have to be overwritten anyway, since the instance name portion of the URL is provided by the user’s input. The entire set-up is three simple lines of code:
var request = new sn_ws.RESTMessageV2();
request.setHttpMethod('get');
request.setEndpoint('https://' + host + '.service-now.com/api/x_11556_col_store/v1/info');
Once you have the completed the set-up, the response object is obtained by calling the execute method:
var response = request.execute();
Because we just built the service that we will be calling, we already know what responses could be coming from the service itself; however, there may be other responses as well if the service is not installed on the instance or if the instance name provided is not a valid instance or if it is a PDI that is currently asleep. Our function should be equipped to handle all of that, so there are several things that we will need to check.
One of the methods on the RESTResponseV2 object is haveError(), which we need to check before anything else. If there some kind of error with the process, then there is no need to check the HTTP Response Code, since we never made it that far. At the very top of our new getStoreInfo() method, I created an empty object for holding all of the information that we will return to the caller:
var respObj = {};
In the event that the haveError() method returns true, we then populate that object with the error details:
if (response.haveError()) {
respObj.error = response.getErrorMessage();
respObj.errorCode = response.getErrorCode();
respObj.body = response.getBody();
}
If there was no error, then the next thing that we want to check is the HTTP Response Code using the response object’s getStatusCode() method. If the code is 200, then we have a valid response. If not, then we are not talking to an active Collaboration Store Host, and we need to populate our response object with that information:
respObj.error = 'Invalid HTTP Response Code: ' + respObj.responseCode;
respObj.body = response.getBody();
If we do receive a 200, though, then we need to parse the JSON string and return all of the information about the Host so that it can be processed by the widget:
respObj.storeInfo = JSON.parse(response.getBody());
respObj.name = respObj.storeInfo.result.info.name;
Putting it all together, the entire function looks like this:
getStoreInfo: function(host) {
var respObj = {};
var request = new sn_ws.RESTMessageV2();
request.setHttpMethod('get');
request.setEndpoint('https://' + host + '.service-now.com/api/x_11556_col_store/v1/info');
var response = request.execute();
respObj.responseCode = response.getStatusCode();
if (response.haveError()) {
respObj.error = response.getErrorMessage();
respObj.errorCode = response.getErrorCode();
respObj.body = response.getBody();
} else if (respObj.responseCode == '200') {
respObj.storeInfo = JSON.parse(response.getBody());
respObj.name = respObj.storeInfo.result.info.name;
gs.setProperty('x_11556_col_store.active_token', respObj.storeInfo.result.info.sys_id);
} else {
respObj.error = 'Invalid HTTP Response Code: ' + respObj.responseCode;
respObj.body = response.getBody();
}
return respObj;
}
That completes all of the work for the first Script Include function that we referenced in the server side code of the widget. Next, we’ll get started on that second one that sends out the email verification code to the email address entered on the form.