# Help with updateEntity issue

**URL:** <https://community.fibery.io/t/help-with-updateentity-issue/6100>\
**Category:** Get Help\
**Created:** [April 19, 2024, 6:35pm UTC](https://community.fibery.io/t/help-with-updateentity-issue/6100 "2024-04-19T18:35:27Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thom\_Bator](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/thom_bator/32/9241_2.png) [@Thom\_Bator](https://community.fibery.io/u/Thom_Bator)\
**Post date:** [April 19, 2024, 6:35pm UTC](https://community.fibery.io/t/help-with-updateentity-issue/6100/1 "2024-04-19T18:35:28Z")

</div>

Hi I am trying to write an automation script on a button. Basically we have form submissions already coming into fibery when a user submits a form on one of our sites.

Submission entities are to be associated with Form entities with in our Space “Form Database”.

I have created a button “Add Form” for each Submission entity that will create a new Form entity, or associate the submission with a matching form is one already exists.

I have got the form creation part working okay, but am having trouble updating my Submission entity so it is associated with the new form that has been created.

I followed the basic pattern I saw in [this post](https://community.fibery.io/t/basic-script-for-link-existing-entity-or-create-a-new-one/6052) on the forum

But I keep getting this error: “Failed to execute Action “Script”: field “Type” was not found in type “Submission””

The thing is when I console.log entity.Type and entity.Id, I can see the values are there as expected.

Here is the relevant code snippet:

const updatedEntity = { …entity, Form: newForm };

await fibery.updateEntity(entity.Type, entity.Id, updatedEntity);

Any help on how to get this working would be appreciated thank you!

---

<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:** [April 19, 2024, 6:48pm UTC](https://community.fibery.io/t/help-with-updateentity-issue/6100/2 "2024-04-19T18:48:21Z")

</div>

Can you share the whole script

---

<div class="post-metadata">

**Author:** ![Thom\_Bator](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/thom_bator/32/9241_2.png) [@Thom\_Bator](https://community.fibery.io/u/Thom_Bator)\
**Post date:** [April 19, 2024, 6:58pm UTC](https://community.fibery.io/t/help-with-updateentity-issue/6100/3 "2024-04-19T18:58:23Z")

</div>

Sure thank you!

const fibery = context.getService(‘fibery’);

for (let entity of args.currentEntities) {

//If the submissions entity already has a Form.Name property, it has already been associated with a form so this button will not do anything  
if (entity.Form.Name) {  
return `No new form created. Form ${entity.Form.Name} is already associated with this submission`;  
}

//By this point in the execution, we know that no forms are currently associated with the submission, but it is possible that a form exists that was not automatically associated for some reason  
//These variables will be used to create the filter on the graphQL query below to return only a Form entity that has a matching name and a matching domain  
const hostName = entity[‘Host Name’];  
const formName = entity[‘Form Name’];

const query = `{findForms(name: {is: "${formName}"}, productionDomain: {is: "${hostName}"}){id, name, productionDomain}}`;

```
const matchingForm = await fibery.graphql("Form Database", query);

if (matchingForm.data.findForms.length > 0) {

    entity.Form.Name = matchingForm.data.findForms[0].name;
    entity.Form.Id = matchingForm.data.findForms[0].id;
    //Here we will update Submission with existing form 
    console.log("Found a form!: ");
    console.log("Form id: ", matchingForm.data.findForms[0].id);
    console.log("Form name: ", matchingForm.data.findForms[0].name);
}

else {

    //Get Submission properties to create new form, will add more later
    const name = entity['Form Name']; 

    //This API call is working fine 
    const newForm = await fibery.createEntity("Form Database/Form", { 'Name': name });

    //Create a new object that has all the original entity properties but adds the new form to the Form property. 
    const updatedEntity = { ...entity, Form: newForm };

```

//All of these logs are showing the expected values  
console.log("Entity Type: ", entity.Type);  
console.log("Entity Id: ", entity.Id);  
console.log("Updated Entity: ", updatedEntity);  
console.log("New form: ", newForm);

```
    //Update the submission entity to associate with the new form 
    //This is the call that is not working 
    await fibery.updateEntity( entity.Type, entity.Id, updatedEntity );
    

}

```

}

---

<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:** [April 19, 2024, 9:18pm UTC](https://community.fibery.io/t/help-with-updateentity-issue/6100/4 "2024-04-19T21:18:17Z")

</div>

What are you trying to do with this code?

```auto
entity.Form.Name = matchingForm.data.findForms[0].name;
    entity.Form.Id = matchingForm.data.findForms[0].id;

```

If you’re trying to link a Form to the entity, this isn’t the way to do it.

And this isnt how to update the entity:

```auto
const updatedEntity = { ...entity, Form: newForm };
await fibery.updateEntity( entity.Type, entity.Id, updatedEntity );

```

…entity will include not only the fields but also the Type, so you will be trying to update a field called Type (which doesn’t exist) hence the error message.

---

<div class="post-metadata">

**Author:** ![Thom\_Bator](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/thom_bator/32/9241_2.png) [@Thom\_Bator](https://community.fibery.io/u/Thom_Bator)\
**Post date:** [April 19, 2024, 9:53pm UTC](https://community.fibery.io/t/help-with-updateentity-issue/6100/5 "2024-04-19T21:53:05Z")

</div>

```
  entity.Form.Name = matchingForm.data.findForms[0].name;
    entity.Form.Id = matchingForm.data.findForms[0].id;

```

This above code is just to check if a form exists already that should be linked to the submission. It is just a check, then I will add logic to update the Submission entity to link to the existing form if there is a match instead of creating a new one.

const updatedEntity = { …entity, Form: newForm };  
await fibery.updateEntity( entity.Type, entity.Id, updatedEntity );

If this way is incorrect, can you please show me the correct way?

---

<div class="post-metadata">

**Author:** ![Thom\_Bator](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/thom_bator/32/9241_2.png) [@Thom\_Bator](https://community.fibery.io/u/Thom_Bator)\
**Post date:** [April 19, 2024, 11:08pm UTC](https://community.fibery.io/t/help-with-updateentity-issue/6100/6 "2024-04-19T23:08:36Z")

</div>

Hi so I think I made some progress based on your feedback.

I tried just the value for the Form field, then I realized I actually just want to pass in the newForm.Name and newForm.Id so I made a new object that just had those properties included.

```
await fibery.updateEntity(entity.Type, entity.Id, {
        Form: newFormIdAndName
    });

```

I am no longer getting the type error, but now I am getting a new Error.

updateEntity: cannot set {“Name”:“Form Name”,“Id”:“54be3f53-8e94-44fc-8ab5-7d74f555d9ea”} to reference field Form. In order to set reference field you need to pass id of entity

I am currently passing entity.Id as an argument. Does it need to be passed somewhere else as well?

---

<div class="post-metadata">

**Author:** ![IMTanuki](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/imtanuki/32/7751_2.png) [@IMTanuki](https://community.fibery.io/u/IMTanuki)\
**Post date:** [April 20, 2024, 2:00am UTC](https://community.fibery.io/t/help-with-updateentity-issue/6100/7 "2024-04-20T02:00:59Z")

</div>

Hi - I’m having similar issues with updateEntity, except with the above syntax, I’m getting an error that Type is not recongized (for a single select field).

It would be very helpful if the use case(s) and syntax for methods used in Fibery automation were published…

---

<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:** [April 20, 2024, 6:56am UTC](https://community.fibery.io/t/help-with-updateentity-issue/6100/8 "2024-04-20T06:56:50Z")

</div>

The intellisense will indicate as you type what arguments are needed for each function.

---

<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:** [April 20, 2024, 6:59am UTC](https://community.fibery.io/t/help-with-updateentity-issue/6100/9 "2024-04-20T06:59:20Z")

</div>

> [@Thom\_Bator](#):
>
> updateEntity: cannot set {“Name”:“Form Name”,“Id”:“54be3f53-8e94-44fc-8ab5-7d74f555d9ea”} to reference field Form. In order to set reference field you need to pass id of entity

When updating an entity, you send an object with key value pairs. The keys are the field names and the values are the field values.  
To update a to-one relation field, the key is the field name and the value is the id of the entity to be linked. You shouldn’t send the name of the entity for any reason.

---

<div class="post-metadata">

**Author:** ![Thom\_Bator](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/thom_bator/32/9241_2.png) [@Thom\_Bator](https://community.fibery.io/u/Thom_Bator)\
**Post date:** [April 20, 2024, 10:48am UTC](https://community.fibery.io/t/help-with-updateentity-issue/6100/10 "2024-04-20T10:48:31Z")

</div>

Hi,

I am not sending the name of the entity. Here is the API call I making, it seems to match what the intellisense is providing.

await fibery.updateEntity(entity.Type, entity.Id, {  
Form: newFormIdAndName

});

The name property you are seeing in the error is because the variable newFormIdAndName is itself an object, which has the shape {Name: “example”, Id: “example”} .

Do I need to do something special to update a nested object maybe?

---

<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:** [April 20, 2024, 11:05am UTC](https://community.fibery.io/t/help-with-updateentity-issue/6100/11 "2024-04-20T11:05:28Z")

</div>

The value in the key value pair needs to be the ID of the form (string). You are using an object.

And the key needs to be the name (string) of the relation field.

---

<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:** [April 20, 2024, 11:08am UTC](https://community.fibery.io/t/help-with-updateentity-issue/6100/12 "2024-04-20T11:08:19Z")

</div>

> [@Thom\_Bator](#):
>
> Do I need to do something special to update a nested object maybe?

It is not possible to ‘update a nested object’. The update only updates the field values of the entity passed via the entity id.

---

<div class="post-metadata">

**Author:** ![Thom\_Bator](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/thom_bator/32/9241_2.png) [@Thom\_Bator](https://community.fibery.io/u/Thom_Bator)\
**Post date:** [April 20, 2024, 11:24am UTC](https://community.fibery.io/t/help-with-updateentity-issue/6100/13 "2024-04-20T11:24:41Z")

</div>

Ok thank you that has moved things along.

Now I am getting the error: Failed to execute Action “Script”: updateEntity: Cannot update [“Form Database/Form”] readonly fields for entities of ‘Form Database/Submission’ database.

I have updated to just pass the Id as you directed. await fibery.updateEntity(entity.Type, entity.Id, {

```
        "Form": newForm.Id 
    });

```

My end goal is just to get the new form my script created associated back to the Submission.

Thanks for your patience!

---

<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:** [April 20, 2024, 12:53pm UTC](https://community.fibery.io/t/help-with-updateentity-issue/6100/14 "2024-04-20T12:53:29Z")

</div>

As the error says, the Form field is read-only. Is it an auto-relation field?

---

<div class="post-metadata">

**Author:** ![interr0bangr](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/interr0bangr/32/8089_2.png) [@interr0bangr](https://community.fibery.io/u/interr0bangr)\
**Post date:** [April 22, 2024, 2:22pm UTC](https://community.fibery.io/t/help-with-updateentity-issue/6100/15 "2024-04-22T14:22:13Z")

</div>

Jumping in for @Thom_Bator as he’s a developer on my team.

@Chr1sG Yes, it’s an auto relation. We were hoping to use scripts to work around the limitation of entities from auto-related fields not being able to be created in automation rules, but I guess the same limitation exists in scripts too?

Our scenario is this:  
When a form is submitted on our site, we use GTM to create a new entity in the “Submissions” database.  
On every Submission entity, there’s a “Domain” field and a “Form Name” field.

We also have a “Form” database, that’s related to a “Page” database.  
On every Form entity, there’s a “Name”  
On every Page entity, there’s a “Domain” field

The auto-relation logic between “Form” and “Submission” is as follows:  
Form.Name = Submission.Form\_Name  
and  
Form.Page.Domain = Submission.Domain

We try to map all pages/forms on a site ahead of time, so the auto-relationship works perfectly, but there’s some scenarios where a new form is created before the page/form gets mapped into Fibery, or the submission doesn’t have the right information on it so it doesn’t get auto-related properly (i.e. the “Form Name” is wrong). It’s in those situations where we’d like to press a fibery button on the Submission entity that run a scripts which creates the missing form/page.

If there’s a better way to do it, would love to hear it!

---

<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:** [April 22, 2024, 2:30pm UTC](https://community.fibery.io/t/help-with-updateentity-issue/6100/16 "2024-04-22T14:30:03Z")

</div>

If you’re making use of auto-relations then there is no way for a user to change which entities are linked (via the gui nor via scripting), so in principle, you don’t need to make a link, provided you create a Form which meets the matching criteria - Fibery will ensure that they get linked.  
However, auto-relations are a bit like formulas in that they take some time to be recalculated, so you will need to be careful when trying to ascertain if a match already exists. It might appear to begin with that nothing is matched, if the calculation of the entities to auto-link has not completed.

---

<div class="post-metadata">

**Author:** ![interr0bangr](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/interr0bangr/32/8089_2.png) [@interr0bangr](https://community.fibery.io/u/interr0bangr)\
**Post date:** [April 22, 2024, 2:42pm UTC](https://community.fibery.io/t/help-with-updateentity-issue/6100/17 "2024-04-22T14:42:45Z")

</div>

Yeah, I run into the formula calculation delay all the time across multiple rules and it always drives me crazy. Wish we could put a delays in workflows!

But I understand what you’re saying. I think instead of trying to create the relationship, we just need to create the entities in a way that will allow them to auto-link. @Thom_Bator , lets chat when you have a sec! 🙂

---

<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:** [April 22, 2024, 3:09pm UTC](https://community.fibery.io/t/help-with-updateentity-issue/6100/18 "2024-04-22T15:09:53Z")

</div>

Given that Fibery supports simultaneous collaboration, I can’t imagine an easy solution to the issue of formula results being calculated with a slight delay.  
If it’s a problem, then try and redesign your automation rules to trigger off the updates to the calculated fields, rather than the fields on which they depend.
