Managed Parent Rule Type

Managed Parent Rule Type
Managed parent rules insert relationships between a child table and a parent table. 
Layer7 Live API Creator
 inserts the parent object if it does not already exist.
In this article:
Create a Managed Parent Rule
 Your API is connected to an active database that contains at least one table or view.
Follow these steps:
  1. In API Creator, in the Create section, click 
    If your API does not have existing rules, the Welcome to Rules page appears. If your API has existing rules, they are listed on the By entity tab by default.
  2. Click 
    Create a Rule
  3. Select the 
    Managed Parent
     rule type, the entity to which it applies, and then click 
    Create Rule
  4. Define the parameters of the rule, and then click 
    or click 
    Activate and Close
     to return to the list of rules:
    The title for the managed parent rule. The title for rules does not affect logic processing. To apply a default value, leave this field blank.
    Role to parent
    The name of the role to the parent.
    The topic associated with this rule
    The code for your managed parent rule.
    Select to activate the rule.
    The name of the managed parent rule. Leave this field blank for the system default.
The managed parent rule is created.
Managed Parent Rule Algorithm
When you insert a child object, managed parent rules insert a parent object if it does not already exist. This rule uses the following algorithm:
  1. You insert a child object. The logic engine checks whether there are any managed parent rules for that entity.
  2. If there are, then for each such rule the following occurs:
    • If the foreign key in the child object is null or incomplete, then nothing happens.
    • If the foreign key in the child object is complete, then the engine looks in the database to verify that the corresponding parent exists.
    • If the parent exists, then nothing happens.
    • If the parent does not exist, API Creator creates the parent and establishes the relationship between the two by copying the attributes used in the foreign key from the child into the new parent.
  3. Optionally, API Creator hands the new parent to your JavaScript code for further initialization.
    You do not need to write any code, unless you want to initialize the parent object beyond what is done automatically.
  4. API Creator inserts the new parent into the database.
 A common way to initialize the foreign key to a managed parent is by using a pre-insert event, for instance:
// Pre-insert event to populate the foreign key to a managed parent
var now = new Date();
row.ts_hour = now.getHours();
row.ts_day = now.getDate();
row.ts_month = now.getMonth() + 1;
row.ts_year = now.getYear();
This works fine, but 
Layer7 Live API Creator
 cannot initialize child columns using SQL snippets. For example, if the 
 column participates in a relationship between a parent and child that a managed parent rule established, the following code snippet fails:
row.ts_hour = SysUtility.sqlSnippetDeferred("hour(now())");
When you insert a child, and 
Layer7 Live API Creator
 is attempting to initialize some of its columns using SQL snippets, then 
Layer7 Live API Creator
 does not know the value of these code snippets until you insert the child. But you cannot insert a child because its (managed) parent does not exist yet, and 
Layer7 Live API Creator
 cannot insert the parent because it does not have the value for the primary key, which are the attributes used in the foreign key from the child. This is a circular dependency which 
Layer7 Live API Creator
 cannot resolve, hence this restriction.
You can access the 
, and 
 variables by providing initialization code. The 
 variables refer to the child object that 
Layer7 Live API Creator
 is modifying.
 variable contains the new parent object that 
Layer7 Live API Creator
 is going to insert. You can modify this new parent object before 
Layer7 Live API Creator
 inserts it through the 
 variable. For example, you might need to compute the values for required attributes.
You cannot access the new parent object through the 
Layer7 Live API Creator
 Processes Managed Parent Rules Prior to Row Logic
Managed parent occurs before row logic. You cannot compute foreign keys using formulas.
For more information about logic execution and generated primary key handling, see Logic Execution.
Common Use Case - Group By
You can store subtotals that are incrementally maintained as updates occur, using the 
 pattern. For example, you can track total sales for each sales rep each month. A common use case is to use managed parents for grouping totals. The managed parent is defined as follows.
For more information about viewing an example of the managed parent rule type, see Group by Rollup Example.
In the 
 API, you can test this pattern.
by posting the following to 
 and verifying that rows are created in 
Follow these steps:
  1. Open the 
  2. In the Tools section, click 
    REST Lab
  3. For 
    , select 
     as the endpoint on which you want to operate and 
     as the named resource.
  4. Copy and paste the following JSON into the Request body:
    "isReady": false,
    "approving_officer": "G PO.1",
    "salesrep_name": "A. Lincoln",
    "customer": "Gloria's Garden",
    "LineItems": [
    "productName": "Hammer",
    "quantityOrdered": 1
  5. Click 
  6. Verify that rows are created in the