[SalesForce] Embed Analytics Dashboard with Filter Logged In User

I want to embed a Lightning Analytics Dashboard and apply a filter to display a filtered set of records from my dataset in a dashboard. In this situation Case records, where filtered where Case's owner is the currently logged in user, so Case.OwnerId == CurrentUserId

{
    "datasets": {
        "Case_Dataset": [
            {
                "fields": [
                    "Case_Owner.Id"
                ],
                "filter": {
                    "operator": "matches",
                    "values": [
                        "$CurrentUser.Id" // ???
                    ]
                }
            }
        ]
    }
}

Can't figure out the merge-syntax for Logged In User's Id (Like UserInfo static class in Visualforce…)

I see there are fields for record context, like $Name, or $Id

UPDATE

Trying to attack the problem at the SOQL/SAQL level, I have tried to create a new dashboard, and instead, add the filter to the SAQL like so:

q = load "Case_Dataset";
q = filter q by 'Case_Owner.Id' matches "\'!{User.Id}\'";

Which works, when I run the query it doesn't throw an error at least.

When editing the overall JSON for the dashboard (pressing, cmd-e) I see:

lens_1": {
         "type": "saql",
         "query": "q = load \"Case_Dataset\";\nq = filter q by 'Case_Owner.Id' matches \"\\'0051N0000075pHRQAY\\'\"; ...",
...

In the query field of my step, looks like the string-value of my user-id has been merged! But why would it display my user id if I am using a merge-field? Will it get reflected when the dashboard is rendered by another user?

Interestingly enough, if I close the dashboard without saving and reload it I see the following (in the query field of the step?!)

\nq = filter q by 'Case_Owner.Id' matches \"\\'!{User.Id}\\'\"

This was inspired by this post – so I am going to try to change the step type to SOQL.

UPDATE

Working with Salesforceforceblogger's great series on "bindings" has provided a lot of insight into how I should be going about this – so going forward I'm digging in deeper with that and will post results here once I figure it out. Also found documentation on bindings

RESULTS

I will leave this question open, as I would like anyone to post more concrete examples (especially since "global dashboard filters", as I could not get that working).

I ultimately create a SOQL query step, that used the !{User.Id} merge field to select the logged in user's ID, and filtered my Case records where OwnerId field matched.

Example (from dashboard editor, cmd-e, edit JSON to insert):

"logged_in_user": {
  "groups": [],
  "numbers": [],
  "query": "select id from User where id = '!{User.Id}'",
  "selectMode": "single",
  "strings": [
    "Id"
  ],
  "type": "soql"
},

I then used binding in the various step's SAQL queries to filter the results for each query like so:

\nq = filter q by {{column(logged_in_user.result, [\"Id\"]).asEquality(\"OwnerId\")}};

Note: It's possible to add this query in the SAQL editor (but you should not add the \n or \ escape characters around the quotation marks.

So just adding the following line in the SAQL editor works as well.

q = filter q by {{column(logged_in_user.result,["Id"]).asEquality("OwnerId")}};

On the global filters:

Despite trying a TON of different combinations with the global dashboard filter, none would work correctly, for example:

"filters": [
            "label":"Current User",
            "fields": [
                "OwnerId"
            ],
            "dataset": "PLsvc_Case_Dataset",
            "operator": "matches",
            "locked":true,
            "value": "{{column(logged_in_user.result, [\"Id\"]).asObject()}}"

        ],

I also tried with cell() and asString() bindings, wrapping the "value":[...] as an array, etc. etc. And none of it worked the same!

Compact Filter Syntax:

"query": {
   ...
   "filters": [
       "OwnerId",
       [ 
          "{{column(logged_in_user.result, [\"Id\"]).asObject()}}"
       ],
       "in"  
   ]
}

Note: Compact filter syntax is different the SAQL, and depending on how you add the widget, or if you access the "SAQL Editor" for the step in the tool, the compact filters are converted to SAQL in the overall JSON editor for the dashboard!?!

Thanks!

Best Answer

I ended up using a security predicate to filter the dataset for the dashboard (on the Lightning app Home page) to records where the owner was the running user, worked great! 'AccountOwner.LastName' == "$User.LastName"

Related Topic