Tips_tutorials   >   Studiojs201   >   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.
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:
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.
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.
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
We are finally ready to write the code in the $construct method of our rtCountryList remote task.
; 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)
Breakpoint
End If
Quit method HTML
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).