2.8. Step 8 - Add a new customer

We also want to add new customers to the list. The "add customer" case is very similar to the "edit customer" case.

Let's copy the customer_edit.ui to customer_add.ui. We remove the 'action' and 'answer' on from the form itself as we don't want to read anything when we create a new customer. But of course we could execute here a "Get new customer initial data" request which initializes certain values in the form.

We change the "Save" button and rename it to "Add". We also change 'CustomerUpdate' to 'CustomerCreate' in the 'action' attribute and we remove the id property as this one is automatically choosen by the sequence in the database:

CustomerCreate customer {
    name {{main.name}};
    address {{main.address}}
}
		

We also introduce a new element here, the 'initalFocus' property. We set it on the 'name' QLineWidget, so that it gets the initial keyboard focus when the form is loaded:

In the customers.ui form we have to add a 'Add' button which has one property 'form' with value 'customer_add':

Now for the server side. We add a new mapping for customer creation in tutorial.dmap:

COMMAND CustomerCreate CALL CreateCustomer;
	

We also have to add the form 'CustomerCreate' to Customer.sfrm:

FORM CustomerCreate
    -root customer
{
    name string
    address string
}

	

This is the same as the 'CustomerUpdate' form with the exception that we don't accept an 'id' attribute to be passed to the server.

Last we add a 'CreateCustomer' transaction function:

TRANSACTION CreateCustomer
BEGIN
    DO INSERT INTO Customer( name, address )
        VALUES( $(name), $(address) );
END
	

When we restart the server and client we see the following request being passed to the wolframe server:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE customer SYSTEM 'CustomerCreate'>
<customer>
    <name>New Customer</name>
    <address>New Location</address>
</customer>