KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Utility Method to Convert Web Variables to Object
PRODUCT: 4D | VERSION: 17 | PLATFORM: Mac & Win
Published On: November 7, 2019

When building a web application with 4D, sometimes it is necessary to get user input from a query string or data form, so that the information can be saved to the database as a new entity. Before the entity can be created, however, the data should be converted into an object.

Below is a method to extract the information using WEB GET VARIABLES and to convert the data into an object.

// ----------------------------------------------------
// Method:
//  webVariablesToObject
//
// Description:
//  Extracts web variables into text arrays and returns an object
//  with Name:Value pairs.
//
// Parameters:
//  N/A
// ----------------------------------------------------

C_OBJECT($0;$webVars_o)
ARRAY TEXT($varNames_at;0)
ARRAY TEXT($varValues_at;0)

WEB GET VARIABLES($varNames_at;$varValues_at)

$webVars_o:=New object

For ($i;1;Size of array($varNames_at))
      $webVars_o[$varNames_at{$i}]:=$varValues_at{$i}
End for

$0:=$webVars_o


For example, if a user inputs values for "varName1" and "varName2" as "varVal1" and "varVal2", respectively, then the object that the above method would return would look like this:

{"varName1":"varVal1", "varName2":"varVal2"}

Then, it's just a matter of mapping the name/value pairs from the object to a new entity by using entity.fromObject() and saving that new entity to the database.