KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: How to programmatically get the forms for a given table
PRODUCT: 4D | VERSION: 11.4 | PLATFORM: Mac & Win
Published On: July 16, 2009
Your current browser may not allow you to download the asset. We recommend Mozilla Firefox or Google Chrome.
Log In

To programatically get forms which belong to a table, we can use the log file created from the MSC after running "Verify the application" in the "Verify" theme. The file is placed in the "Logs" folder located next to the structure file and it is named "<databaseName>_Verify_Log.xml". We then need to parse the log file and populate form names into an array.

Let say we have a database with two tables and we want to get the forms for the "Table_1"



We need to specify the table name into $forTable and run the following method:

ARRAY STRING(16;$elemRef_a;0)
ARRAY TEXT($formName_a;0)
C_STRING(16;$elemRef;$rootRef)
C_TEXT($forTable)
C_TEXT($formName;$formNameResult)
C_TEXT($path; $xPath)
C_INTEGER($loopIndex;$namePos)

$forTable:="Table_1"

`In <databaseName> specify the actual name of the database
$path:=get 4D folder(Logs Folder)+"<databaseName>_Verify_Log.xml"
$rootRef:= DOM Parse XML source($path)

`element step[7] contains the information for the table forms
$xPath:="/verifylog/step[7]/step"
$elemRef:=DOM Find XML element($rootRef;$xPath;$elemRef_a)

For($loopIndex;1; Size of array($elemRef_a))
  DOM GET XML ATTRIBUTE BY NAME($elemRef_a{$loopIndex};"title";$formName)
  $namePos:=Position($forTable;$formName)
  If($namePos>0)
    $formNameResult:=Substring($formName;Length($forTable)+4)
    APPEND TO ARRAY($formName_a; $formNameResult)
  End if
End for

DOM CLOSE XML($rootRef)


After running this method, $formName_a will contain the names of the forms for table "Table_1". Note that although this programmatically returns data about forms, the Verify the Application feature of the MSC must have been run since the last time forms were created or deleted for the information to be accurate.

Commented by Thomas Fitch on August 7, 2009 at 6:03 PM
This can be very useful in different environments, but you have to be especially careful when using it in a development environment. It's easy to make changes and then run this method, expecting a valid result but getting another, with no error message. If you do not regularly use the "Verify the Application" feature of the MSC then this method can return bad data which can cause errors in your application.
Commented by Charlie Vass on July 17, 2009 at 1:47 PM
Excellent example of using all of the 4D tools available to a developer