How to create a duplicate of a Thing, including all of it's fields and linked Things

Is it possible to create a workflow that creates a duplicate of a Thing, and also all of it’s linked Things?

For example - my two data types are as follows:

Template.
Template has the following fields:

  • Name [type: text]
  • Message [type: text]
  • Resource [type: Resource]

Resource
Resource has the following fields:

  • Title [type: text]
  • Image [type: image file]
  • Description [type: text]

When the user clicks a button, I want to automatically create a duplicate of the Template (let’s call this new template “Template 2”).

Template 2 should not reference the original Resource that is linked to the Template. Rather, Template 2 should link to a duplicated copy of the original Resource (let’s call this “Resource 2”).

Template 2 should have the following attributes:

  • Name = copy of the text from Template 1
  • Message = copy of the text from Template 1
  • Resource = Resource 2

Hi there, @twillcs… if I understand your post correctly, what you have described is definitely possible.

When the user clicks the button…

  1. Create a new thing (Template 2), and set the new thing’s Name and Message fields equal to the associated fields in the original Template.

  2. In the next step of the workflow, create a new thing (Resource 2), and set the new thing’s fields equal to the associated fields in the original Resource.

  3. In the final step of the workflow, set the Resource field of the thing created in step 1 (Template 2) equal to the thing created in step 2 (Resource 2).

Any of that make sense? Hope it helps.

Best…
Mike

1 Like

Thanks for the reply! This works well when there are a small number of resources. Do you know if there’s a simpler way of doing this when there are lots of resources?

Some of my templates have 50+ resources. My concern would be that this method could become hard to maintain as resources are added/removed from the template, because I would need to remember to update the workflow accordingly.

Ah, that’s a pretty important detail. :slight_smile:

With that additional piece of information, I am thinking you’d want to go the route of a backend workflow that duplicates a resource and then calls itself to keep duplicating resources until there are no more resources to duplicate on the associated template.

1 Like

Thanks, yes I think you’re right. I’m not so familiar with backend workflows. I’ve created an API endpoint workflow that can create the new resources – could you say more about how I would set that to run until it has duplicated all resources on the template? How do you set up that loop? Thanks!

You will need a final step in the backend workflow that runs the same backend workflow again only when a particular condition is true. In your case, I’m thinking if you are adding the newly-created resources to the new template as part of the backend workflow, the only when condition on the last step could be something along the lines of only when a count of the resources on the new template is less than a count of the resources on the template that is being duplicated. So, if there are 50 resources on the original template, the backend workflow is creating new resources and adding them to the new template until the new template has 50 resources because at that point, the count of resources on the new template is not less than the count of resources on the template that is being duplicated, and the workflow will stop running. Make sense?

1 Like

Makes perfect sense. Thanks for the detailed reply!

1 Like

This topic was automatically closed after 70 days. New replies are no longer allowed.