Assigns a default value to an attribute or variable.
Default(att | &var, expression);
Where:
att | &var
Is the attribute or variable to be assigned.
expression
Is any valid expression. The result must match the attribute or variable data type.
Objects: Transaction, Procedure, Web Panel
This rule is mostly used to initialize an attribute with a certain value, which can be modified, in a Transaction object. Here, the rule is triggered only when the Transaction is executed in Insert mode. When the Transaction is executed in other modes like Update, Delete or Display, the attribute already has a stored value, and the objective of this rule is not to rewrite it.
Although this rule can be used in other objects with the same objective, its use is unusual because depending on the object, the variables may be initialized in different sections, like an event, source, etc.
Sample # 1
Customer
{
CustomerId*
CustomerName
CustomerAddress
CustomerPhone
CustomerAddedDate
CustomerState
}
Customer Rule:
Default(CustomerAddedDate,today());
When the Customer Transaction is executed in Insert mode, the CustomerAddedDate attribute is displayed initialized with today's date. The end user can modify the suggested date.
If you don't want to allow it to be modified, you can add the following rule:
Noaccept(CustomerAddedDate);
Sample # 2
The following example shows a rule defined in the Customer Transaction that uses the Previous function to initialize the CustomerState attribute for a new customer that is being inserted (with the value of the previous customer).
Default(CustomerState,Previous());
In other GeneXus objects (like Procedures or Web panels), the Default rule can only be defined to initialize variables, and they will be triggered at the beginning of program execution. The expression in these cases only allows 'literals' between quotes, numbers, and the following functions: Today(), Date() and Sysdate(). An example is shown below:
Default(&FirstCustomerId, 1);
Default(&LastCustomerId, 999);