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

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:

Setup

This is the relationship I have:
image

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 :grinning:

Recreating layout

Here’s the rule I created on Child type:

Here’s how the Child type looks like:

And the layout of the Parent Type

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

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

<%
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

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.

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 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.

10 Likes