# \[Quick guide\] Using automation to populate template in child from parent

**URL:** <https://community.fibery.io/t/quick-guide-using-automation-to-populate-template-in-child-from-parent/2146>\
**Category:** API & Programming\
**Created:** [October 25, 2021, 10:59pm UTC](https://community.fibery.io/t/quick-guide-using-automation-to-populate-template-in-child-from-parent/2146 "2021-10-25T22:59:59Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Haslien](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/haslien/32/493_2.png) [@Haslien](https://community.fibery.io/u/Haslien)\
**Post date:** [October 25, 2021, 10:59pm UTC](https://community.fibery.io/t/quick-guide-using-automation-to-populate-template-in-child-from-parent/2146/1 "2021-10-25T22:59:59Z")

</div>

I am sharing code for using Automations to populate a rich text field from a linked parent.  
Scroll to the very bottom for another video that shows the entire process.

_Parent Type_ has a Rich Text field named _My Template_.  
_Child type_ has the default Rich Text field called _Description_.

**Goal:** Copy the content of _Parent Type/My Template_ into _Child type/Description_ when I create a new _Child type_ that has a link to _Parent Type_.

**Video demo:**

[https://i.thevirt.us/10/Q9T1o.mp4](https://i.thevirt.us/10/Q9T1o.mp4)

## Setup

This is the relationship I have:

 ![image](https://us1.discourse-cdn.com/flex020/uploads/fibery/original/2X/0/0bf97d17d2e8696a7e1cdb535ff02abf5d4bb932.png)

**Note:** I won’t go into setting all the rules you need for this to work from _any_ entrypoint, because you can create _Child type_ that isn’t instantly linked to _Parent Type_. Maybe when I have more time, or someone in the comments have that to share.

This is post is likely to be outdated if we get some nice templates built-in in the future, and I really hope that’s the case 😀

## Recreating layout

Here’s the rule I created on _Child type_:

 ![image](https://us1.discourse-cdn.com/flex020/uploads/fibery/original/2X/f/f302df22a3090034f90c815d2a0d91cd0871b40a.png)

Here’s how the _Child type_ looks like:

 ![image](https://us1.discourse-cdn.com/flex020/uploads/fibery/original/2X/7/703059b14b713a2955dbb3092c08e1aa301f6734.png)

And the layout of the _Parent Type_

 ![image](https://us1.discourse-cdn.com/flex020/uploads/fibery/original/2X/7/7e5f489d8194c18c58d6caa6677ca02167abeb56.png)

## Replacing variables

There are four important texts you need and replace in the code with your own ones:

1. The _ **name of the app** _, goes into `foreign.appName`
2. The _ **name of the parent type** _ (go into it and get the correct capitalizing of the type), goes into `foreign.typeName`
3. The child’s _ **relation field name** _ , which in this case I renamed for clarity to “The Parent”, goes into `localField`
4. The _ **name of field of your template** _, inside _Parent Type_, goes into `foreign.richTextFieldName`

 ![image](https://us1.discourse-cdn.com/flex020/uploads/fibery/original/2X/8/8b641b98b8af01b619ac94986d0e72916ee374c4.png)

Put this code inside the rule. You can choose to use Append or Prepend instead of overwrite of course.

```auto
<%
const localField = "LOCAL_FIELD";
const foreign = {
  appName: "APP_NAME",
  typeName: "PARENT_TYPE_NAME",
  richTextFieldName: "SOURCE_TEMPLATE_FIELD_NAME"
}

const fibery = context.getService('fibery');
const populatedEntity = await fibery.getEntityById(Entity.Type, Entity.Id, [localField]);
const populatedParent = await fibery.getEntityById(`${foreign.appName}/${foreign.typeName}`, populatedEntity[localField].Id, [foreign.richTextFieldName]);
const docContent = await fibery.getDocumentContent(populatedParent[foreign.richTextFieldName].Secret, "md");
%>
<%= docContent %>

```

## Full video of setup

[https://i.thevirt.us/10/p3Q3w.mp4](https://i.thevirt.us/10/p3Q3w.mp4)

## Using multiple templates

You can compose easily by breaking templates up so you can mix them by “loading in” each one of them. Simply make the automation also clear the `Use template from` field, and have it use `Append` instead of `Overwrite`.

That way you can just select to load Template A, then select to load Template C, then Template D.

Those of you that know everything is an entity could also modify the code a bit, and potentially [use the Multi Select to kind of have your selected templates active](https://i.thevirt.us/10/H4U8o.mp4).

## Preventing overwrites once a template is loaded

Fibery is flexible, there’s multiple ways of doing it depending on how you want it to work. [A simple way](https://i.thevirt.us/10/Y0F1i.mp4) is to add a checkbox that gets checked once a template is loaded. In the rule, simply add a condition that the “template used” checkbox is unticket.
