CopyEntity
Table of Contents
CopyEntity is a method, that is available within any entity. In the model of the AI-Framework, the call is: .Copy. This is the functional name. It copies all fields of an entity (which is a record in memory) to a new entity.
Read more about this in How to > Basic concepts > Entities
Example of usage
public IMethodCall MakeCopyOfOrder()
{
return Method(() =>
{
var currentOrder = OrdersView.CreateItemReference();
var newOrder = OrdersView.CreateItemReference();
return new Body
{
currentOrder.Assign(OrdersView.Current),
newOrder.Assign(currentOrder.Copy()),
newOrder.AssignNextOrderNr(),
newOrder.OrderDateTime.Assign(DateTimeExpression.Now),
newOrder.Status.Assign(OrderStatus.Open),
Orders.Save()
};
})
.SetWorkDescription("make a copy of an order")
.SetImage(CommonImages.ArrowRight)
.Call();
}
Explanation of the example
This is a method, named MakeCopyOfOrder(), which is situated inside the Entity OrderEntity. It has therefore direct access to the data, related to the table Order.
- Line 5:
OrdersViewis a view, based on theOrderEntity. The variablecurrentOrderis defined as a new instance of a record (in memory). - Line 6: Another variable,
newOrder, is also defined as a new instance of a record (in memory). - Line 10 assigns the Current record of the
OrdersViewto the variable currentRecord. - It all happens in line 11: All data in the record, that was just assigned to the variable
currentOrder, is now copied to the variablenewOrder– that is: the new record in memory. - After that, three values (fields) are overwritten with new values, so the new record will be unique:
- A new Order number (line 12),
- an order date and time stamp (line 13) and
- the status is set on Open.
- In line 15, the changes that were made in Orders – in memory, that is on entity level – will now be written to the database with
Orders.Save().
How to use this
Anywhere in the model code, where the OrderEntity is available, a button can be placed on a form, that invokes this method: Orders.MakeCopyOfOrder(). If, for example, the form shows a list of orders, clicking the button will make a copy of the order that is currently selected in that list.