KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Utility method to find all choice lists
PRODUCT: 4D | VERSION: 19 | PLATFORM: Mac & Win
Published On: April 25, 2022

Since lists in project mode will be read-only in compiled and client-server, it may be recommended to start loading list from elsewhere like via records which allows read/write access. To follow up on this change, it will be useful to find all choice lists in the application to make any necessary code changes. The utility method below will find all form objects with an assigned choice list and copies the JSON object to clipboard.

C_LONGINT($i)
C_LONGINT($j)
C_LONGINT($count_l)
C_LONGINT($pos_l)

C_TEXT($sourcePath_t)
C_TEXT($formPath_t)
C_TEXT($form_t)
C_TEXT($obName_t)
C_TEXT($pasteboard_t)
C_TEXT($tableName_t)

C_OBJECT($formJSON_o)
C_OBJECT($page_o)
C_OBJECT($prop_o)
C_OBJECT($result_o)
C_OBJECT($choiceList_o)

ARRAY LONGINT($table_al; 0)

ARRAY TEXT($table_at; 0)
ARRAY TEXT($forms_at; 0)
ARRAY TEXT($tableForms_at; 0)

C_COLLECTION($formPaths_c)

$sourcePath_t:=Get 4D folder(Database folder)+"Project"+Folder separator+"Sources"+Folder separator

// Get all table names and numbers
GET TABLE TITLES($table_at; $table_al)

$formPaths_c:=New collection

// Push all project form paths to collection
FORM GET NAMES($forms_at)
For ($i; 1; Size of array($forms_at))
   $formPaths_c.push($sourcePath_t+"Forms"+Folder separator+$forms_at{$i}+Folder separator+"form.4DForm")
End for

// Push all table form paths to collection
For ($i; 1; Size of array($table_al))
   FORM GET NAMES(Table($table_al{$i})->; $tableForms_at)
   For ($j; 1; Size of array($tableForms_at))
     $formPaths_c.push($sourcePath_t+"TableForms"+Folder separator+String($table_al{$i})+Folder separator+$tableForms_at{$j}+Folder separator+"form.4DForm")
   End for
End for

$result_o:=New object
$result_o.result:=New collection

// For each form path in the collection
For each ($form_t; $formPaths_c)

  If (Test path name($form_t)=Is a document)
    $formJSON_o:=JSON Parse(Document to text($form_t))

      // For each page on a form
      For each ($page_o; $formJSON_o.pages)
        // Make sure page has some objects
        If ($page_o#Null)
          // For each object in the page
         For each ($obName_t; $page_o.objects)

          If ($page_o.objects[$obName_t].choiceList#Null)
          $choiceList_o:=New object

          $count_l:=$count_l+1

          $pos_l:=Position("TableForms"; $form_t)
          If ($pos_l>0)
            C_TEXT($temp; $tableNum_t; $formName_t)

            $temp:=Split string($form_t; "TableForms")[1]
            $tableNum_t:=Split string($temp; Folder separator)[1]
            $tableName_t:=Table name(Num($tableNum_t))

            $formName_t:=Split string(Split string($form_t; Folder separator+$tableNum_t+Folder separator)[1]; Folder separator)[0]
            $formName_t:="["+$tableName_t+"]"+$formName_t+"."+$obName_t

            $choiceList_o.table_form:=$formName_t
           Else
            $tableName_t:=""
            $formName_t:=Split string(Split string($form_t; "Forms")[1]; Folder separator)[1]+"."+$obName_t

            $choiceList_o.project_form:=$formName_t
          End if

          $choiceList_o.path:=Split string($form_t; Get 4D folder(Database folder))[1]
          $choiceList_o.choiceList:=$page_o.objects[$obName_t].choiceList

          If ($page_o.objects[$obName_t].dataSource#Null)
            $choiceList_o.dataSource:=String($page_o.objects[$obName_t].dataSource)
          End if

          If ($page_o.objects[$obName_t].dataSourceTypeHint#Null)
            $choiceList_o.dataSourceTypeHint:=String($page_o.objects[$obName_t].dataSourceTypeHint)
          End if

          $result_o.result.push($choiceList_o)

        End if

        End for each
      End if
    End for each
  End if
End for each

$pasteboard_t:="Found variables with ChoiceList: "+String($count_l)+Char(Carriage return)+Char(Carriage return)+JSON Stringify($result_o; *)
SET TEXT TO PASTEBOARD($pasteboard_t)
ALERT("Copied to clipboard")