Can I get current user on formula?

I noticed that I can get current user by trigger some column like “created_by” using user.email. But I can’t get a current user by formula. For example, I try to do some formula bool(user.email = $created_by), but there is no reference to user.

Anyone have some idea to do this? I will use this colunm to filter all lines that is true. Then I can have two tables like activities and my_activities.

Hi Jose!

If you want to get the name of the current user, you would use a trigger formula with the formula user.Name. You can see this in action in the INTERACTIONS table of the Influencer Outreach template.

Here, it’s a trigger formula that applies to new records. When a new record is created the column will populate with the name of the logged in user. In the screenshot below, I added a new interaction and it automatically filled in the name that is associated with my Grist account.

image

You’ve already done the same thing but with the user email. Since you’re trying to filter, you can just use the email column you already have, rather than adding another with the user’s name. I have set up an example for you in the example linked below:

https://public.getgrist.com/gsfhmvQSYHZf/Community-721/m/fork/p/11

On the page linked above, you should just see a table with no rows. This is because there are no interactions with your email listed under the User Email column. Save a copy of this document to your own page then add an Interaction or two from the Interactions Page. Once you add an interaction, it will show up for you under the Filtered Interactions page. I’ll walk you through how to set this up.

Create a copy of the activities table and name it my_activities. Add a new column to the table called ‘Access Rules’. Keep in mind this will be added to all copies of the Activities table. I’ve left this column visible to you on both the Interactions and Filtered Interactions pages so you could see it at work but you’ll hide this on both of your tables once we’ve set this all up.

Next, we are going to add Access Rules to the table. Select ‘Access Rules’ from the Tools menu in the left-hand navigation panel. You’ll need to ‘Add Table Rules’ for the activities table. Specifically, we want to add a column rule. Click the three dots in the upper right of the rules table and ‘Add Column Rule’ for the Access Rule column you added to the table. The condition will be newRec.[YOUR_COLUMN_NAME] == user.Email. Replace [YOUR_COLUMN_NAME] with the name of the column that contains the user email. Add Read and Update permissions for this condition. Then, for everyone else, Deny Read and Update permissions.

For ‘All Other’ columns, you can add whatever rules you would like. OWNER should have all permissions. All other users can read/update/create. You can determine if they should have the ability to delete.

Be sure to ‘Save’ the updates you made to Access Rules. Back in the table, you’ll see CENSORED listed in the Access Rules column for all records where your email is not listed in the user email column.

This is how we are going to filter the my_activities table to only show activities where your email is listed. Click on the filter icon in the upper-right of the my_activities table. Click the + icon next to Add Filter then select the Access Rules column.

Uncheck ‘CENSORED’ values then apply.

image

After filtering, be sure to save your filter by clicking the green check mark in the upper-right of the table (circled in red).

Now your table should only show your activities! Feel free to hide the ‘Access Rules’ column in both the Activities and my_activities tables. Hover over the column header. Click the arrow icon that appears then ‘Hide Column’.

image

If you want to see how this would look for other users when they login, go to Access Rules, click the users dropdown then ‘View As’ another user.

image

I can log in as Pom Braid and confirm the user sees their interactions in our new, filtered table.

Let me know if you have any questions on this! I hope it helps what you are trying to accomplish.

You can’t get the current user in a non-trigger formula.

I suggest you make a summary table grouping your main table by the created_by column, and then use widget linking so that the main table ‘selects by’ the summary table. The summary table will give you a list of all unique emails, and clicking on one of them will filter the main table by that email. You still have to manually select your own email, but otherwise it should work quite nicely.

1 Like