Select your user interface:

Exercise 9 (Continued): Configure a New Batch and Save Data to the Batch

Step 1 -  Review the check constraint on USR_BATCHFOODITEM.

When this table spec was loaded into the Infinity catalog, a table was created with a corresponding database constraint to enforce the TextField's Required="true".

Step 2 -  View the FORMDEFINITIONXML column for the batch.

If we look for the appropriate metadata within the BATCH table, we can see that the FORMDEFINITIONXML column contains the form field options for the batch that was created from the template.

Step 3 -  Create a batch.

Exceptions from Database Constraints

After we hurdle any form field metadata errors that can prevent us from saving data to batch tables, the platform attempts to save the data to batch tables in the database. The platform calls upon the save implementations within the Add Batch Row Data Form and Edit Batch Row Data Form. Within the BatchTypeSpec, you will find references to the AddRowDataFormTemplate and EditRowDataFormTemplate XML elements that point to the ID attribute of an Add Data Form and an Edit Data Form, respectively. These data forms add rows to the batch and edit rows within the batch from the batch user interface grid.

When the save to the batch occurs, if a row was added to the batch user interface grid, then a call is made to the save implementation of the Add Batch Row Data Form. If a row was edited within the batch user interface grid, then a call is made to the save implementation of the Edit Batch Row Data Form. If no rows were modified or added, no calls are made to save data in the database.

Batch staging tables may contain constraints such as unique, required, foreign key, and format. The degree to which constraints are applied to these tables is up to the discretion of the software developer who should consider the source of the data that feeds these tables. As an example, below we see the TableSpec that defines our USR_BATCHFOODITEMADD table. The yellow highlights define the metadata that could cause issues when we try save a record to the batch.

<TableSpec 
    xmlns:common="bb_appfx_commontypes"
    ID="a613c7a0-20db-4618-9112-17b5d6f59113"
    Name="Food Item Add Batch"
    Description="Stores food item add batch information."
    Author="Technical Training"
    Tablename="USR_BATCHFOODITEMADD"
    IsBuiltIn="false"
    xmlns="bb_appfx_table">
  <Fields>
		<ForeignKeyField Name="BATCHID" Required="true" ForeignTable="BATCH" OnDelete="CascadeDelete" />
		<SequenceField Name="SEQUENCE" />
		<TextField Name="NAME" Length="100" Required="true"/>
		<MemoField Name="DESCRIPTION"/>
		<MoneyField Name="CURRENTCOST"/>
		<NumberField Name="LOWINVENTORYTHRESHOLD" Type="smallint" />
		<DecimalField Description="The weight of a food item." Name="WEIGHT" Precision="10" Scale="2" />
	</Fields>

  <Indexes>
    <Index IsUnique="true">
      <IndexFields>
        <IndexField Name="BATCHID"/>
        <IndexField Name="NAME"/>
      </IndexFields>
    </Index>
  </Indexes>

In the TableSpec example above, we can expect to receive a database exception if we attempt to add a food item without a name. This constraint is defined by the Required ="true" attribute on the NAMETextField. We can also expect to receive a database exception if we attempt to add the same food item name to the same batch. This unique index constraint is defined by the composite unique index that contains the BATCHID and NAME fields. So, we have included the appropriate metadata within our ExpectedDBExceptions sections of both the Add Batch Row Data Form and the Edit Batch Row Data Form.

<common:ExpectedDBExceptions>
  <common:Constraints>
<common:Constraint Name="CK_USR_BATCHFOODITEMADD_NAME" Field="NAME" Type="Required" />
<common:Constraint Name="UIX_USR_BATCHFOODITEMADD_BATCHID_NAME" Field="NAME"    Type="Unique" />
  </common:Constraints>
<common:ExpectedDBExceptions>

Step 4 -  Test the check constraint on the batch table.

Let's conduct a test and see what happens when we attempt to add two food items with the same name for the same Batch ID into the batch table.

Add two "Coconut" food items into the batch UI grid and save the batch.

Figure: An expected database exception is handled within the user interface.

As you can see, we have violated the UIX_USR_BATCHFOODITEMADD_BATCHID_NAME constraint on the database table that was defined within our TableSpec. The ExpectedDBException in the Add Batch Row Data Form caught the exception from the database and presented the gracefully formatted error message to the appropriate row. The end result is the duplicate "Coconut" row was not added to the database.

Exceptions from RAISERROR() within Save Implementations

Custom errors raised from either the Add Batch Row Data Form or the Edit Batch Row Data Form save implementation's stored procedures via RAISERROR() will cause errors when you attempt to save data into a batch table.

Step 5 -  Use RAISERROR() within the Add batch Row Data Form.

For example, within your Add Batch Row Data Form, before you attempt to add a row into the table, you can check to see if the description is equal to the name. If so, then the description is not descriptive. We use RAISERROR() to raise a custom exception from the stored procedure.