So far, you have seen examples of adding and updating records, where the primary key value is supplied in input. In some cases, you may want your Rulesheet to define a primary key value for a new record and then add the record to the database. To do this, you use the .new entity operator in a rule action. You can access this operator from Entity/Association Operators > Entity in the Rule Operators view.

media/image56.png

The syntax of the new operator is:

<Entity>.new[attribute1=value, attribute2=value,… association1.attribute1=value.

Let’s model a rule that uses the new operator. In this example, assume that you need to model a rule that creates a new FlightPlan instance where an Aircraft instance has a maxCargoWeight between 100000 and 200000. If so, an Aircraft instance is assigned to the FlightPlan instance.

Create a Rulesheet named NewRecord.ers and model a rule as shown here:

media/image57.png

The range in column 1 is written as the low limit, two dots, then the high limit. The new operator is used in the action row expression: FlightPlan.new[flightNumber=222,aircraft=Aircraft]

A flightNumber (222 in this example) must be supplied since flightNumber is the primary key for FlightPlan. The expression aircraft=Aircraft in the action row associates the Aircraft instance in input with the FlightPlan entity that will be created.

Let’s test this rule. Create a Ruletest named NewRecord.ert that uses NewRecord.ers as its test subject. Configure the Ruletest to write to the database by selecting Ruletest > Testsheet > Database Access > Read/Update. Define the following input and run the test (make sure that you provide a value such as N444D for tailNumber, since tailNumber is the primary key for Aircraft):

media/image58.png

As you can see, a new FlightPlan instance is created with the flightNumber 222 and the Aircraft instance is assigned to it.

Let’s look at the database tables. Right-click dbo.Aircraft in the Transportation database and select Select Top 1000 Rows. You should see the following results:

media/image59.png

A new Aircraft record has been added. Now, right-click dbo.FlightPlan and select Select Top 1000 Rows. You should see the following results:

media/image60.png

A new FlightPlan record has been added with the flightNumber 222. It is also associated with an Aircraft bearing the tailNumber N444D.