---
name: common-business-component
description: Reference for Business Component (BC) operations on Transaction objects
---

Transaction objects defined as Business Component provides object-oriented CRUD interface

---

# BUSINESS COMPONENT
A Business Component (or `BC`) enables programmatic access to Transaction data by setting the `Business Component` property to `True` in the target Transaction object, and allows CRUD operations while preserving all business rules and data integrity defined in the Transaction

---

# ENABLE BUSINESS COMPONENT
The target Transaction must define:

Properties
[
	'Business Component': 'True'
]

Once enabled:
- The Transaction can be used as a DataType
- It exposes properties and methods for CRUD and validation
- All Transaction rules and validations are preserved

---

# BC PROPERTIES
Each Transaction attribute becomes a property of the BC instance, accessible via dot notation

## Attribute Access

Get value:
~~~
&Var = &<trn-name>.<att-name>
~~~

Set value:
~~~
&<trn-name>.<att-name> = <value>
~~~

Get old value (value before in-memory modification):
~~~
&OldValue = &<trn-name>.<att-name>.GetOldValue()
~~~

## Levels (Substructures)
If the Transaction includes levels, each level is exposed as a collection-type property

Example:
- `&<trn-name>.LevelName` is a collection of `<TransactionName>.<LevelName>` type
- Supports collection properties, methods, and iteration commands

---

# BC METHODS

## Load
Loads an existing record from the database into the current instance using the Transaction primary key values

Syntax: `&<trn-name>.Load(<primary-key-1>, …, <primary-key-N>)`

Return:
N/A

Notes:
- Primary key may be simple or compound
- Operation status must be evaluated using Success() or Fail()

## Save
Saves the current instance into the database, executing all Transaction rules and failing if validation errors occur

Syntax: `&<trn-name>.Save()`

Return:
N/A

## InsertOrUpdate
Inserts the record if it does not exist; otherwise updates the existing one

Syntax: `&<trn-name>.InsertOrUpdate()`

Return:
`Boolean`

## Insert
Forces an insert operation and fails if the record already exists

Syntax: `&<trn-name>.Insert()`

Return:
`Boolean`

## Update
Forces an update operation and fails if the record does not exist

Syntax: `&<trn-name>.Update()`

Return:
`Boolean`

## Delete
Removes the current record from the database

Syntax: `&<trn-name>.Delete()`

Return:
N/A

## Check
Executes all business rules and validations without saving changes

Syntax: `&<trn-name>.Check()`

Return:
N/A

---

## Success
Checks if the last operation succeeded

Syntax: `&<trn-name>.Success()`

Return:
`Boolean`

## Fail
Checks if the last operation failed

Syntax: `&<trn-name>.Fail()`

Return:
`Boolean`

## GetMessages
Returns the collection of messages generated by the last operation

Syntax: `&<trn-name>.GetMessages()`

Return:
`Messages, GeneXus.Common` (built-in SDT)

---

# LEVEL METHODS

## GetByKey
Retrieves a specific line in the current record by indicating the key values defined in the Transaction level

Syntax: `&<trn-name>.LevelName.GetByKey(<level-key-1>, …, <level-key-N>)`

Return:
`<TransactionName>.<LevelName>` instance

## RemoveByKey
Removes a specific line in the current record by indicating the key values defined in the Transaction level

Syntax: `&<trn-name>.LevelName.RemoveByKey(<level-key-1>, …, <level-key-N>)`

Return:
`Boolean`

---

# EXAMPLES

## Example 1
Assuming the following Transaction is defined as Business Component:
~~~
Transaction Attraction
{
	AttractionId* [ DataType = 'Numeric(10.0)', Autonumber = 'True' ]
	AttractionType* [ DataType = 'AttractionType' ]
	AttractionName! [ DataType = 'VarChar(64)' ]
	CountryId []
	CountryName []

	Ticket
	{
		AttractionTicketId* [ DataType = 'Numeric(10.0)' ]
		AttractionTicketType* [ DataType = 'TicketType' ]
		AttractionTicketDescription [ DataType = 'VarChar(512)' ]
		AttractionTicketPrice [ DataType = 'Numeric(7.3)' ]
	}

	#Rules
		Error(!"Name cannot be empty") IF AttractionName.IsEmpty();
	#End
}
Properties
[
	'Business Component': 'True'
]
~~~

Create a new record:
~~~
&Attraction = new()
&Attraction.AttractionType = AttractionType.Museum
&Attraction.AttractionName = "Louvre Museum"
&Attraction.CountryId = 2

&Attraction.Save()
If &Attraction.Success()
	Commit
	msg(!"Success: Data added", status)
Else
	Rollback
	msg(Format(!"Error: %1", &Attraction.GetMessages().ToJson()), status)
EndIf
~~~

