This method can be applied to a variable based on a business component type of a transaction in order to perform an “upsert” operation; that is to say, an addition will be attempted, but if it fails because a duplicate key is found, an update will be performed.
&BCVariable is a scalar or collection variable based on a Business Component type.
1) Suppose you define the following transaction as Business Component (by setting its Business Component property = True):
So, a business component data type of the Customer transaction is automatically created in the KB and you are able to define in any object, a variable of the new type created.
Thus, the following procedure receives a &Customer variable based on the Customer type, and in its source, the InsertOrUpdate method is applied to the variable in order to try to insert the customer or update it if the addition fails because the key is duplicated.
2) Suppose you define the following transaction as Business Component (by setting its Business Component property = True):
So, a business component data type of the Product transaction is automatically created in the KB and 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 InsertOrUpdate method is applied to it as follows:
Look at the DPProducts Data Provider definition:
Data Provider: DPProducts
Properties: Output:Product / Collection:True
ProductId = 100
ProductName = 'X Muscular Pain Medicine'
ProductStock = 1000
ProductId = 101
ProductName = 'J Headache Medicine'
ProductStock = 1500
In conclusion, a collection of products is loaded and after that, the InsertOrUpdate method is applied to the collection variable. Each insertion will be tried, and if some product of the collection already exists physically, it will be updated.
3) Now, suppose you define the following two-level transaction as Business Component (by setting its Business Component property = True):
So, a business component data type of the Country transaction is automatically created in the KB and you are able to define in any object, a variable of the new type created.
Suppose that a record with CountryId=1 is already stored, its corresponding CountryName='Uruguay', its CountryPopulation=3300000, the CountryFlag has the corresponding stored image and the following related cities are also stored:
1, 1, Montevideo
1, 2, Maldonado
1, 3, Colonia
After that, in some object, a variable named &Countries is defined based on the Country type and it is set as a collection. The DPCountries Data Provider loads the &Countries variable (for example inside an event). Once the &Countries variable is loaded, the InsertOrUpdate method is applied to it as follows:
Look at the DPCountries Data Provider definition:
Data Provider: DPCountries
Properties: Output:Country / Collection:True
CountryId = 1
CountryPopulation = 3445863
CityId = 4
CityName = 'Salto'
CityId = 5
CityName = 'Paysandu'
CountryId = 2
CountryName = Argentina
CountryFlag = ArgentinaFlag.Link()
CountryPopulation = 43000000
CityId = 1
CityName = 'Buenos Aires'
CityId = 2
CityName = 'Rosario'
CityId = 3
CityName = 'Cordoba'
For the CountryId=1, the insertion will be tried, but the CountryId=1 already exists. So, its CountryPopulation will be updated and the CityId=4 and the CityId=5 will be added related to it. Then, For the CountryId=2 , the insertion will be tried and it will be successful.
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 you to commit the changes depending on the errors or not.
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 InsertOrUpdate method, you may write the following code:
For &Product in &Products
if &Product.GetMessages().Count > 0
This method is available since GeneXus 15.
- For iOS offline is available as from GeneXus 15 Upgrade 2.
- If the primary key has its Autonumber property set to True, the method will always insert, since it's never going to happen that a duplicate key is detected (because always a new identifier value is generated).
Error handling in Business Components