javascript / filter script question

So I definitely am working on my javascript skills, and am stuck on something that could be really obvious - I’m not sure!

The dashboard that I’m working on has a filter connected to a data input cube with results which are documented in connection with the user id of the end user logged in.

On page ready, I just want to grab the user id, clear the filter, and then enter the user id into my filter which is connected to my visualizations / etc so that the user just sees their own data.

I know that I’m almost there, and not sure what I need to do to update the filter. I’ve experimented with different things from the script library, this is what I have in there now…any ideas?:

//get Login ID
var currentLogin = dundas.context.currentSession.accountId;

console.log(currentLogin);
// Get the parent view.
var parentView = this.parentView;

// Get the view parameters.
var viewParameters =
parentView.control.viewParameters;

// Using the library, get the view parameter based on it’s name
var viewParameter =
this.parentView.control.getViewParameterByName(“viewParameter1”);

// Remove all values and tokens from the parameter value
viewParameter.parameterValue.clearTokens();
viewParameter.parameterValue.clearValues();

//this is where I’m stuck, the piece that is not working, and so after this I’m not really sure which pieces of code I need to add / remove / etc.

//set the value of view parameter to = the current Login ID
//viewParameter.parameterValue = currentLogin

// Load the member value.
currentLogin.loadMemberValue();

// Push the parameter value to the values list.
viewParameter.parameterValue.values.push(currentLogin);

// Sets the value of lastModifiedTime to now
viewParameter.invalidateParameterValueLastModifiedTime();

// Update all the connected adapters with the newly modified values
// Includes data visualizations and filter controls
viewParameter.refreshAllAdapters();

1 Like

Hello @heather,

From what I know if you try to edit a filter by script on the ready or loading event you have to do it without .parentView.

1 Like

Not answering your question exactly but why do you need a javascript function to do this?

You can do this in the cube itself - you can add a filter transform and set it to filter to current user - something like this article.

And in relation to your script - @costin.manea is right. You can’t use ‘this’ in the on ready interaction.
Instead of var parentView = this.parentView; I would do var parentView = dundas.context.baseViewService.currentView; and then just not use this after that.

To set the value of the filter, you need to create a member value based on the hierarchy name. I don’t know what that is so I’ll just call it ‘user’. You will need to modify as necessary.

Instead of

//set the value of view parameter to = the current Login ID
//viewParameter.parameterValue = currentLogin

// Load the member value.
currentLogin.loadMemberValue();
// Push the parameter value to the values list.
viewParameter.parameterValue.values.push(currentLogin);

You should do something like the following
//set the value of view parameter to = the current Login ID

var myMemberValue = new dundas.data.MemberValue({
“hierarchyUniqueName”: “user”,
“levelUniqueName”: “user”,
“memberKind”: “Regular”,
“uniqueName”: currentLogin+ “.user”
});
// Push the parameter value to the values list.
viewParameter.parameterValue.values.push(myMemberValue);

2 Likes

As a note, you can use this in both the ‘ready’ and ‘loading’ events but the this is actually the dashboard adapter.

So you can just say var parentView = this;. This is preferred to using the context because if the dashboard is embedded you’ll get the wrong one.

As well, refreshAllAdapters should definitely not be used in the ‘ready’ event as it will cause a second load (double refresh). Parameter modification should occur in the ‘loading’ event, and do not call refresh as they will all refresh before ‘ready’ fires.

2 Likes

Hi Heather,

If I understand correctly, you have stored some data in the Data Cube along with the GUIDs of the user. On your Dashboard you have a filter that filters the data from the Data Cube based on GUID of the user logged in and shows the corresponding data in a visualization. If this is the case, you can use the following script:

// Get the current account ID
var currentLogin = dundas.context.currentSession.accountId;

 //Get the Base View Service to access the View Parameters
    var baseViewService = this.getService("BaseViewService");

 //Get the View Parameter. In this case only one View Parameter is available, e.g., viewParameter1
    var myParameter = baseViewService.currentView.control.viewParameters.filter(function(item) 
                    {
    			return item.name === "viewParameter1";
    		})[0];

 //Prepare a UniqueName for filterValue for the currentLogin. Here UserID is the field from Data Cube that holds GUID of the user. Update this according to your field.
    var uniqueNameValue = currentLogin + "." + "UserID";
    label1.labelText = uniqueNameValue;

 // Prepare filter value
    var filterValue = new dundas.data.MemberValue({
    		hierarchyUniqueName: "UserID",
    		levelUniqueName: "UserID",
      		uniqueName: uniqueNameValue });

 // Set filter value on the view parameter
    myParameter.parameterValue.token = null;
    myParameter.parameterValue.values.length = 0;
    myParameter.parameterValue.values.push(filterValue);

 // Refresh data visualization with new filter values. 
    myParameter.refreshAllAdapters();

Please read the comment above from Terrence regarding using refreshAllAdapters before putting this code in the ready event.

Please note that the same functionality can be achieved through UI as well. Please follow the link below for more information:
https://www.dundas.com/support/learning/documentation/administration-configuration-customization/how-to/using-security-hierarchy-to-filter-data-by-user

I hope it helps.

Regards,
Razi

Thank you all so much for this feedback, it’s definitely helpful. I am trying to figure out the best way to use a filter right on the cube now. Unfortunately, I think it’s funny that on a data input cube you can capture userid automatically, but have to script seperately to capture display name. And the tokens available include a display name token, but not a user id token that populates when you look at filter settings. I am sure I can modify, it just seems like it’d be a useful thing to be able to connect those things without having to modify / edit. I definitely will update whichever solution ends up working best.