Current User entity in formulas

I am wondering if it is possible to add the current logged in user as a constant/function that can be accessed in formulas. My use case is automatically adding the user as an attendee to a meeting. I imagine it will have other uses as well.

I suspect it’s not possible - what would/should a formula show if two users were viewing the entity at the same time?

Of course, it is possible to refer to the current user in action buttons and automation rules, but that’s another story.

That is exactly what I was thinking: use it in action button or automation. Totally agree that it would be useless in a formula field. I was thinking something along the line of the special “[Step 1]” object that is only available for actions and automations.

I think it will also probably have some usefulness in the future for using permissions as part of actions/automations. I think that would require implementation of some flow control/conditionals in action editor but probably will be useful.

Based on my understanding of what you want to achieve, it’s already possible with a script as part of a button/automation.
I believe flow control logic in buttons/automation may be on the radar for future improvements.

1 Like

Agreed, I just think formulas are easier to understand and therefore easier to maintain long term. Fully agree that this is down the list of priorities :slight_smile:

It is actually also possible using only formulas, but in an very crazy/roundabout way :slight_smile:

You make use of the ‘Assign to me’ function available in buttons/automations to find out who is the current user, and then switch the contents of the Assignees and Attendees fields.

It’s hard to explain, but here is it visually:

(Note: it uses a ‘helper’ field called Current assignees for temporary storage).

Hi Chris,

Following up on this one, is there any chance to implement such feature in a (close :pray:) future or is there any strong technical limitations?

Use case shall be to compute user specific indicators in order to filter user specific views; example below:

  • we have tasks & studies that are related with an internal goal and internal goals that are related to a Milestone
  • tasks & studies are assigned to people

I’d like to know how many tasks & studies are assigned to me (by “me”, I mean current user) on a given internal goal.
May you have some kinda magic potion to workaround this?

Cheers.
/BC

It sounds like you need to add a couple of lookup fields in the Internal goal DB to get the users assigned to related Tasks/Studies.
Then, you can create views with filters ‘Task Assignees’ contains Me OR ‘Studies Assignees’ contains Me to show all Internal goals you are contributing to.
However, it’s not possible to make a formula field that counts how many things are ‘assigned to Me’ for the reasons described above:

Please can you tell me how to refer to the current user in action buttons? Thanks

1 Like

It’s currently only possible in a script action (by using args.currentUser).

1 Like

I made an automation button that ‘toggles’ the current user being linked to the button entity, meaning linking if unlinked, and unlinking when linked.

I couldn’t figure out how to do that with a single button and formulas only. Would it be possible?

In the meantime, I used the following script to accomplish it, but of course we want o minimize the use of scripts.
In this script, the current user is added or removed to the ‘Selectors’ field in entity of database ‘Y Page’.

const fibery = context.getService('fibery');
const pageType = 'Yuri/Y Page';
const userFieldType = 'Selectors';

async function toggleUserLink() {
    try {
        const currentUser = args.currentUser;
        const currentEntity = args.currentEntities[0];

        if (!currentUser) {
            console.error('No current user information available.');
            return;
        }

        // Fetch the current entity with 'Selectors' information
        const pageEntity = await fibery.getEntityById(pageType, currentEntity.id, [userFieldType]);
        const linkedSelectors = pageEntity[userFieldType].map(({ id }) => id);

        // Check if the current user is already linked
        const isUserLinked = linkedSelectors.includes(currentUser['Id']);

        if (isUserLinked) {
            // User is linked, so we'll unlink them
            console.log(`Unlinking user ${currentUser.Email} from Y Page with ID:`, currentEntity.id);
            await fibery.removeCollectionItem(pageType, currentEntity.id, userFieldType, currentUser['Id']);
        } else {
            // User is not linked, so we'll link them
            console.log(`Linking user ${currentUser.Email} to Y Page with ID:`, currentEntity.id);
            await fibery.addCollectionItem(pageType, currentEntity.id, userFieldType, currentUser['Id']);
        }
        console.log('Operation completed successfully.');
    } catch (error) {
        console.error('Error in script:', error);
    }
}

await toggleUserLink();

Just to check I understand:
you have an entity with a (to-one) relation to the User db, and you want to allow that a user can press a button, and when doing so

  • the button presser becomes the linked User if there is no User currently linked
  • the button presser is unlinked if they are the currently linked User

Is that correct?

What should happen if User A presses the button while User B is currently linked?

Assuming that you want to change the linked user (from B to A) in this situation, then you can achieve it by having a button automation with an action that updates the User field according to this formula:

Users.Filter([Public Id] != [Step 1 Thing].User.[Public Id] and [Public Id] = [User who clicked Button].[Public Id]).Sort().First()

It finds the user who

  • is not the person currently linked and
  • is the person who pressed the button.

So if the button presser is currently linked, this equals nobody, and they will be unlinked.
If the currently linked user is not the button presser (or is empty) then this formula equals the button presser, so they will become linked.

1 Like

The entity has a to many relation with the User db, meaning that multiple users can link and unlink themselves to it by clicking the button.

The use case is to allow users to create a ‘select list’ where multiple unrelated entities can be selected/unselected by the current user using the toggle button, and after that the user can perform bulk operations on that select list.

(I also created a more flexible version: a personal ‘Bucket list’ where a Bucket entity is linked to both the user and the enity upon button click, when the user has one of their personal buckets selected in their profile. The user then can perform bulk operation on bucket list items.)

For the m:m case, I’m not sure it’s possible to either link/unlink a user with a single button automation and a formula. I think the scripting method is currently the only way.

1 Like