Script Button to archive a linked record

Hello,

I am trying to create a button that moves a record reference in a table from one field (“User”) to another field (“Archived User”). I noticed this can be done without scripting through Fibery’s no-code automations, but, there is a conditional that I need built into the button:

 if (User!=""){
      ... }

This is the code I have so far:

const api = context.getService("fibery");

for (const entity of args.currentEntities) {
    const UserIds = await api.getEntityById(entity.type, entity.id, [["User"]]);
    if (UserIds != null) {
        await api.updateEntity(entity.type, entity.id, { "Archived User": UserIds });
        await api.updateEntity(entity.type, entity.id, { "User": "[]" });
    }
}

And that code is throwing this unexpected exception…

Failed to execute action "Script": field.toLowerCase is not a function

Any help is appreciated!

Disclaimer

I am new to Fibery, and I know there may be better ways to structure my app, but please note my main purpose of posting this is to focus on learning the API/scripting portion of Fibery.

I have read these so far to construct the above code:


Hi, Matthew

Thanks for letting know about your problem. Please find below explanation

  1. Let’s assume I have setup with two users for entity as you described above. Every feature can have one User and one Archived User

  2. This is how the button script action can be configured to move User to Archived User

The code is

const fibery = context.getService('fibery');
for (const entity of args.currentEntities) {
    const entityWithExtraFields = await fibery.getEntityById(entity.type, entity.id, ['User']);
    if (entityWithExtraFields[`User`].id) {
        await fibery.updateEntity(entity.type, entity.id, {
            'Archived User': entityWithExtraFields[`User`].id,
            'User': null
        });
    }
}

Please let me know if I didn’t catch the issue correctly.

Thanks,
Oleg

1 Like

What do I need to study to learn how to create such scripts? :slight_smile:

Hi, @v3j

We hope that there will be no need to learn scripts in the nearest future. No-code applications should make the life simpler. Will do our best to accomplish that.

Thanks,
Aleh

1 Like

Hi Oleg,

No worries! I’m just really interested to learn how while waiting :slight_smile: What google keyword would you suggest for starters?

Hi, @v3j

Thanks for your question. You may try suggestions from how to become javascript developer - Google Search

Good luck,
Oleg

1 Like

Hello Oleg, and thank you for the reply. Very clear, and concise. Thanks for the included premise/assumption + picture + code. Also, apologies for the delayed reply. I work on this project in my spare time.

Regarding this assumption: Every feature can have one User and one Archived User

I should have mentioned in my first post earlier that there can be multiple users for “UserIds” field. I see now that my naming convention is also misleading… I will update the original post! Sorry for the confusion!

I ended up just restructuring my table and avoiding the whole array issue. :smiley: thanks for your help!