Tips_tutorials   >   Studiojs201   >   Countries List

Countries List

We are now ready to tackle creating a remote task that will fetch records from the database and return it as an HTML page.

The simplest table in our database is the Country table. We will begin with creating a remote task that will return a web page listing all of the countries in our database.

Create the remote task.

  1. F2 Browser > select ContactsWeb library > New Class > Remote Task.
  2. Name the remote task, rtCountryList.

Task Variables

The first problem we are going to run into is that the instance of rtCountryList opened by the Omnis Web App Server is not in scope with the Startup_Task instance. They live in two different worlds.

The Startup_Task has two task variables that we would like to access from our remote task instance:

  1. dbsessionobj - The session which was opened with the database on startup.
  2. errhndlr - The error handler which was initialized on startup.

Being able to access those Startup_Task variables from our remote task would save waste time opening a new session and initializing another error handler each time a remote task request came in.

To accomplish this we need to add a method to the Startup_Task.

  1. Add a $getTaskVarRef method to the Startup_Task.
  2. Add a Character parameter, pTaskVarName, to the method.
  3. Add a Field reference parameter, pfrVar, to the method.
  4. Add the following code to the method:

Set reference pfrVar to [pTaskVarName].$ref
Quit method kTrue

The $getTaskVarRef method sets a reference to the specified task variable.

To minimize code in the $construct method of rtCountryList we will use a private method to set the remote task task variables.

  1. Add the following task variables to rtCountryList.
    1. dbsessionobj - Item reference variable
    2. errhndlr - Item reference variable

  2. Add a private method, setTaskVars to rtCountryList.

  3. Add the following code to the setTaskVars method.

Do $itasks.[$clib().$name].$getTaskVarRef('dbsessionobj',$ctask.dbsessionobj) Returns FlagOK
If FlagOK
   Do $itasks.[$clib().$name].$getTaskVarRef('errhndlr',errhndlr) Returns FlagOK
End If
Quit method FlagOK

$construct Method

We are finally ready to write the code in the $construct method of our rtCountryList remote task.

  1. Add an Object type instance variable, ioHTMLTools, to the rtCountryList class.
  2. Point ioHTMLTools to the oHTMLTools object class.
  3. Add a Row type parameter, pParamsRow to the $construct method. (Do not attempt to make this a field reference variable! I discovered the hard way that Omnis Studio doesn't like that.)
  4. Add the following code to the $construct method.

    ; Set the task variables.
    Do method setTaskVars Returns FlagOK
    If FlagOK
       ; Define a list variable using the 'tCountry' table class.
       Do List.$definefromsqlclass('tCountry')
       ; Set the session object in the list variable so that the SQL statements will be issued to that session's database.
       Do List.$sessionobject.$assign(dbsessionobj)
       ; Get all the records in the table.
       Do List.$getAllRecords() Returns FlagOK
       If FlagOK
          ; Prepare HTML table to return to the web browser.
          Calculate bInclCheckboxes as kTrue
          Calculate CSVColHeadings as 'Country Name'
          Calculate CSVColNames as 'CountryName'
          Calculate PKeyColName as List.$:PrimaryKeyColName
          Do ioHTMLTools.$convertListToHTMLTable(List,bInclCheckboxes,CSVColNames,CSVColHeadings,PKeyColName) Returns TableHTML
          ; Get an HTML page template.
          Do ioHTMLTools.$retHTMLPageTemplate() Returns HTML
          ; Replace the placeholders with content.
          Calculate HTML as replaceall(HTML,'##TITLE##','Countries')
          Calculate HTML as replaceall(HTML,'##BODY##',TableHTML)
          ; Add the HTTP content header.
          Do ioHTMLTools.$addHTTPContentHeader(HTML) Returns FlagOK
       End If
    End If

    If not(FlagOK)
    End If

    Quit method HTML

Test Countries Remote Task

We are ready to test the rtCountryList remote task.

Open a web browser and enter the URL which applies to your localhost web server:

All going well the rtCountryList remote task will receive your request and respond to you with a new web page listing the countries in the database.

The page won't look fancy. Don't worry we can dress it up later using CSS (Cascading Style Sheets).