BACK

Quick Tip: Using JavaScript within a Bot

CATEGORY:

Quick Tip: Using JavaScript within a Bot

Automation Anywhere Enterprise A2019 enables developers to leverage lots of different technologies from within their bots – optical character recognition, file manipulation, and even different programming languages. One programming language that developers frequently find useful to leverage in their bots is JavaScript. Not to be confused with Java, JavaScript is a well-supported programming language that powers a good majority of the front and back end web applications that we use every day. Fortunately, that power is also available from directly within an Enterprise A2019 bot.

  1. Establish a JavaScript Session

    1. Using the Open action of the JavaScript package, bot builders can establish a session that can be called using the Run JavaScript action.
    2. The JavaScript session can be established off of a .js file that is stored in a local/network directory, stored on the Control Room, or stored in a variable.
    3. Additionally, the action supports manual JavaScript input (which is shown in the example)
    4. A best practice would be to store the JavaScript in the Control Room or Network directory as opposed to manual input. There are several reasons for this:
      1. When stored in a central repository, the JavaScript code itself could be updated without having to make modifications to the bot itself
      2. If the JavaScript is something that other bots could leverage, having it in a central location allows multiple bots to reference the same code – and again, should modifications need to be made, the changes could be made in a single place instead of needing to update the manual input for multiple bots
  2. Run JavaScript

    1. To execute any function(s) defined in the JavaScript session set up with the Open action, the Run JavaScript action is used.
    2. Provide a function name and any applicable arguments (input values)
      1. Note: The Run JavaScript action requires a list variable for its argument(s). You can use the list variable to pass multiple arguments of different data types such a boolean, date/time, number, and string.
    3. The output of the Run JavaScript action always comes in the form of a string – so make sure that:
      1. Your JavaScript functions established in the Open action all return strings
      2. You have a variable set up to receive that output from the Run JavaScript action.
    4. For Reference, the code used in the video above to calculate the difference between two times is:
      function dateDiffToString(dates) {
        //Read Dates from List
        var date0 = new Date(dates[0]);
        var date1 = new Date(dates[1]);
      
        //Subtract
        diff = Math.abs(date0 - date1);
      
        //Use mod to find times
        ms = diff % 1000;
        diff = (diff - ms) / 1000
        ss = diff % 60;
        ss = ("0" + ss).slice(-2);
        diff = (diff - ss) / 60
        mm = diff % 60;
        mm = ("0"+mm).slice(-2)
        diff = (diff - mm) / 60
        hh = diff % 24;
        hh = ("0"+hh).slice(-2)
      
        return hh + ":" + mm + ":" + ss;
      }
  3. Close

    1. While not shown in the demo video above, it’s a good practice to close your JavaScript sessions as you are done with them.
    2. Closing a session enables the re-use of the same session name for a different JavaScript file, and also helps free up memory that may have been dedicated to the scripts established with the Open JavaScript action.

Conclusion

Hopefully, you found this Quick Tip on running JavaScript from within a bot helpful. With Automation Anywhere Enterprise A2019, developers have the flexibility of leveraging several popular programming languages in addition to a host of Automation Anywhere native packages. Go build powerful bots that leverage JavaScript (among other technologies), and Go Be Great!

Related Articles
A2019
AARI
Intermediate
Video
AARI Web: Full Build Tutorial
Get hands on with AARI Web by following this end to end tutorial to set up and test the use of bots and forms brought together in an AARI process Read More
A2019
Video
What's Your Bot Type?
What "Type" of bots is your organization creating? Let's take a look at 4 common bot types and see if it might spark some creativity in the way that your team approaches the creation of new automations. Read More