Oracle Policy Automation – JavaScript Custom Search

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

Update (December 2017)

I forgot to mention that if your input control is displaying an attribute that is not of type text, this code will not run. It works (perhaps self-evidently, but for completeness I reiterate it) only on text attributes.

Richard Napier

Author: Richard Napier

Richard Napier joined Siebel Systems in 1999 and took up the role of managing the nascent Siebel University in Southern Europe. He subsequently was Director of Business Development and Education for InFact Group (now part of Business & Decisions) for 8 years. He now runs his Consulting and mentoring company, On Demand Consulting & Education Ltd and has run ODCE since 2010. Owner of intelligent-advisor.com, he also is Co-Founder of the Siebel Hub.

12 thoughts on “Oracle Policy Automation – JavaScript Custom Search

  1. Hi Richard – I found your insightful OPA – Javascript Custom Search post today. I have attempted to recreate the javascript file you shared online and in the video. I think I mostly have it. However, when I run the OPM debugger (running the latest version of Policy Modeler 12.2.9), I get an error. Would you be willing to share your .js file? Thanks!

    1. Hey Christian thanks for stopping by. If you reach out and connect to to me on linkedin I will send you a link to my file tomorrow evening my time (Switzerland). Travelling tomorrow during the day but will make every effort to get it to you tomorrow. Thanks, Richard.

  2. Hi Richard, I have seen your example on this custom search extension and was following along with you. I have completed the build and the JS however I am receiving a “$” undefined error whilst searching, which i think is related to jQuery library not being loaded prior. How did you go about solving this issue?

    Thanks,
    Krestin

  3. Hi Richard,
    Your post on Custom Search indeed has been very helpful and nicely explained.
    i have executed it in OPA debugger and it’s working good,(performing search and updating values based on response).

    However when i executed same for integrated OPA with RightNow, it didn’t work. Could you please advise what i am missing?
    Thanks.

    1. Hey There. Well, it’s kind of hard to debug from a distance 🙂 but I would look into the following areas
      a) Are you embedding this in an Answer for example, on the Customer Portal as an IFRAME? There might be potential issues with cross-site scripting if you are attempting to manipulate the iframe data from outside

      When you run the interview in Oracle Service Cloud, is the console displaying any messages? Is the script being downloaded correctly and can you see it in the Browser console? If this is a publically accessible URL, feel free to reach out to me on LinkedIn and I can take a look. Cheers Richard

      1. Hey Richard,
        My apologies for late response.
        First of all , i sincerely want to thank you for your comments .This indeed was helpful.

        I am using it as inline in CP. I would look into cross-site scripting issue.As you mentioned,this might be a cause.
        Once again would like to thank you for your inputs.
        Regards
        Nootan

  4. Hi Richard,
    Really wonderful article and thank you in advance for publishing.
    I have a requirement like this. My API returns a value as an object which I need to place in to a field as a drop down list.
    How can I achieve this?

    Thank you,
    Nirmani

    1. Hi Nirmani. Thanks for your comments. If I understand you correctly, your API call returns a JS object, and you need to parse the object and put the results in the dropdown? Sounds to me like you are talking about a Custom Options Extension – you want the user to see a Dropdown, and pick an item from it? So you need to for…each over the object and put the values (and a text label) into the array used by the Drop Down? If you search the Shop there are 2 or 3 examples of Custom Options. If that doesn’t clarify it, or if I have misunderstood, please get in touch again and we can look at it.

      1. Hi Richard,

        Really appreciate your quick response.

        Yes, based in the entered value API will return object like below.
        data[
        {
        “id”: 1,
        “name”: “Leanne Graham”,
        “username”: “Bret”,
        “email”: “Sincere@april.biz”,
        “modes”: [
        {
        “cmode”:”extenal”,
        “sp”: [
        {
        “termcode”:”1268″;
        “term”:”abc”;
        }
        ]
        {
        “cmode”:”internal”,
        “sp”: [
        {
        “termcode”:”1278″;
        “term”:”abd”;
        }
        ]
        }] } }]
        What I need is take “cmode” values to one drop down and related “sp” s to an another child drop down.

        Hope my it clear. Thank you again for the response.

        Thank you,
        Nirmani

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Worldwide
Logo by Southpaw Projects LLC