Use case:
Invoices come in via email.
Within the Email/Message database there is a button “Create invoice”, which prompts the user for some input (invoice date, number, amount, and so on) and then creates an entity in the Finance/Invoice database.
From my understanding of the backend, there is a separate storage for files, and each database utilizing files does so by adding an “entity collection” field, which basically references to files via file’s ID/secret.
Now as we can’t leave the server environment in automations, I guess what should be possible is to:
- add an entity to the collection field (files)
→ with the same values for ID, Secret, name, and content-type of an already existing file.
This would basically mean the file would show in multiple entities, but if deleted in ANY place, it will be deleted everywhere.
Maybe executeSingleCommand(command: FiberyCommand)
might be needed for specific commands, but it should be possible from within Fibery automations as there is no need to leave the server environment, no?
Hi here is the sketch for a script which copies files from target Entity
// Fibery API is used to retrieve and update entities
const fibery = context.getService('fibery');
const utils = context.getService('utils');
const yourAccountHostName = "https://"; // e.g. "https://example.fibery.io"
const recipientEntityType = "" // e.g. "Copy File/File Recipient";
const recipientEntityId = "" // e.g. "bdbac3b0-c41e-11ed-b1e2-91c412ad7219";
// affected entities are stored in args.currentEntities;
// to support batch actions they always come in an array
for (const entity of args.currentEntities) {
// to get collection fields query the API and provide the list of fields
const entityWithExtraFields = await fibery.getEntityById(entity.type, entity.id, ['Files']);
for (const file of entityWithExtraFields.Files) {
const fileWithSecret = await fibery.getEntityById("fibery/file", file.id, ["Secret", "Name", "Content Type"]);
const fileUrl = yourAccountHostName + "/api/files" + fileWithSecret["Secret"];
await fibery.addFileFromUrl(fileUrl, fileWithSecret["Name"], recipientEntityType, recipientEntityId, {
"Authorization": "Token YOUR_API_TOKEN" // replace YOUR_API_TOKEN with one from "You can generate, list and delete tokens on the "API Tokens" page available from the workspace menu.
});
}
}
4 Likes