Parent / Child Forms

You can build pretty complex applications based around a single form. Before long, though, you will find yourself wanting to associate form instances together. More often than not, those form instances will be different form types as well.

 

True Parent Child

You may build a Form that manages information about a Customer. You then have another Form that captures Order Details. There can, of course be any number of Orders. The key point here is that you want to associate all of the Orders with the Customer. As such, the Customer Form Instance can be considered the Parent and the Order Details Instances are the children.

The normal way to represent the relationship between the Parent and the Children would be to use a Table object. This allows parent/child (one to many) relationships to be listed. The user can then select a row in the Table and press a button to launch the child form instance.

 

Associated / Linked Form Instances

There may be relationships between different form instances that you might not think of as parent / child relationships, but just some form of association between them. For example, let's assume that you have a form that manages product defects. You may want to associate a specific defect with another defect in order to indicate that they are somehow related.

You can take this a step further by relating several defects to the defect in question. This would require the use of a table object again.

In this example, one defect does not "own" the others. They are merely related in some way.

 

Instance IDs and Parent Instances IDs

Each form instances has a unique id called the Form Instance ID. This is an unpleasant looking number that is guaranteed to be unique across the entire system. If a form instance was created by another form instance (see below for information on how this can be done) then that instance will have its Parent Instance ID automatically set.

Both the Instance ID and the Parent Instance ID can be inspected using the Special list in the PerfectForms Designer.

 

 

How to Create a Child Instance

There are three typical ways to create a child instance

  • Show Form Button object
  • Show Form button in the Table Object
  • Show Form Behavior Command

 

Each of these have the following property bar options that allow you to specify exactly what the relationship is between the current form instance and the other form instance that is about to be created. Note that the first two options do not set up a parent/child relationship, they just create a new form instance for the specified form type

This image shows the options available for the Show Form object.

... and this one shows the equivalent options for the Show Form behavior command. You will notice that for the Show Form behavior command you have the extra parameter that specifies the field that contains the Form Instance ID. If a new instance is created (“New Instance” or “New Child Instance” options), the new Form Instance ID is written into this field. If an existing instance is opened (“Existing Instance” or “Single Child Instance” options), then the Form Instance ID is taken from this field. The field is ignored for the “Parent Insance” option.orm Instance ID. 

 

 

  • New Instance - this creates a brand new instance of the specified form type without setting the Parent Instance ID. This allows you to have a button that simply fires up a new form instance without anything special otherwise.
  • Existing Instance - this opens up an existing form instance in a new browser tab or window. You need to specify which helper form field contains the instance id. This presupposes that you are managing the related (child) instance id yourself by storing it somewhere inside your form instance. This instance ID can always be looked up using the Connect or Connect and Search commands so you can build these relationships dynamically.
  • New Child Instance - this will create a new child instance, each and every time that the button is pressed (or the behavior command executes). The Parent Instance ID will be set in the newly created child instance but (if you are using a button command) the button property will not contain a child instance id, as is the case with the Single Child Instance.
  • Single Child Instance - this will create a new child instance just once. Once this child instance has been created, the child instance id is stored either as a property of the button, if you are using the button object, or in the form field specified in the property bar if you are using the Show Form behavior command.
  • Existing Child Instance - this will display a pop-up list of all child instances of the current form instance id. The form respondent can then select from that list which will open up the child instance in a new browser window/tab.
  • Parent Instance - this will open up the parent instance of the currently loaded form instance id.

 

“New Window” checkbox: You can choose whether you want to display the other Instance in the same window (the current Instance will close without saving) or in a new Browser window/tab.


Using Form Lookups to Populate a Table

One very important trick is to use a combination of a Connect Command and a Form Connection definition to populate a table object. This approach also gives you the flexibility to make associations from a child form instance to its parent rather than the other way round. For example, assume you have an Order Details form and you want to associate it with a Company form instance. If you are in the Order Details form instance you would perform a lookup into the Company form to locate the company it belongs to. You can then set a helper field (a field that is often hidden that is used to store data for general purposes) with the value of the Instance ID of the Company selected.

Let's assume that you have linked several Order Details instances to the same company. Now let's imagine we are in the Company form instance and we want to list all Order Detail instances that are linked to it. We would use a table object for this and then use the Connect command to populate all rows of the table by performing a lookup into the Order Details form where the Helper field we used contains the same ID as the Company instance currently being looked at. This is simply done by passing in the Instance ID of the open form to the Send parameters of the Connect Command. The Connection Action should be set up to receive this parameter and compare it to the helper field in the Order Details form.

I am going to assume that you already know how to perform lookups using the Connect command and how to set up a Form Connection & Action. If you are not, then please familiarize yourself with this first.

 

How are Form Instance IDs and Form Instance Numbers created?

It is very important for you to realize exactly when a Form gets its Instance ID. When a brand new, empty form is created, it is assigned Instance ID but is not yet assigned its sequential Instance Number (that starts at one and counts from there). Only once the form instance has been submitted is the Instance ID properly accessible and the Instance Number is assigned.

 

Submission of Parent / Child Instances

When a child instance is submitted before its parent instance has been submitted for the first time, there are 2 possible options. You can select them in the child form in the “Submit” button and “Submit” behaviour command properties, under “Advanced”:


“[x] Submit as child even if parent is not submitted”.

If the option is selected, and the child is submitted, it will be written into the database straight away, even if the parent instance has not been submitted for the first time. If the parent instance does not get submitted at all, the child instance will exist normally but its parent relationship is broken, since the parent instance does not exist in the database.

If the option is not selected, and the child is submitted, it will not be written into the database until the parent has been submitted at least once. If the parent has already been submitted before, it is written into the database straight away. This option allows you to prevent “orphaned” child instances.

Note: If you are planning to use Form Connections to do look-ups between Parent and Child instance, you need to make sure that the looked up instance is submitted. You cannot look up another instance if it is not submitted yet. If necessary, use the “Submit” behaviour command with the “Re-open same instance” option to make an “invisible submit” of the parent befofe you open a child instance.


 

Set / Get Field from Other Instance Behavior Commands

One feature that you may find useful is the ability to Get or Set field data in another form instance. These commands let you get or set data from a specific field in another instance by referencing the form instance id of the other instance. This instance id can be taken from a form field. If you have a relationship defined using the Show Form button, then you can reference this button object and PerfectForms will return the Form Instance associated with that button. This only applies if you used the button with Single child instance option.