“The beginning is the most important part of the work.”
— Plato
With the completion of the last piece of the registration service, the only remaining component of the set-up process is the Client instance function that utilizes the registration service provided by the Host instance. This function will actually be quite similar to the function that we just created to inform one instance about another. This time, we will be invoking the Scripted REST API instead of a stock REST API, but the process is virtually the same.
Before we make the call, however, we need to take care of few little items. First, we need to create the Service Account needed to access the instance, and then we need to grab a couple of our System Properties. We already created a function to establish the Service Account, so all we need to do is to call that function and then grab the two property values.
this.createUpdateWorker(mbrGR.getUniqueValue());
var host = gs.getProperty('x_11556_col_store.host_instance');
var token = gs.getProperty('x_11556_col_store.active_token');
Now we can build the payload from the instance GlideRecord that was passed to the function.
var payload = {};
payload.sys_id = mbrGR.getUniqueValue();
payload.name = mbrGR.getDisplayValue('name');
payload.instance = mbrGR.getDisplayValue('instance');
payload.email = mbrGR.getDisplayValue('email');
payload.description = mbrGR.getDisplayValue('description');
At this point, we can create a new instance of our old friend, the sn_ws.RESTMessageV2 object, and then populate it with all of the relevant information.
var request = new sn_ws.RESTMessageV2();
request.setHttpMethod('post');
request.setBasicAuth(this.WORKER_ROOT + host, token);
request.setRequestHeader("Accept", "application/json");
request.setEndpoint('https://' + host + '.service-now.com/api/x_11556_col_store/v1/register');
request.setRequestBody(JSON.stringify(payload));
… and as we did before, we obtain the response object by invoking the execute method.
var response = request.execute();
Now all we have to do is to populate the result object with the information contained in the response.
result.responseCode = response.getStatusCode();
result.bodyText = response.getBody();
try {
result.body = JSON.parse(response.getBody());
} catch(e) {
//
}
if (response.getErrorCode()) {
result.error = response.getErrorMessage();
result.errorCode = response.getErrorCode();
} else if (result.responseCode != '202') {
result.error = 'Invalid HTTP Response Code: ' + result.responseCode;
} else {
mbrGR.accepted = new GlideDateTime();
mbrGR.update();
}
The complete function, including the return of the result, looks like this:
registerWithHost: function(mbrGR) {
var result = {};
this.createUpdateWorker(mbrGR.getUniqueValue());
var host = gs.getProperty('x_11556_col_store.host_instance');
var token = gs.getProperty('x_11556_col_store.active_token');
var payload = {};
payload.sys_id = mbrGR.getUniqueValue();
payload.name = mbrGR.getDisplayValue('name');
payload.instance = mbrGR.getDisplayValue('instance');
payload.email = mbrGR.getDisplayValue('email');
payload.description = mbrGR.getDisplayValue('description');
var request = new sn_ws.RESTMessageV2();
request.setHttpMethod('post');
request.setBasicAuth(this.WORKER_ROOT + host, token);
request.setRequestHeader("Accept", "application/json");
request.setEndpoint('https://' + host + '.service-now.com/api/x_11556_col_store/v1/register');
request.setRequestBody(JSON.stringify(payload));
var response = request.execute();
result.responseCode = response.getStatusCode();
result.bodyText = response.getBody();
try {
result.body = JSON.parse(response.getBody());
} catch(e) {
//
}
if (response.getErrorCode()) {
result.error = response.getErrorMessage();
result.errorCode = response.getErrorCode();
} else if (result.responseCode != '202') {
result.error = 'Invalid HTTP Response Code: ' + result.responseCode;
} else {
mbrGR.accepted = new GlideDateTime();
mbrGR.update();
}
return result;
}
This final function completes the initial set-up process for our new Scoped Application. The application still doesn’t do anything in the way of sharing components between instances, but it’s a start. Next time, we will figure out where we go from here.