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

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

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

2 Likes

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

Something like this might work:

The script is as follows:

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

1 Like

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?

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:

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')
}
2 Likes

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

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