Creating a Project
Let's begin by learning how to create a record in the database. We will start by creating a new project in the projects table.
Edit the current card script of the program stack. If the To-Do window is frontmost then you can choose Object > Card Script.
Locate the uiCreateProject handler in the card script. This is where you will add the logic that creates a record in the projects table of the database.
Replace the uiCreateProject handler in the card script with the RevTalk code below. Make sure and compile the script after pasting in the new code so that the updated script is applied. After you insert the code we will go through the relevant parts.
---------- Copy & Paste The Following Code ---------- command uiCreateProject ## Create a SQL Record object for 'projects' table put sqlrecord_createObject("projects") into theRecordA ## Set name property of object sqlrecord_set theRecordA, "name", "New Project" ## Create record in the database sqlrecord_create theRecordA put the result into theError if theError is empty then ## Refresh list lock screen RefreshProjectsPeopleList ## select new record (uSelectedProjectID is a custom property defined in group script) set the uSelectedProjectID of group "ProjectsPeople" to theRecordA["id"] ## tell Data Grid to select all text when field editor is opened set the dgTemplateFieldEditor["select text"] of group "ProjectsPeople" to true ## open field editor so that user can change name. dispatch "EditKeyOfIndex" to group "ProjectsPeople" with "name", \ the dgHilitedIndex of group "ProjectsPeople" unlock screen end if if theError is not empty then answer "Error creating project:" && theError & "." end if end uiCreateProject
The code to create a new record is fairly straightforward.
1) Create a SQL Record object for the projects table. This object is represented by an array and it contains the keys for each of the fields (or columns) in your database. Each field key in the array contains the default value as defined by the database. Note that since the object is represented using an array it only exists as long as the array variable exists. A SQL Record object does not persist across sessions.
2) Set table field properties. We are going to assign every new project the name "New Project". The user can change the name in the UI if they wish.
3) Tell SQL Yoga to create a record in the database that has the properties of the SQL Record object.
You that you have updated the card script you can test. The Project button already has a script that calls uiCreateProject in the card script. Click on the button to create a record. YOU WON'T GET ANY VISUAL FEEDBACK AT THIS POINT.