Creating Header Entity from Line Entity - arp6333/xplusplus GitHub Wiki
Allows avoiding use of composite entities (ex. want to import data into composite entity without using XML)
- Create extension and code extension of the Line entity, and extension of the Line Staging table
- I chose to overwrite the validateField method in this example as to create the Header when the JournalBatchNumber is validated, but the following code can be put in a variety of methods based on use case (see Data Entity Method Call Sequence for other usable methods)
/// <summary>
/// Overwrite the validateField method to create the CustomerPaymentJournalHeaderEntity record.
/// This method is called for all fields on the Entity to make sure they are valid.
/// </summary>
/// <param name = "fieldId">Field to validate.</param>
/// <returns>True if successful; false if not.</returns>
public boolean validateField(int fieldId)
{
// Switch on the field to validate
switch (fieldId)
{
// If the field is JournalBatchNum:
case fieldNum(CustomerPaymentJournalLineEntity, JournalBatchNumber):
CustomerPaymentJournalHeaderEntity header;
// Check if the Header record exists already
// We are using the Header primary keys to check this
select firstonly header
where header.dataAreaId == this.dataAreaId &&
header.JournalBatchNumber == this.JournalBatchNumber;
if (!header.RecId)
{
// Create the Header record
// Entity records can be created and inserted just like regular tables
ttsbegin;
header.clear();
header.JournalName = this.JournalName;
header.Description = this.JournalDescription;
header.JournalBatchNumber = this.JournalBatchNumber;
// Double check the line is valid to write to the table
if (header.validateWrite())
{
header.insert();
}
ttscommit;
}
break;
}
// Continue chain of command call
return next validateField(fieldId);;
}