# Enable file field to be a "where" filter in the action trigger of automations

**URL:** <https://community.fibery.io/t/enable-file-field-to-be-a-where-filter-in-the-action-trigger-of-automations/8366>\
**Category:** Ideas & Features\
**Tags:** automations, files\
**Created:** [March 4, 2025, 1:39am UTC](https://community.fibery.io/t/enable-file-field-to-be-a-where-filter-in-the-action-trigger-of-automations/8366 "2025-03-04T01:39:43Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![tycecycle](https://avatars.discourse-cdn.com/v4/letter/t/848f3c/32.png) [@tycecycle](https://community.fibery.io/u/tycecycle)\
**Post date:** [March 4, 2025, 1:39am UTC](https://community.fibery.io/t/enable-file-field-to-be-a-where-filter-in-the-action-trigger-of-automations/8366/1 "2025-03-04T01:39:43Z")

</div>

I wanted to set up an automation that would trigger whenever an email entity from the email integration gets linked to a job candidate entity in another database, and if and only if there’s an attachment to the email that’s likely a resume (i.e., ends in .pdf or .docx and contains “resume”), then the file gets added to the hiring candidate. Looking at the filter options in the action trigger on the email entities, the files field isn’t included.

---

<div class="post-metadata">

**Author:** ![tycecycle](https://avatars.discourse-cdn.com/v4/letter/t/848f3c/32.png) [@tycecycle](https://community.fibery.io/u/tycecycle)\
**Post date:** [March 4, 2025, 1:44am UTC](https://community.fibery.io/t/enable-file-field-to-be-a-where-filter-in-the-action-trigger-of-automations/8366/2 "2025-03-04T01:44:28Z")

</div>

I (AI) was able to script a button that does what I’m looking for though 😎

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

// Get candidate ids from incoming entities
const candidateIds = args.currentEntities.map(e => e.id);

// Batch fetch candidates with Emails field
const candidates = await fibery.getEntitiesByIds('Home/Hiring candidates', candidateIds, ['Emails']);

// Gather all unique Email ids from candidates
let emailIdsSet = new Set();
const candidateEmailsMap = {};
for (const candidate of candidates) {
    if (candidate.Emails && candidate.Emails.length > 0) {
        candidateEmailsMap[candidate.id] = candidate.Emails.map(email => email.id);
        candidateEmailsMap[candidate.id].forEach(id => emailIdsSet.add(id));
    }
}

// Convert set to array
const emailIds = Array.from(emailIdsSet);
if (emailIds.length === 0) {
    return `No linked emails found.`;
}

// Batch fetch emails with Files field
const emails = await fibery.getEntitiesByIds('Email/Email', emailIds, ['Files']);

// Create a map for email id to its file ids (if any)
const emailFilesMap = {};
for (const email of emails) {
    if (email.Files && email.Files.length > 0) {
        emailFilesMap[email.id] = email.Files.map(file => file.id);
    }
}

// Prepare batch addition updates for candidate's Files field
let batchArgs = [];
for (const candidate of candidates) {
    const emailIdsForCandidate = candidateEmailsMap[candidate.id] || [];
    let filesToAdd = [];
    emailIdsForCandidate.forEach(emailId => {
        if (emailFilesMap[emailId]) {
            filesToAdd = filesToAdd.concat(emailFilesMap[emailId]);
        }
    });
    // Remove duplicates if any
    filesToAdd = [...new Set(filesToAdd)];
    // If there are file ids to add, prepare them for batch call
    for (const fileId of filesToAdd) {
        batchArgs.push({ id: candidate.id, itemId: fileId });
    }
}

// If there is any update, add new files to candidates "Files" collection field using batch operation
if (batchArgs.length > 0) {
    await fibery.addCollectionItemBatch('Home/Hiring candidates', 'Files', batchArgs);
}

return `${batchArgs.length} file link(s) were added to candidates`;

```

---

<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:** [March 4, 2025, 7:14am UTC](https://community.fibery.io/t/enable-file-field-to-be-a-where-filter-in-the-action-trigger-of-automations/8366/3 "2025-03-04T07:14:25Z")

</div>

You could create a formula field, something like this

 ![image](https://us1.discourse-cdn.com/flex020/uploads/fibery/original/2X/f/f6161f16631c52eeaf494cdf4c9e5f33aca843be.png)  
and then use its value as a filter in your automation.

Or in your case, maybe this:

`Candidate.Files.Count()>0`

in the Email database.

---

<div class="post-metadata">

**Author:** ![tycecycle](https://avatars.discourse-cdn.com/v4/letter/t/848f3c/32.png) [@tycecycle](https://community.fibery.io/u/tycecycle)\
**Post date:** [March 4, 2025, 1:43pm UTC](https://community.fibery.io/t/enable-file-field-to-be-a-where-filter-in-the-action-trigger-of-automations/8366/4 "2025-03-04T13:43:10Z")

</div>

That’s a great workaround @ChrisG. I think it still would be more intuitive to set this up without a helper formula, and I’d rather not add custom fields to databases from integrations, but those are relatively small quibbles.

Also for posterity, the script above does not permanently copy files to the entity. Chris helpfully pointed me to this thread for more context: [Copy file to another entity](https://community.fibery.io/t/copy-file-to-another-entity/4139)

---

<div class="post-metadata">

**Author:** ![tycecycle](https://avatars.discourse-cdn.com/v4/letter/t/848f3c/32.png) [@tycecycle](https://community.fibery.io/u/tycecycle)\
**Post date:** [March 6, 2025, 8:56pm UTC](https://community.fibery.io/t/enable-file-field-to-be-a-where-filter-in-the-action-trigger-of-automations/8366/5 "2025-03-06T20:56:58Z")

</div>

Claude and I finally got there with a script that will actually copy files from one entity to another:

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

// Get candidate ids from incoming entities
const candidateIds = args.currentEntities.map(e => e.id);

// Batch fetch candidates with Emails field
const candidates = await fibery.getEntitiesByIds('Hiring/Hiring candidates', candidateIds, ['Emails', 'Files']);

// Gather all unique Email ids from candidates while mapping candidate id to its email ids
const emailIdsSet = new Set();
const candidateEmailsMap = {};
for (const candidate of candidates) {
    if (candidate.Emails && candidate.Emails.length > 0) {
        const relatedEmailIds = candidate.Emails.map(email => email.id);
        candidateEmailsMap[candidate.id] = relatedEmailIds;
        relatedEmailIds.forEach(id => emailIdsSet.add(id));
    }
}

// Convert set to array and check
const emailIds = Array.from(emailIdsSet);
if (emailIds.length === 0) {
    return `No linked emails found.`;
}

// Batch fetch emails with Files field
const emails = await fibery.getEntitiesByIds('Email/Email', emailIds, ['Files']);

// Track progress
let totalFilesFound = 0;
let successCount = 0;
let errorCount = 0;

// Process each candidate
for (const candidateId of candidateIds) {
    const candidate = candidates.find(c => c.id === candidateId);
    
    // Get IDs of files already attached to the candidate to avoid duplicates
    const existingFileIds = new Set();
    if (candidate.Files && candidate.Files.length > 0) {
        candidate.Files.forEach(file => {
            const fileId = file.Id || file.id;
            if (fileId) existingFileIds.add(fileId);
        });
    }
    
    const emailIdsForCandidate = candidateEmailsMap[candidateId] || [];
    
    // Process each email related to this candidate
    for (const emailId of emailIdsForCandidate) {
        const email = emails.find(e => e.id === emailId);
        if (!email || !email.Files || !email.Files.length) continue;
        
        // Process each file in the email
        for (const file of email.Files) {
            const fileId = file.Id || file.id;
            
            // Skip if this file is already attached to the candidate
            if (existingFileIds.has(fileId)) continue;
            
            totalFilesFound++;
            
            try {
                // Use the working URL format we discovered
                const fileUrl = `https://api.fibery.io/api/files/${fileId}`;
                const fileName = file.Name || file.name || `File-${fileId}`;
                
                // Add the file to the candidate
                await fibery.addFileFromUrl(
                    fileUrl,
                    fileName,
                    'Hiring/Hiring candidates',
                    candidateId,
                    { field: 'Files' }
                );
                
                successCount++;
            } catch (error) {
                console.error(`Failed to copy file ${fileId} to candidate ${candidateId}: ${error.message}`);
                errorCount++;
            }
        }
    }
}

if (totalFilesFound === 0) {
    return `No new files found to copy.`;
}

return `Files processed: ${successCount} successful, ${errorCount} failed.`;

```

---

<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:** [October 30, 2025, 2:31pm UTC](https://community.fibery.io/t/enable-file-field-to-be-a-where-filter-in-the-action-trigger-of-automations/8366/6 "2025-10-30T14:31:06Z")

</div>

Can be done without script now

> [@October 30, 2025 / Share entities to web using access templates, New names for access levels, Improved File Fields support in automations](https://community.fibery.io/t/october-30-2025-share-entities-to-web-using-access-templates-new-names-for-access-levels-improved-file-fields-support-in-automations/9924):
>
> Share entities to web using access templates Introducing a new mechanism for sharing entities to web via public links with two major benefits: Everyone can see that an entity is being shared publicly → no accidental overshares. The sharer can control what related entities are shared alongside the main one → no accidental overshares. Now you can, for example, safely share a Projects and its Tasks (but not Allocations) using a public link. Check out the [guide for the new mechanism](https://the.fibery.io/@public/User_Guide/Guide/Share-Entity-to-Web-via-Public-Link-439). T…

---

<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 30, 2025, 4:14pm UTC](https://community.fibery.io/t/enable-file-field-to-be-a-where-filter-in-the-action-trigger-of-automations/8366/7 "2025-10-30T16:14:48Z")

</div>


