The Save method executes something equivalent to what happens when the user press the confirm button in a transaction form.
When it is executed (because it is applyed to a variable based on a business component type of a transaction), the formulas and rules defined in the transaction are triggered and the referential integrity is checked, before saving phisically the involved data.
This method can be used to insert and update physically. GeneXus understands the Save method is used to insert, if we assign values to the properties of a variable based on a business component type and we execute the Save method to the variable, but we haven't instantiated data in memory previously by using the Load method. On the other hand, if first of all we apply the Load method to the business component variable to instatiate certain data in memory, and after that we assign values to the properties of the business component variable and save, GeneXus understand we are updating the data we have loaded as a first step.
It's essential to know that the Save method is only valid to be applied to business component variables of the transaction first level. This is easy to be understood since the behavior is the same as the Confirm button offers in a transaction form: when it is pressed, it saves all the data present in the form (belonging to all the transaction levels).
&VbleBasedOnBCType.Save()
Where:
&VbleBasedOnBCType
Is a -temporary and local- variable defined in a GeneXus object, based on a business component type of a transaction.
Let's suppose we define the following transaction as Business Component (by setting its Business Component property = True):
Customer
{
CustomerId* (Autonumber property = True)
CustomerName
CustomerAddress
CustomerPhone
CustomerEmail
CustomerAddedDate
CustomerTotalMiles
}
Customer rule:
Default(CustomerAddedDate,&today);
Thus, a business component data type of the Customer transaction is automatically created in the KB and we are able to define in any object, a variable of the new type created. So, in any object we define a variable named &customer based on the Customer type.
1) The following code (defined for example in a procedure source or inside an event in a web panel) is inserting a customer:
&Customer.CustomerName = 'Mary'
&Customer.CustomerLastName = 'Brown'
&Customer.CustomerAddress = '767 5th Avenue'
&Customer.CustomerEmail = 'mbrown@gmail.com'
&Customer.save()
if &Customer.success()
commit
else
rollback
endif
2) The following code (defined for example in a procedure source or inside an event in a web panel) is updating a customer:
&Customer.Load(8)
&Customer.CustomerEmail = 'marybrown@gmail.com'
&Customer.save()
if &Customer.success()
commit
else
rollback
endif
Error handling in Business Components
|