# Is there a way to use a button to copy field values to a document?

**URL:** <https://community.fibery.io/t/is-there-a-way-to-use-a-button-to-copy-field-values-to-a-document/2069>\
**Category:** Get Help\
**Created:** [September 28, 2021, 1:30pm UTC](https://community.fibery.io/t/is-there-a-way-to-use-a-button-to-copy-field-values-to-a-document/2069 "2021-09-28T13:30:59Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![v3j](https://avatars.discourse-cdn.com/v4/letter/v/f07891/32.png) [@v3j](https://community.fibery.io/u/v3j)\
**Post date:** [September 28, 2021, 1:30pm UTC](https://community.fibery.io/t/is-there-a-way-to-use-a-button-to-copy-field-values-to-a-document/2069/1 "2021-09-28T13:30:59Z")

</div>

hi there!

Use case: I want to publicly share an entity with clients but I’m not keen on sharing some of the fields. So I’m thinking of using a document instead which will contain certain fields and field values after hitting the button. 🙂

---

<div class="post-metadata">

**Author:** ![mdubakov](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/mdubakov/32/10_2.png) [@mdubakov](https://community.fibery.io/u/mdubakov)\
**Post date:** [September 28, 2021, 4:31pm UTC](https://community.fibery.io/t/is-there-a-way-to-use-a-button-to-copy-field-values-to-a-document/2069/2 "2021-09-28T16:31:36Z")

</div>

You can do that using Markdown templates. Here is the explanation

> **[Markdown Templates](https://help.fibery.io/en/articles/5170015-markdown-templates)**
>
> Markdown Templates for Action Buttons

---

<div class="post-metadata">

**Author:** ![Chr1sG](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/chr1sg/32/3941_2.png) [@Chr1sG](https://community.fibery.io/u/Chr1sG)\
**Post date:** [September 28, 2021, 6:19pm UTC](https://community.fibery.io/t/is-there-a-way-to-use-a-button-to-copy-field-values-to-a-document/2069/3 "2021-09-28T18:19:47Z")

</div>

I think the original request is to copy field data into document views, not rich-text fields.  
I think it can be done using the api - `fibery.setDocumentContent(secret: string, content: string, format: string)` - provided the secret of the destination document is known (and @v3j knows how to do this, I believe).

---

<div class="post-metadata">

**Author:** ![Chr1sG](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/chr1sg/32/3941_2.png) [@Chr1sG](https://community.fibery.io/u/Chr1sG)\
**Post date:** [September 28, 2021, 6:36pm UTC](https://community.fibery.io/t/is-there-a-way-to-use-a-button-to-copy-field-values-to-a-document/2069/4 "2021-09-28T18:36:59Z")

</div>

Something like this might work:

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

The script is as follows:

```auto
const fibery = context.getService('fibery');

for (const entity of args.currentEntities) {
    const entityWithExtraFields = await fibery.getEntityById(entity.type, entity.id, ['Number field','Date field','Text field'])
    const text = await fibery.setDocumentContent('39520f20-905a-4c00-ab56-8a129d5a1f67', entityWithExtraFields['Text field'] + entityWithExtraFields['Number field'] , 'html')
}

```

but you’ll need to replace the secret with the relevant one for your destination document  
(the secret can be obtained by examining the payload of the network request in the browser).

---

<div class="post-metadata">

**Author:** ![v3j](https://avatars.discourse-cdn.com/v4/letter/v/f07891/32.png) [@v3j](https://community.fibery.io/u/v3j)\
**Post date:** [September 28, 2021, 10:18pm UTC](https://community.fibery.io/t/is-there-a-way-to-use-a-button-to-copy-field-values-to-a-document/2069/5 "2021-09-28T22:18:16Z")

</div>

Thanks a lot @mdubakov and @Chr1sG !

I tried it out and if I’m not mistaken, this only works for text, number, or date fields?

I tried it with Relation/Workflow/Lookup fields and it gives me [object Object]

* * *

Also, is it possible to append rather than overwrite whenever I hit the button?

---

<div class="post-metadata">

**Author:** ![Chr1sG](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/chr1sg/32/3941_2.png) [@Chr1sG](https://community.fibery.io/u/Chr1sG)\
**Post date:** [September 29, 2021, 7:25am UTC](https://community.fibery.io/t/is-there-a-way-to-use-a-button-to-copy-field-values-to-a-document/2069/6 "2021-09-29T07:25:26Z")

</div>

If you’re getting an object, then it probably means that the field you are trying to access is a relation, rather than a simple field.  
In these cases, you need to lookup the info of the related item, with something like this:

```auto
const fibery = context.getService('fibery');

for (const entity of args.currentEntities) {
    const entityWithExtraFields = await fibery.getEntityById(entity.type, entity.id, ['Text field', 'Relation field'])
    const relatedEntity = await fibery.getEntityById('App_name/Type_name', entityWithExtraFields['Relation field']['Id'], ['Name' ,'Number field', 'Date field', 'Text field'])
    const text = await fibery.setDocumentContent('39520f20-905a-4c00-ab56-8a129d5a1f67', entityWithExtraFields['Text field'] + relatedEntity['Name'], 'html')
}

```

---

<div class="post-metadata">

**Author:** ![v3j](https://avatars.discourse-cdn.com/v4/letter/v/f07891/32.png) [@v3j](https://community.fibery.io/u/v3j)\
**Post date:** [October 18, 2021, 7:31pm UTC](https://community.fibery.io/t/is-there-a-way-to-use-a-button-to-copy-field-values-to-a-document/2069/7 "2021-10-18T19:31:03Z")

</div>

hi @Chr1sG ! can the script be tweaked so that it will append/prepend to the document rather than overwrite?

---

<div class="post-metadata">

**Author:** ![Chr1sG](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/chr1sg/32/3941_2.png) [@Chr1sG](https://community.fibery.io/u/Chr1sG)\
**Post date:** [October 18, 2021, 7:46pm UTC](https://community.fibery.io/t/is-there-a-way-to-use-a-button-to-copy-field-values-to-a-document/2069/8 "2021-10-18T19:46:04Z")

</div>

You’d have to read the document content first, and then join it with the text to be added (in the right order) before writing the result back again.

Use  
`getDocumentContent`  
🙂
