“Change is easy. Improvement is far more difficult.”
— Ferdinand Porsche
Now that we have a Business Rule to launch our Subflow, it’s time to get busy and actually create the Subflow. To create a new Subflow, open up the Flow Designer, click on the New button and select Subflow from drop-down menu. I initially called mine Webhook Poster, but the Business Rule couldn’t find it to launch it for some reason. I finally got it to work when I replaced the space with an underscore, so now the name is Webhook_Poster. I’m not really sure why I had to do that, but it works, so we will leave it at that.
With the Subflow named, the next thing to do is to define all of the inputs. In my mind, there were only two, the current object and the previous object. Unfortunately, the Flow Designer will not recognize any of the properties of those objects unless you explicitly define them, so I had to specify every element of both objects. That seemed like a rather tedious waste of time, but eventually I got through it and now it’s done.
That takes care of the set-up, so now it’s on to the flow itself. The first thing that we are going to want to do is to gather up all of the registration records that would be triggered by this Incident. Since we gave our users several options for selecting records, we will have to test them all. Essentially, we will need a query filter for each possible Type, which we can then OR together to create one huge query filter. Here is how that looks in practice:
As a second step, I threw in an IF statement to see if the query returned any records. We are basically done at this point if there are no records, but if we do have records to process, then we will need to build the payload that we will be posting to all of the target URLs. For that job, I needed to create a new Flow Designer Action, so I saved my Subflow and closed the Subflow tab for now.
The payload is basically a generic JSON object containing the relevant information about the the things that have changed about the record since the last time that it was saved. Pulling that all together will basically be a lot of tedious scripting, so that sounds like a job for yet another function in our Script Include. Here is what I came up with:
buildPayload: function(current, previous) {
var payload = {};
payload.id = current.number;
payload.short_description = current.short_description;
payload.message = '';
var separator = '';
if (current.state != previous.state) {
payload.state = current.state;
if (previous.state) {
payload.message += separator + 'State changed from ' + previous.state + ' to ' + current.state + ' on ' + payload.id + '.';
} else {
payload.message += separator + 'State set to ' + current.state + ' on ' + payload.id + '.';
}
separator = '\n\n';
}
if (current.assignment_group != previous.assignment_group) {
payload.assignment_group = current.assignment_group;
if (previous.assignment_group) {
payload.message += separator + 'Assignment Group changed from ' + previous.assignment_group + ' to ' + current.assignment_group + ' on ' + payload.id + '.';
} else {
payload.message += separator + 'Assignment Group set to ' + current.assignment_group + ' on ' + payload.id + '.';
}
separator = '\n\n';
}
if (current.assigned_to != previous.assigned_to) {
payload.assigned_to = current.assigned_to;
if (previous.assigned_to) {
payload.message += separator + 'Assigned To changed from ' + previous.assigned_to + ' to ' + current.assigned_to + ' on ' + payload.id + '.';
} else {
payload.message += separator + 'Assigned To set to ' + current.assigned_to + ' on ' + payload.id + '.';
}
separator = '\n\n';
}
if (current.comments) {
payload.comments = current.comments;
payload.message += separator + current.operator + ' has added a new comment to ' + payload.id + ':\n' + current.comments;
separator = '\n\n';
}
if (current.work_notes) {
payload.work_notes = current.work_notes;
payload.message += separator + current.operator + ' has added a new work note to ' + payload.id + ':\n' + current.work_notes;
separator = '\n\n';
}
return JSON.stringify(payload, null, '\t');
},
We still need a Flow Designer Action to call this function, but the function does almost all of the heavy lifting and our Action should now be quite simple to put together. Let’s open up the Flow Designer again, click on the New button, and select Action from the drop-down selection list. We’ll call our new Action Build Webhook Payload since that’s what it does, and we will define two inputs, the current and previous objects. Since we won’t be referencing any of the properties of those objects directly, this time we won’t have to invest the time in specifying all of the elements of the objects.
All we will need for our Action is to add a Script step with the same two inputs and the payload as an output. The script itself is just a call to the function that we just added to our Script Include.
(function execute(inputs, outputs) {
var wru = new WebhookRegistryUtils();
outputs.payload = wru.buildPayload(inputs.current, inputs.previous);
})(inputs, outputs);
We also need to define the payload as an output of Action itself, and map the Action inputs to the Script step inputs and the Script step output to the Action output. That should complete our new Action.
Now we can go back into our Subflow and select this Action as a third step under our second step conditional that checks to see if there are any records from the query in the first step. For our fourth step, we will add a flow logic step that loops through all of the records from step 1, and inside of that loop, our fifth step will make the POST of the payload. I looked for an out-of-the-box simple HTTP POST Action already included on the platform, but I couldn’t find anything, so that’s another Action that we are going to have to produce ourselves. We already built much of the script for that when we built our testURL function; if we rework that code just a little bit, we can probably find a way to make it work just fine for both purposes.
testURL: function(whrGR) {
var jsonObj = {message: 'This is a test posting.'};
return this.postWebhook(whrGR, JSON.stringify(jsonObj, null, 4));
},
postWebhook: function(whrGR, payload) {
var result = {};
var request = new sn_ws.RESTMessageV2();
request.setEndpoint(whrGR.getValue('url'));
request.setHttpMethod('POST');
request.setRequestHeader('Content-Type', 'application/json');
request.setRequestHeader('Accept', 'application/json');
request.setRequestBody(payload);
var response = request.execute();
result.status = response.getStatusCode();
result.body = response.getBody();
if (result.body) {
try {
result.obj = JSON.parse(result.body);
} catch (e) {
result.parse_error = e.toString();
}
}
result.error = response.haveError();
result.error_code = response.getErrorCode();
result.error_message = response.getErrorMessage();
return result;
},
Now the testURL function is just a call to our new postWebhook function that contains most of the code previously contained in the testURL function. The testURL function will pass in our simple test payload, and when we create out new Action, we can call the very same postWebhook function, passing in a real payload. The script for our new Action will then simply be this:
(function execute(inputs, outputs) {
var wru = new WebhookRegistryUtils();
var result = wru.postWebhook(inputs.webhook_registry, inputs.payload);
for (var key in result) {
outputs[key] = result[key];
}
})(inputs, outputs);
Stopping right here should give us enough with which to test out the process so far. This isn’t all that we want to do here, because we still want to record both the attempt and the response in our log table. However, since we haven enough built out to just test the POSTing of payloads, I think we should stop and do that. Afterwards, we can circle back and build out the activity logging and any Event logging that we might want to do for any kind of bad responses. But right now, we have enough built out that we can create a few Webhook Registrations and then go update some qualifying Incidents to see what actually happens.
Setting all of that up and verifying the results seems worthy of its own episode, so let’s wrap things up for today and pick that up next time out.