Updates the content assigned to a Business Component variable into the database. You must have previously assigned the desired data to the variable.
&VarBasedOnBC.Update()
Where:
&VarBasedOnBC
Is a scalar or collection variable based on a Business Component.
Type returned:
Boolean
When the Update method is executed, the database will be updated only if the referential integrity doesn't fail and if error rules don't occur.
The method always returns a boolean value that informs whether the Update could be executed successfully or not. This boolean value can be evaluated if you want to.
Suppose you 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 Knowledge Base and you can define a variable of the new type created in any object. So, in any object, you can 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 object) 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 only 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, suppose you 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 Knowledge Base and you can define a variable of the new type created in any object (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 an error occurs or not. Then, it's up to you to commit the changes depending on the errors.
If you want to know which BCs had an error, you have to scan the list and check each one; for example, after applying the Update method, you 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