Oracle Policy Automation – JavaScript Custom Search

As many of you will no doubt  have discovered, the recent version of Oracle Policy Automation now comes equipped with a complete JavaScript Extension API. Simply put, we can create custom Labels, Input Controls and so on using JavaScript objects. Our code, which is part of the Project, is detected at runtime and executed by the JavaScript engine in your browser. In this post we will look at JavaScript Custom Search and how to use it in your own projects.

One of the most fun parts of this new API is of course discovering ways to use it. Following on from the post a couple of weeks ago, I thought I would share an example of using the customSearch. The principle behind using JavaScript Custom Search in Oracle Policy Automation is very easy to understand. Most of us are familiar, for example, with the concept of typing in a little bit of text and then the browser completes it, providing a drop down of potential choices. There are a myriad of ways and places to see it on the Internet. Perhaps you are searching for a street, so you type in the postal code and sure enough, your website might propose the list of streets in that area.

Whatever your reason for using it, here is an example you can play along with. Since we need a data provider, we are going to use JSONPlaceholder – a wonderful fake REST API that you can use for testing just this sort of thing. It is free and easy to use. We can call various different endpoints and the server will report back with fake data. We can use all the verbs and many different sorts of REST call. Great!

What else do we need?otn We need a JavaScript editor (step forward Notepad ++ for example) and the August 2017 edition of Oracle Policy Modeling. Before we look at the code however, remember the following caveat : anything you read here is for entertainment and education purposes only and should not be relied upon for any reason or situation.

Let’s get a big picture first. You are going to be coding three things

  1. The Search
  2. The Commit, which you can think of as “any post search processing”
  3. You will be building a set of records to be passed to the callback function

JavaScript Custom Search

The search itself, as I mentioned before we will use the JSONPlaceholder service. I have selected the users endpoint, so that I can fire off a query like “‘/users?q='” and pass the text string entered by my user. Hopefully the service will return some people.

JavaScript Custom Search Step by Step

The processing and preparation will involve, purely for the purpose of demonstrating the principles, laboriously removing any elements of the data I am not interested in. In fact I am going to delete everything except the name and the email address. The callback function expects a set of data that respects a specific format: “This can either be an array of strings or an array of objects where each object has a text property.” So now you know why I wanted to demonstrate the following

  1. Renaming a property in the object
  2. Using the other property, email, in the Commit.

Here is the first part, basically I am :

  1. getting the text from the user and then passing it to the REST endpoint in the form of a GET.
  2. The response is coming back at me as a bunch of objects, which I am going to loop through and trim by removing unnecessary stuff.
  3. Then I am going to convert it into an array of results for our JavaScript Custom Search.
  4. Then I call the callback function with the array passed in.

JavaScript Custom Search Script

The user can now select one of the values I have returned. When they select their chosen value, the commit will handle it:

JavaScript Custom Search Commit

In the picture above you can see the value which is passed in actually included the email element from the REST call, so I am free to use it (and any of the other bits I didn’t delete in the previous steps) to populate other attributes, such as the email address.

To help you make sense of all of that, you will find below a video which guides you through the steps and shows the different elements.

Have a nice day and I hope you enjoyed reading an example of JavaScript Custom Search in Oracle Policy Automation.

JavaScript Custom Search Video