KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Utility Method to Get Names of Choice Lists Used by Objects in a Form
PRODUCT: 4D | VERSION: 17 R | PLATFORM: Mac & Win
Published On: December 18, 2019

Choice lists are a great 4D tool to use when you have multiple form objects that frequently utilize common lists. Keeping track of which objects in a form are using which lists can become difficult if the number of form objects or lists grows rapidly. Below is a utility method to organize all form objects that use choice lists into a collection of JSON objects. In the JSON objects, a form object's name will be paired with the name of the choice list that it uses.

// --------------------------------------------------------------------------------
// Method: FORM_GET_OBJECT_LISTS

// Description: Iterates each page of a form and pairs any form objects that use a choice list with the name of the corresponding list.
//
// Parameters: $1 (TEXT) - Form Name
//
// Output: $0 (COLLECTION of OBJECTS)
// - Each object represents a page on the form, starting from 0 and going in ascending order.
// - Pages with form objects that use choice lists will output an object in this format: {"formObjectName1":"ListName1", "formObjectName2":"ListName2", ...}
// - Pages with no form objects will output "null"
// - Pages with form objects but no choice lists used will output "{}"
// --------------------------------------------------------------------------------

C_OBJECT($dynform_o,$pageObjects_o;$pageObjectsListnames_o)
C_COLLECTION($0;$dynformPages_c;$formObjectsListnames_c)
C_LONGINT($pagesLength_li;$counter_li)
C_TEXT($1;$formName_t;$objName_t)

If (Count parameters=1)

  $formName_t:=$1

  $dynform_o:=FORM Convert to dynamic($formName_t)
  $dynformPages_c:=$dynform_o.pages
  $pagesLength_li:=$dynformPages_c.length-1
  $formObjectsListnames_c:=New collection

  For ($counter_li;0;$pagesLength_li) // for each page on the form
   If ($dynformPages_c[$counter_li]#Null)
     $pageObjects_o:=$dynformPages_c[$counter_li].objects
     $pageObjectsListnames_o:=New object
     For each ($objName_t;$pageObjects_o) // for each form object on the page
       If ($pageObjects_o[$objName_t].type="list") // Hierarchical Lists have different property to access choice list
         $pageObjectsListnames_o[$objName_t]:=$pageObjects_o[$objName_t].list
       Else
         $pageObjectsListnames_o[$objName_t]:=$pageObjects_o[$objName_t].choiceList
       End if
     End for each
     $formObjectsListnames_c.push($pageObjectsListnames_o)
   Else
     $formObjectsListnames_c.push(Null)
   End if
  End for
  $0:=$formObjectsListnames_c

End if

For example, suppose that you have a form named "Form1" with two form objects on page one: (1) a variable named "Variable" that uses a choice list named "New List1", and (2) a hierarchical list named "HList" that uses a choice list named "New List2". On page two, suppose there is only a combo box named "Combo Box" that does not use a choice list. If you pass "Form1" into the above method, this would be the output:

[ null, {"Variable":"New List1", "HList":"New List2"}, {} ]

You would get null for page zero (because it contains no objects), a JSON object for page one, and an empty object for page two (because it contains no choice list objects).