Use updateEntity with multi-select fields

I was trying to find examples of how to add multiple items to multi-select fields using scripts and the updateEntity function. I was passing in the following values object to the function:

{
  Name: 'Test task',
  Assignee: 'bae5f000-5af9-11eb-bced-bb2f3ecffbc1',
  Status: 'Assigned',
  Priority: [ 'Important', 'Urgent' ]
}

Everything works fine except for the Priority field which is a multi-select field. When I pass the object, I get the following error:

updateEntity: Only string values are supported for enum fields

I tried passing the fibery/id values as well and got the same error.

Apologies if this is explained somewhere and I missed it.

Hi, @cannibalflea

Could you please give more context where are you going to use this code? Is it automations script action?

Thanks,
Oleg

Hi @Oleg ,

Sorry for the delay in getting back to you. Yes, I am building an automation script action. The action is triggered whenever a “Task” entity is created and it parses the name of the entity, looking for predefined tags enclosed in []. So I would create an entity called:

Complete the review of the specifications [to me ] [by 0 aft ] [tags i u ]

The automation script would take each of the items enclosed by [] and using the first word, prepares an object to update the entity:

[to me/Contact Name]: assigns the task to the entity’s creator (me) or a Contact entity with the given name (if multiple Contacts are found, it adds a note to the task to double check the name

[by #days morn/aft/eod]: assigns the deadline by setting the date to today+#days and the time based on predefined value of what morn/aft/eod means

[tags u i]: sets the task priority with u=urgent and i=important. none/one/all of these could be selected as Priority is a multi-select field

I collect the updates in the entUpdates object, which looks like:

{
  Name: 'Test task',
  Assignee: 'bae5f000-5af9-11eb-bced-bb2f3ecffbc1',
  Status: 'Assigned',
  Priority: [ 'Important', 'Urgent' ]
}

And then try to update the entity:

await fibery.updateEntity(entity.type, entity.id, entUpdates);

This is where I get the error.

This sounds a bit crazy but it is the best solution that I could come up with to set of the parameters for tasks without leaving the writing context.

I hope that makes sense. I can post the entire script if that is helpful.

Hi @cannibalflea
Multi-select is coded as relation field with many-to-many semantic. Not quite sure if there is a custom logic in Script API, will ask the author. However you definitely may utilise a common API.
So instead of Names of Priority you should use ids like one you use for Assignee, please find script example bellow.

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

// affected entities are stored in args.currentEntities;
// to support batch actions they always come in an array
for (const entity of args.currentEntities) {
    // expansive call, avoid it if you can
    const schema = await fibery.getSchema();

    // Test/Project is a name of Project type in Test space
    // Test/Priority is a name of Priority field in Project type
    const typeFieldNames = schema.typeObjectsByName["Test/Project"].fieldObjects.map((f) => {
        return {
            name: f.name,
            uiTitle: f.title,
            fieldType: f.type,
            kind: f.kind,
        };
    })

    console.log("Project Fields", typeFieldNames);

    const multiSelectType = schema.typeObjectsByName["Test/Project"].fieldObjectsByName["Test/Priority"].type;
    console.log("multiSelectType", multiSelectType);

    const selectValues = await fibery.executeSingleCommand({
        command: "fibery.entity/query",
        args: {
            query: {
                "q/from": multiSelectType,
                "q/select": ["fibery/id", "enum/name"],
                "q/limit": 10,
            }
        }
    });

    console.log("selectValues", selectValues);

    const valuesMap = new Map( selectValues.map((x) => [x["enum/name"], x["fibery/id"]]));

    // cast to a String to make redactor happy
    const highPriorityId = String(valuesMap.get("high"));
    console.log("highPriorityId", highPriorityId);

    await fibery.addCollectionItem("Test/Project", entity["Id"], "Priority", highPriorityId);
}

Custom script logs (console.log) you may find in Activity tab

1 Like