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 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.
//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 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 );
}
…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.
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.
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.
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?
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…
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.
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?
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.
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!
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.
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!
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.