Docs‎ > ‎CA Live API Creator‎ > ‎Rules‎ > ‎

Managed Parent

Managed Parent is a type of rule that automatically inserts a parent object if it does not already exist when a child is inserted.

This is done using the following algorithm:
  1. when a child object is inserted, the logic engine checks whether there are any Managed Parent rules for that table.
  2. if there are, then for each such rule:
    1. if the foreign key in the child object is null or incomplete, then nothing happens
    2. if the foreign key in the child object is complete, then the engine looks in the database to verify that the corresponding parent exists
    3. if the parent exists, then nothing happens
    4. if the parent does not exist, it is automatically created, and the attributes used in the foreign key are copied from the child into the new parent, thus establishing the relationship between the two
    5. the new parent is then optionally handed 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
    6. the new parent is then inserted into the database


If you provide some initialization code, you will have access to the usual variables (row, oldRow, and logicContext). The row and oldRow variables will refer to the child object being modified.

In addition, you will also have a variable called parent, which will contain the new parent about to be inserted. This allows you to modify this new parent object in whatever way you see fit, before it actually gets inserted.  For example, you may need to compute the values for required attributes.

Processed prior to row logic

Managed Parent occurs before row logic, as described in Generated Primary Key Handling.  That means that formulas are not yet operational, so cannot be used to compute foreign keys.


Common Use Case - Group By

A common Use Case is to use Managed Parents for grouping totals.  See the example for totaling sales by month and salesrep.  The Managed Parent is defined like this:

You can test this by posting the following to PartnerOrder, and verifying that rows are created in empsales:

{
    "isReady": false,
    "approving_officer": "G PO.1",
    "salesrep_name": "A. Lincoln",
    "customer": "Gloria's Garden",
    "LineItems": [
    {   "productName": "Hammer",
"quantityOrdered": 1
    }
    ]
}