Automation: Create a new Page from Template as a Child Page of the Publish Page Trigger
Asked by Joel Scott:

I've spent several hours on this so I thought I would take a step back and ask for help.

On Confluence, each product our org works on has the same child page structure. For the sake of creating an example let's say this is the structure:

A - Company Products

A.1 - Perfect Product Name

A.1.1 - Perfect Product Name Requirements Document

A.1.2 - Perfect Product Name Creative Assets

A.1.2 - Perfect Product Name Guides

What I've been trying to do is create an automation, so that if I create a child page to "A - Company Products", for example the "A.1 - Perfect Product Name" page in the above example. That A.1.1, A.1.2, and A.1.3 will all be automaticallhy created from template as child pages to the newly created "A.1 - Perfect Product Name".

I've tried everything I can think of including trying to use confluence APIs with the "Send web request" automation, and the best I can do is create a generic file from template in a folder who's name I know, that already exists. Could anyone help me use Confluence automations to automatically create these child pages?

Any help would be greatly appreciated :)


Answers:
author: Joel Scott
body: Aright so I managed to eventually figure this out but it was not a simple solution. I decided to trigger the parent page creation with the "When: Manually Triggered" rule. The child pages are then automatically triggered with the "When: Page Labeled" rule. I had to create a separate automation for each child page I wanted to add. It's a painful solution but it does the trick.

Unfortunately, I can't find a nice way to share the automation here in a simple way, but I'll copy screen shots of the automations below in the off chance this can help someone else. Here are the steps I followed:

1. Create a personal API key according to <https://community.atlassian.com/t5/Jira-Software-articles/Automation-for-Jira-Send-web-request-using-Jira-REST-API/ba-p/1443828>
2. Create parent page with "When: Manually Triggered" rule:
   1. ![Screenshot 2024-01-26 at 1.01.26?AM.png](https://community.atlassian.com/t5/image/serverpage/image-id/302650i55529186530B0ECE/image-size/large?v=v2&px=999 "Screenshot 2024-01-26 at 1.01.26?AM.png")
3. Creat each child page with the "When: Page Labeled" rule. Make sure "Check to allow other rule actions to trigger this rule. Only enable this if you need this rule to execute in response to another rule." is checked:  
   1. ![Screenshot 2024-01-26 at 1.01.53?AM.png](https://community.atlassian.com/t5/image/serverpage/image-id/302648i8577552673D096A4/image-size/large?v=v2&px=999 "Screenshot 2024-01-26 at 1.01.53?AM.png")


author: Benno Baumgartner
body: Hi Joel,

You might be able to built something like this with our addon [JavaScript Button for Confluence](https://marketplace.atlassian.com/apps/1231339/javascript-button-for-confluence) . Add a JavaScript Button to the very top of your Product Page and a field for the products name, like so:

![Screenshot 2023-11-12 at 19.16.25.png](https://community.atlassian.com/t5/image/serverpage/image-id/291283i604D769C9837233E/image-size/large?v=v2&px=999 "Screenshot 2023-11-12 at 19.16.25.png")

Use the following code:

```
function createProduct(page, productName) {
  const body = JSON.parse(page.body.atlas_doc_format.value);
  const extensionKey = body.content[0].attrs.extensionKey;
  
  return {
    parentId: page.id,
    spaceId: page.spaceId,
    title: productName,
    body: {
      representation: "atlas_doc_format",
      value: "{\"type\":\"doc\",\"content\":[{\"type\":\"extension\",\"attrs\":{\"layout\":\"default\",\"extensionType\":\"com.atlassian.confluence.macro.core\",\"extensionKey\":\"children\",\"parameters\":{\"macroParams\":{},\"macroMetadata\":{\"macroId\":{\"value\":\"47ab8f0b-766b-46f6-af0b-6c377c1f699d\"},\"schemaVersion\":{\"value\":\"2\"},\"title\":\"Children Display\"}},\"localId\":\"5ab88798-faf9-47ff-84b6-79f7e4a95c2c\"}},{\"type\":\"extension\",\"attrs\":{\"layout\":\"default\",\"extensionType\":\"com.atlassian.ecosystem\",\"extensionKey\":\"" + extensionKey + "\",\"text\":\"JavaScript Button (Staging)\",\"parameters\":{\"guestParams\":{\"resultLocation\":\"$page\",\"code\":\"async function createChildren(page) {\\n const productId = page.title.substring(0, 3);\\n const productName = page.title.substring(6);\\n return [{\\n parentId: page.id,\\n spaceId: page.spaceId,\\n title: productId + \\\".1 - \\\" + productName + \\\" Requirements Document\\\",\\n body: {\\n representation: \\\"atlas_doc_format\\\",\\n value: '{\\\"type\\\":\\\"doc\\\",\\\"content\\\":[{\\\"type\\\":\\\"paragraph\\\",\\\"content\\\":[{\\\"text\\\":\\\"Add requirements.\\\",\\\"type\\\":\\\"text\\\"}]}],\\\"version\\\":1}',\\n }\\n },{\\n parentId: page.id,\\n spaceId: page.spaceId,\\n title: productId + \\\".2 - \\\" + productName + \\\" Creative Assets\\\",\\n body: {\\n representation: \\\"atlas_doc_format\\\",\\n value: '{\\\"type\\\":\\\"doc\\\",\\\"content\\\":[{\\\"type\\\":\\\"paragraph\\\",\\\"content\\\":[{\\\"text\\\":\\\"Add creative assets.\\\",\\\"type\\\":\\\"text\\\"}]}],\\\"version\\\":1}',\\n }\\n },{\\n parentId: page.id,\\n spaceId: page.spaceId,\\n title: productId + \\\".3 - \\\" + productName + \\\" Guides\\\",\\n body: {\\n representation: \\\"atlas_doc_format\\\",\\n value: '{\\\"type\\\":\\\"doc\\\",\\\"content\\\":[{\\\"type\\\":\\\"paragraph\\\",\\\"content\\\":[{\\\"text\\\":\\\"Add guides.\\\",\\\"type\\\":\\\"text\\\"}]}],\\\"version\\\":1}',\\n }\\n }]\\n}\",\"functionName\":\"createChildren\",\"cqlQuery\":\"\",\"caption\":\"Create Child Pages\",\"parameters\":\"$page\"},\"localId\":\"4839322d-e0c3-4ff9-9901-e14733e0bfab\",\"extensionId\":\"ari:cloud:ecosystem::extension/" + extensionKey + "\",\"extensionTitle\":\"JavaScript Button (Staging)\"},\"localId\":\"19454fdd-7ab4-40c9-882a-f09b95a2f549\"}},{\"type\":\"paragraph\"}],\"version\":1}"
    }  
  } 
}
```

Regards, Benno


Tags: automation

cloud

confluence-cloud