To prototype a script, you need to create a script, and a test case. This tutorial demonstrates prototyping a script that logs the application startup time to a text file in the HL7Connect log directory.

  1. Open the HL7Connect Development Environment

  2. Create a new blank Javascript script (File..New..Javascript), and type the following script:

       function test(){
       }
      
    Note: Case is important with Javascript

  3. Save the script to c:\temp\test.js

  4. Create a new Test Case (File|New|Test Case File), and choose OnKernelStart

    Note: The choice of OnKernelStart is somewhat arbitrary at this point, however it is good practice to use the right event type

  5. In the test case, choose test.js for the script, and type "test" in procedure

  6. From the tools menu, choose Debug Log

  7. Execute the script by clicking on the "Go" button

    the following line will appear in the debug log:

      	HH:NN:SS Executing test.js.test in test case Unnamed.tci - OnKernelStart
    	HH:NN:SS Execution Finished
    This means that the script executed with no errors

  8. Define a variable for the filename, and the line of text that will be logged:

       function test(){
         var filename;
         var logline;
       }
      

  9. Assign a value to the contents of filename. Check the value using the WriteToLog routine

        function test(){
          var filename;
          var logline;
          filename = kernel.HL7ConnectRoot+'\\logs\\mylog.txt';
          kernel.WriteToLog(DBG_SERIOUS, filename);
        }
       
    Note: When calling WriteToLog, you must provide a numerical level for the message. If the level is lower than that defined by the HL7Connect administrator in the global options, then the message will be written to the log. The level is ignored in the Development Environment. Execute the script to check that the filename is correct

  10. Build the line to be logged

        function test(){
          var filename;
          var logline;
          var now;
    
          filename = kernel.HL7ConnectRoot+'\\mylogs\\mylog.txt';
          now = new Date();
          logline = 'HL7Connect started at '+now.toLocaleString()+'\r\n';
    
          kernel.WriteToLog(DBG_SERIOUS, logline);
        }
       
    Again, the debug log output should show the correct value in the log with a blank line for the end of line.
    Note: When HL7Connect is running as a service, toLocaleString may behave in unexpected fashion due to the way Windows NT etc handle date formats in the system context.

  11. Save the line to the file.

    Javascript doesn't have any built in support for interacting with files. The HL7Connect API includes some useful routines for interacting with files. See Utilities for further information.

    Use AppendStringToFile to write to the log file:

       function test(){
         var filename;
         var logline;
         var now;
    
         filename = kernel.HL7ConnectRoot+'\\mylogs\\mylog.txt';
         now = new Date();
         logline = 'HL7Connect started at '+now.toLocaleString()+'\r\n';
    
         AppendStringToFile(logline, filename);
       }
      


© Kestral Computing P/L 2000-2010.