If the developer needs to perform an update in the database, he/she can apply this method to a variable based on a Business Component type of a transaction, to which he/she must previously have assigned the desired data.
When the Update method is executed, the database will be updated only if the referential integrity doesn't fail (because it is checked) and if error rules don't occur (because the rules defined in the transaction are triggered).
The method always returns a boolean value that informs if the update could be executed successfully or not. The boolean value can be evaluated by the developer or not.
&BCVariable.Update()
Where:
&BCVariable is a scalar or collection variable based on a Business Component type.
Type returned
Boolean
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 updating a customer:
&Customer = new()
&Customer.CustomerId = 8
&Customer.CustomerEmail = 'marybrown@gmail.com'
&Customer.Update()
if &Customer.Success()
commit
else
msg(&Customer.GetMessages().ToJson())
endif
2) The following code (defined for example in a procedure source or inside an event in a web panel) is updating a customer. It is almost equal to the previous example with the unique variant that the result of applying the Update() method is directly evaluated with an if sentence:
&Customer = new()
&Customer.CustomerId = 8
&Customer.CustomerEmail = 'marybrown@gmail.com'
if (&Customer.Update())
commit
else
msg(&Customer.GetMessages().ToJson())
endif
3) The following procedure is called from several objects. It receives a &Customer variable based on the Customer type and it only has to update the database:
Procedure: UpdateCustomer
Rules
parm(in:&Customer)
Source
&Customer.Update()
if &Customer.Success()
commit
else
msg(&Customer.GetMessages().ToJson())
endif
4) Now, let's suppose we define the following transaction as Business Component (by setting its Business Component property = True):
Product
{
ProductId*
ProductName
ProductStock
}
So, a business component data type of the Product transaction is automatically created in the KB and we are able to define in any object, a variable of the new type created (and it can be set as a collection).
Thus, in a certain object, a variable named &Products is defined based on the Product type and it is set as a collection. The DPProducts Data Provider loads the &Products variable (for example inside an event). Once the &Products variable is loaded, the Update method is applied to it as follows:
&Products=DPProducts()
&Products.Update()
if &Products.success()
Commit
else
msg(&Products.GetMessages().ToJson(), status)
endif
Look at the DPProducts Data Provider definition:
Data Provider: DPProducts
Properties: Output:Product / Collection:True
Source:
Product
{
ProductId = 100
ProductStock = 5000
}
Product
{
ProductId = 101
ProductStock = 6000
}
In conclusion, a collection of products is loaded and after that, the Update method is applied to the collection variable. Each product in the collection will be updated.
If there's an error in a BC from the list, are the following BCs processed?
Yes, all elements in the list are processed whether or not an error occurs. Then, it's up to the developer to commit the changes depending on the errors or not.
If the developer wants to know which BCs had an error, he has to scan the list and check each one; for example, after applying the Update method, he may write the following code:
For &Product in &Products
if &Product.GetMessages().Count > 0
//msg(...)
endif
endfor
Availability
This method is available since GeneXus 15.
See Also
Differences between the Save method and the Update method
Error handling in Business Components
|