The assign( function assigns a value to a field or variable, also returning the value.
This function has two parameters:
value – the value to be stored in the field or variable. This value is also returned as the result of the function.
fieldvariable – the name of the field or variable to store the value into. The field or variable must already exist and must be enclosed in quotes.
The assign( function is different from every other Panorama function. Instead of calculating a new value based on its parameters, the assign( function allows you to assign the result of a partial formula to a field or variable. This is called a side effect because this assignment is in addition to the normal operation of the formula that contains the assign( function (or multiple assign( functions).
Let’s look at some examples to illustrate the operation of this function. For example, consider the equation below, which might be part of a procedure. In this example, both A and B will be assigned the value X+Y.
When using the assign( function it’s important to keep in mind that this function also produces a value - the value calculated by the value parameter. For example, in the example above B is also assigned the value X+Y. If you forget that the assign( function produces a value you may encounter unexpected error messages or even accidentally overwrite data.
The previous example illustrated the operation of the assign( function, but it wasn’t really very useful. Here is a more interesting example. This example quickly scans a field and finds the largest value. The output of the arraybuild (the variable x), is simply ignored. The real result is the variable biggest, which is calculated as a side effect. At the end of the procedure this variable will contain the largest value in the current field.
local biggest,x biggest=«Shoe Size» arraybuild x,cr(),"",assign( ?( biggest>«Shoe Size»,biggest,«Shoe Size»),"biggest") message biggest
The assign( function is very useful for formulas that display text in form objects. In these objects the assign( function can be used for intermediate values that need to be used over and over. Of course when you use the assign( function this way you have to keep in mind the order of expression evaluation. You must make sure that the assign( function is evaluated before the destination is used. Here is an example where the customer name is assigned to a variable named tempCustomerName (which must be created with a global or fileglobal statement before this formula is displayed), then used in several locations in the text without having to re-do the lookup (which is relatively slow).
"Dear "+ assign( lookup( "CustomerFile","CustomerNumber",custID,"Name","",0),"tempCustomerName")+ ","+cr()+ tempCustomerName+", you are invited to particpate blah blah blah. "+ tempCustomerName", you won't want to miss out on this blah blah blah"
Note: Panorama now includes a cache( function that is similar to assign, but automatically creates a temporary variable for the duration of the formula itself. In the example above you could use cache( instead of assign(, eliminating the need to create the tempCustomerName variable in advance with a global or fileglobal statement. Any variables created with the cache( function are automatically disposed of when the formula evaluation is complete.
If you use the ?( function in combination with the assign( function be careful because the assign( function will always be evaluated. It doesn’t matter whether the assign( function is in the true or false part of the ?( function.
When the assign( function is used with a field, the field display will not be updated automatically. You’ll need to do that separately with showfields, showcolumns, etc. (depending on what operation you have done).
Assigning a value to a field in a formulafill (or select) in a multi-user shared database doesn’t work. There’s no warning.
|10.0||No Change||Carried over from Panorama 6.0.|