# Add vs Overwrite - Relationship Creation Automation

**URL:** <https://community.fibery.io/t/add-vs-overwrite-relationship-creation-automation/5997>\
**Category:** Misc\
**Created:** [March 19, 2024, 6:20pm UTC](https://community.fibery.io/t/add-vs-overwrite-relationship-creation-automation/5997 "2024-03-19T18:20:10Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Maximilian\_Breckbill](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/maximilian_breckbill/32/5080_2.png) [@Maximilian\_Breckbill](https://community.fibery.io/u/Maximilian_Breckbill)\
**Post date:** [March 19, 2024, 6:20pm UTC](https://community.fibery.io/t/add-vs-overwrite-relationship-creation-automation/5997/1 "2024-03-19T18:20:10Z")

</div>

Hello All - I have 3 databases: Candidates, Positions, and Matches. This last database is a relational DB with each record under Matches being the combination of 1 Candidate and 1 Position.

I’d like to eliminate the Match database, and relate Candidates and Positions directly to each other.

As I have thousands of Match records, I’d like to find an automated way of doing this.

I was able to create a Button formula that does this… almost:

 ![Screenshot 2024-03-19 at 19.11.44](https://us1.discourse-cdn.com/flex020/uploads/fibery/original/2X/2/26bcd530fcf4e3bf3bf724ac547d68941dd10d1e.png)

[POSITIONS (SalesMaster)].Filter(Name = [Step 1 MATCHES].POSITION.Name)

However, I have the problem that many candidates are linked to multiple positions via Match records, and this automation overwrites any positions the candidate was previously directly matched to.

I’d like to find a way to ADD new position relations to each Candidate instead of REPLACING the ones that are already there.

I tried this:

[POSITIONS (SalesMaster)].Filter(Name = [Step 1 MATCHES].POSITION.Name)

- [Step 1 MATCHES].CANDIDATE.[POSITIONS-relational]

But it doesn’t allow me to complete the formula

 ![Screenshot 2024-03-19 at 19.19.51](https://us1.discourse-cdn.com/flex020/uploads/fibery/original/2X/5/58d5a517ec704f0645cf0211012a93d79d692ef9.png)

Any tips on how to resolve this?

---

<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 20, 2024, 9:21am UTC](https://community.fibery.io/t/add-vs-overwrite-relationship-creation-automation/5997/2 "2024-03-20T09:21:10Z")

</div>

I’d suggest adding a (temporary) lookup field to get all Positions from a Candidate’s Matches.  
Then run an automation to copy the values in that field into the Candidate’s Positions field.

---

<div class="post-metadata">

**Author:** ![Maximilian\_Breckbill](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/maximilian_breckbill/32/5080_2.png) [@Maximilian\_Breckbill](https://community.fibery.io/u/Maximilian_Breckbill)\
**Post date:** [March 20, 2024, 10:49am UTC](https://community.fibery.io/t/add-vs-overwrite-relationship-creation-automation/5997/3 "2024-03-20T10:49:47Z")

</div>

Wow. 🤦‍♂️

Next time I will ask before even starting 😅

THANK YOU!

---

<div class="post-metadata">

**Author:** ![Maximilian\_Breckbill](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/maximilian_breckbill/32/5080_2.png) [@Maximilian\_Breckbill](https://community.fibery.io/u/Maximilian_Breckbill)\
**Post date:** [March 20, 2024, 11:13am UTC](https://community.fibery.io/t/add-vs-overwrite-relationship-creation-automation/5997/4 "2024-03-20T11:13:16Z")

</div>

The Next thing I want to do is copy over all the text in the “Notes” text field from each Match record a candidate is associated with to the Candidate Record.

Any idea how to best go about that?

---

<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 20, 2024, 12:12pm UTC](https://community.fibery.io/t/add-vs-overwrite-relationship-creation-automation/5997/5 "2024-03-20T12:12:49Z")

</div>

I would suggest a button automation on the Candidate db with the following script:

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

for (const entity of args.currentEntities) {

    const entityWithMatches = await fibery.getEntityById(entity.type, entity.id, ['Matches']);
    const matchIds = entityWithMatches['Matches'].map((m) => m['Id']);
    const matches = await fibery.getEntitiesByIds('Space/Match', matchIds, ['Notes']);

    let summary = '';
    for (const match of matches) {
        const note = await fibery.getDocumentContent(match['Notes']['Secret'], 'md');
        summary = summary + '\n\n' + note;
    }

    await fibery.setDocumentContent(entity['Summary']['Secret'], summary, 'md');
}

```

(I have made guesses as to what the field names might be, and the space name is almost certainly not ‘Space’ 😉 but I hope you will be able to adapt it)

---

<div class="post-metadata">

**Author:** ![Maximilian\_Breckbill](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/maximilian_breckbill/32/5080_2.png) [@Maximilian\_Breckbill](https://community.fibery.io/u/Maximilian_Breckbill)\
**Post date:** [March 20, 2024, 1:37pm UTC](https://community.fibery.io/t/add-vs-overwrite-relationship-creation-automation/5997/6 "2024-03-20T13:37:21Z")

</div>

Amazing. This would have taken me forever.

As I’m not as well versed in Fibery code as I’d like, what are the field-names which you guessed that I would need to replace?

---

<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 20, 2024, 1:46pm UTC](https://community.fibery.io/t/add-vs-overwrite-relationship-creation-automation/5997/7 "2024-03-20T13:46:16Z")

</div>

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

for (const entity of args.currentEntities) {

    const entityWithMatches = await fibery.getEntityById(entity.type, entity.id, ['Matches']);
// ↑
// 'Matches' needs to be replaced with the name of the field that links the candidate db to the match db

    const matchIds = entityWithMatches['Matches'].map((m) => m['Id']);
    const matches = await fibery.getEntitiesByIds('Space/Match', matchIds, ['Notes']);
// ↑ ↑ ↑
// 'Space' needs to be replaced with the name of the space that contains the match db.
// 'Match' needs to be replaced with the name of the match db
// 'Notes' needs to be replaced with the name of the rich text field in the match db

    let summary = '';
    for (const match of matches) {
        const note = await fibery.getDocumentContent(match['Notes']['Secret'], 'md');
// ↑
// and again in this line

        summary = summary + '\n\n' + note;
    }

    await fibery.setDocumentContent(entity['Summary']['Secret'], summary, 'md');
// ↑
// 'Summary' needs to be replaced with the name of the rich text field in the candidate db where you want the notes to be collected together
}

```

Hope that helps!

---

<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 20, 2024, 3:48pm UTC](https://community.fibery.io/t/add-vs-overwrite-relationship-creation-automation/5997/8 "2024-03-20T15:48:35Z")

</div>

Sorry I wasn’t clear.  
You need to use the code I pasted in a ‘Script’ action, not in the ‘Update’ formula:

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