Error message when you open a form where user variable is not set properly.
Error message when you open a dashboard where user variable is not set properly on an underlying form.

Who loves those error messages ? While user variables are providing a great mechanism to increase user experience, initial setup of user variables are always problematic in a way that it can slow down user adaption to EPM applications. There is no out of the box way of customizing user variable selections for users and EPM product is not providing a screen to set user variables’ values on behalf of users. Luckily, Groovy is providing a way that you can set user variables with minimum effort from users like clicking a form attached to a navigation flow. You can divert users to click on something like a form to set their user variables automatically. Sounds nice, please continue reading.

Example Navigation Flow

I will attach groovy script to the form opening under this navigation flow to continue my explanation, however you can potentially build the process as a step in taskflows, action menu item to be selected somewhere or any sub tabs in your existing navigation flows.

Suppose you have a user variable from Entity Dimension called UEntity. High level steps in the script will be

1- Check UEntity is already selected
2- Retrieve list of Entities User have access to
3- Set UEntity to the first Entity that user is having access to

if(operation.application.getUserVariable('UEntity').getValue()==null){
  //UEntity User Variable is not selected
}
else
{
  //UEntity User Variable is selected to operation.application.getUserVariable('UEntity').getValue()
}

This code block is simply to check if there was any selection earlier in the User Variable. Let’s focus on what to do if this is the case and you want to capture the entities that user is having access to.

Cube cube = operation.application.getCube("CubeName")
Dimension dim = cube.getApplication().getDimension('Entity');
String memberfunction = 'IDescendants(All Entities)'
def mems = dim.getEvaluatedMembers(memberfunction,cube)

Usage of .getEvaluatedMembers() method is a key in this script. It has 2 parameters, second one is only the cube, first one is memberfunction script which is ‘IDescendants(All Entities)’. Whilst this member function refers to all Entities underneath that top member, it returns list of members with the security rights that user executing the script holds. In other words, if I run this method as an admin user I receive full list of entities, whereas user having access to only single entity will retrieve single entity from this evaluation.

if(mems.size()>0){
operation.application.setUserVariableValue(operation.application.getUserVariable('UEntity'),mems[0])
}

As you need to select only 1 entity even when you have more than many, script here uses mem[0] which is the first member returning from earlier .getEvaluatedMembers() method.