With great power comes great responsibility. Signed: Every superhero's uncle.

Description

The SetVariables() function can be used to set one or more variables from within CFL. The function takes a JSON formatted string.

The JSON syntax requires:

  • An opening curly brace
  • The variable name be wrapped in double quotes
  • A color
  • The value. For numeric values, no quotes are used. For text values, surround the value in quotes
  • A comma between each variable
  • A closing curly brace

Returns in the text will be ignored anywhere except within the quotes.

Examples

Simple Example

DECLARE NewValues :=  '{ "Quantity"    : 11, "ArtworkFileName" : "MyFile.PNG" } ';
SetVariables( NewValues )

After calling this, Quantity will be overridden to be 11 and the ArtworkFileName variable will be likewise set to MyFile.PNG.

Complex Example

Here is a real-world example. A part UDF was just a text field that used to enter an INI field of values. (This required a UDF entry form so the user could enter multi-line values.) Machine=HP-2500 Media=64 inch Bond etc. These values were pulled from the part and used to set variables with the same name. To do this, the INI field had to be converted from the INI format to a JSON format. The rest was easy.

// DECLARE INIFile :=  'Description=Friday
// Quantity=5';
DECLARE INIFile := PartUDFByPartName(PartName, "Configuration UDF", "");
 
DECLARE JSON := "";
DECLARE line := "";
DECLARE propvalue := "";
DECLARE prop : = "";
DECLARE p := 0;
DECLARE lineend := 0;
 
WHILE (LEN(INIFile) > 0) DO
  lineend := pos(INIFile, CHAR(13),1)-1;
  IF (lineend<1) THEN lineend := LEN(INIFile) ENDIF;
 
  line := TRIM(LEFT(INIFile, lineend));
 
  IF (LEN(line)>0) THEN
    p := pos(line, "=");
    IF (p = 0) THEN
      JSON := JSON + '"' + line + '" : "", '
    ELSE
      propvalue := TRIM(MID(line, p+1,9999));
      JSON := JSON 
              + IF (LEN(JSON)>1) THEN ", " ELSE "" ENDIF
              + '"' + TRIM(LEFT(line, p-1)) + '" : '
              + IF ISNumber(propvalue) THEN propvalue ELSE '"'+propvalue+'"' ENDIF;
    ENDIF;
  ENDIF;
 
  INIFile := TRIM(MID(INIFile, lineend+1, 9999));
ENDWHILE;
 
JSON := "{ "+JSON+" } "
 
SetVariables( JSON )

Contributor: Cyrious Software Date: 2017-05 Version: Control 6.1+

You could leave a comment if you were logged in.