Quick Tip: Using JavaScript within a Bot

  • 25 January 2021
  • 0 replies
  • 851 views

Userlevel 6
Badge +9

 

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.

  • Establish a JavaScript Session

    • Using the Open action of the JavaScript package, bot builders can establish a session that can be called using the Run JavaScript action.
    • 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.
    • Additionally, the action supports manual JavaScript input (which is shown in the example)
    • 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:
      • When stored in a central repository, the JavaScript code itself could be updated without having to make modifications to the bot itself
      • 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

 

  • Run JavaScript

    • To execute any function(s) defined in the JavaScript session set up with the Open action, the Run JavaScript action is used.
    • Provide a function name and any applicable arguments (input values)
      • 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.
    • The output of the Run JavaScript action always comes in the form of a string - so make sure that:
      • Your JavaScript functions established in the Open action all return strings
      • You have a variable set up to receive that output from the Run JavaScript action.
    • 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;
      }
  • Close

    • While not shown in the demo video above, it's a good practice to close your JavaScript sessions as you are done with them.
    • 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!


0 replies

Be the first to reply!

Reply