I know it’s late in the day, but I couldn’t pass up the opportunity to blog about International Talk Like a Pirate Day.
In this quick tutorial, we’ll create a simple scoped integration app that uses ARRPI, the Talk like a Pirate translation API.
I’ve already created a skeleton (arrrrrr, see what I did there?) scoped app where I’ll write my code, and I’ll assume you know how to do the same (if not, check out Building a ServiceNow Application on the Developer Portal).
1. Configure a REST Message
From Studio, Create New Application File > REST Message with the following details:
- Name: Talk Like a Pirate API
- Endpoint: http://isithackday.com/arrpi.php
Open the automatically generated get method:
Create two HTTP Query Parameters:
Navigate back to the REST Message, and from the Related Lists section, create a new Variable Substitution so we can test our call:
Test everything out by clicking the Test related link:
With any luck, we should see a result that looks like this:
We’re now ready to start consuming the API elsewhere in the platform.
2. Create a Business Rule
Now we’ll create a business rule to execute our piratey logic.
From Studio, Create New Application File > Business Rule with the following details:
- Name: Translate, Arrrrrrrr
- Table: Task
- Advanced: checked
- When: async
- Insert: checked
- Condition: new GlideDate().getByFormat(‘yyyy-MM-dd’).endsWith(‘09-19’)
Script:
Note: you’ll need to update lines 3 and 22 depending on the name of your application scope. Replace x_48785_tlap with your application’s scope name.
(function executeRule(current, previous /\*null when async\*/) {
B // Let's decide which fields to translate
B var fields = gs.getProperty('x\_48785\_tlap.fields\_to\_translate').split(',');
B var value;
B var translated;
B // Use some ECMA5 goodness to loop through the fields
B fields.forEach(function(field) {
B B B B B var value = current.getValue(field);
B B B B B // If the field holds a value, translate it!
B B B B B if (value) {
B B B B B B B B B translated = englishToPirate(current.getValue(field));
B B B B B B B B B current.work_notes += 'Pirate ' + current\[field\].getLabel() + ': ' + translated + '\\n';
B B B B B }
B });
B current.update();
})(current, previous);
// Encapsulate our translation logic so it's easy to reuse
function englishToPirate(text) {
B try {
B B B B B var r = new sn\_ws.RESTMessageV2('x\_48785_tlap.Talk Like a Pirate API', 'get');
B B B B B r.setStringParameter('text', text);
B B B B B var response = r.execute();
B B B B B var responseBody = response.getBody();
B B B B B var httpStatus = response.getStatusCode();
B B B B B var responseObj = JSON.parse(responseBody);
B B B B B return responseObj.translation.pirate || '';
B } catch(ex) {
B B B B B var message = ex.getMessage();
B }
}
3. Create a System Property
Finally, let’s create a system property to control which fields get translated.
From Studio, Create New Application File > System Property with the following values:
- Suffix: fields_to_translate
- Type: string
- Value: short_description,description
Let’s try it out!
Go create a new incident and give it a short description of “Hello my friend, do you know where I can find a printer around here?”. If everything worked correctly, we should see a work noteB after a few moments:
Conclusion
This may seem like a silly example, but it demonstrates a fairly common integration scenario: make a call to an external API when a record is created and do something with the result. It also shows that it really is this easy to set up an integration with an external REST API. It took me about an hour to create the app and write this blog post, and while talking like a pirate requires less care and planning than a production integration, setting up a simple call to an external API may not be as hard as as you think.
If you’d like to play with this on your own, my sample code is available in a public github repo here. Feel free to fork this and talk like a pirate to your heart’s content.
Arrrrrrr.
Share this post
Twitter
Facebook
Reddit
LinkedIn
Email