Business Component Insert method

Official Content

This method can be applied to a variable based on a Business Component type of a transaction, in order to try to insert in the database the data previously assigned to the variable.

When the Insert method is executed, the data previously assigned into memory will be inserted 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 addition to the database could be executed successfully or not. The boolean value can be evaluated by the developer or not.

Syntax

    &BCVariable.Insert()

Where:
   &BCVariable is a scalar or collection variable based on a Business Component type.  

Type returned
    Boolean

Examples

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=new() //in this case the new operator can be ommited, but if more than one customer is inserted, it must be used
&Customer.CustomerName = 'Mary'
&Customer.CustomerLastName = 'Brown'
&Customer.CustomerAddress = '767 5th Avenue'
&Customer.CustomerEmail = 'mbrown@gmail.com'
&Customer.Insert()
if &Customer.Success()
   commit   
else   
   msg(&Customer.GetMessages().ToJson(), status)
endif 


2) The following code (defined for example in a procedure source or inside an event in a web panel) is inserting a customer. It is almost equal to the previous example with the unique variant that the result of applying the Insert() method is directly evaluated with an if sentence:

&Customer=new() //in this case the new operator can be ommited, but if more than one customer is inserted, it must be used
&Customer.CustomerName = 'Mary'
&Customer.CustomerLastName = 'Brown'
&Customer.CustomerAddress = '767 5th Avenue'
&Customer.CustomerEmail = 'mbrown@gmail.com'
if (&Customer.Insert())
    commit    
else
    msg(&Customer.GetMessages().ToJson(), status)
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 perform the addition in the database:

Procedure: InsertCustomer
   Rules
      parm(in:&Customer)
   Source
      &Customer.Insert()
     if &Customer.Success()
         commit   
      else
         msg(&Customer.GetMessages().ToJson(), status)
      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 Insert method is applied to it as follows:

&Products=DPProducts()
if &Products.Insert()
    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
    ProductName = 'X Muscular Pain Medicine'
    ProductStock = 1000
}
Product
{ 
   ProductId = 101
   ProductName = 'J Headache Medicine'
   ProductStock = 1500
}

In conclusion, a collection of products is loaded and after that, the Insert method is applied to the collection variable. Each product in the collection will be inserted.

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 Insert 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 Insert method
Error handling in Business Components


Was this page helpful?
What Is This?
Your feedback about this content is important. Let us know what you think.