KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Utility Method to Convert an Entity Selection to an Object Array
PRODUCT: 4D | VERSION: 17 R | PLATFORM: Mac & Win
Published On: October 10, 2019

Below is a utility method that will convert an entity selection to an object array, or properties of an entity selection to passed arrays.

// ----------------------------------------------------
// Method: EntitySelectionToArray
//
// Description
// Converts an entity selection or properies
// of an entity selection to an array or arrays
//
// Parameters
// $1 - Entity Selection Object
// $2 - Collection of Filters for specific properties
// or Empty collection for array of entire entity object
// ${3}- Pointer{s} to Array{s} to contain the result{s}
// NOTE : Typing is not checked.
//
// ----------------------------------------------------

C_OBJECT($1;$entSel_ob)
C_COLLECTION($2;$filters_c)
C_POINTER(${3})


C_COLLECTION($temp_c)
C_TEXT($filter_t)
C_LONGINT($i)

If (Count parameters>2)
   $entSel_ob:=$1
   $filters_c:=$2
  
   If($filters_c.length>0)
      $filter_t:=$filters_c[0]
      For ($i;1;$filters_c.length-1)
         $filter_t:=$filter_t+","+$filters_c[$i]
      End for
  
      $temp_c:=$entSel_ob.toCollection($filter_t)
  
      For ($i;1;Count parameters-2)
         COLLECTION TO ARRAY($temp_c;(${$i+2})->;$filters_c[$i-1])
      End for
  
   Else
      $temp_c:=$entSel_ob.toCollection()
      COLLECTION TO ARRAY($temp_c;$3->)
   End if
  
End if


Examples of the command:

The following will create an object array of the entity selection.
C_OBJECT($entSel_ob)
C_COLLECTION($filters_c)
ARRAY OBJECT($result_ao;0)

$entSel_ob:=ds.Table_1.all()

EntitySelectionToArray($entSel_ob;$filters_c;->$result_ao)


The following will populate the two arrays with the values of the two properties passed into the collection.
C_OBJECT($entSel_ob)
C_COLLECTION($filters_c)

ARRAY TEXT($arrText;0)
ARRAY LONGINT($arrNum;0)

$filters_c:=New collection
$filters_c.push("TextField")
$filters_c.push("NumericField")

$entSel_ob:=ds.Table_1.all()

EntitySelectionToArray($entSel_ob;$filters_c;->$arrText;->$arrNum)