Dayalan Punniyamoorthy Blog

Friday, May 10, 2024

Setting User variables values for the first time using Groovy rule!



User variables function as filters within forms, allowing users to narrow their focus to specific members, like a department. Prior to linking a user variable to a form, you need to establish the user variable. When designing forms with user variables, users are required to initially designate values in preferences for the variable prior to accessing the forms. Subsequently, users can modify the variable within the form only if it's dynamic. If not, they must persist in setting the variable in preferences. For instance, if a user variable named "Cost Center" is created, users must designate a Cost Center before engaging with the form.

When users interact with a form for the first time, they set their preferred variable, such as a department or division, in their preferences. This initial selection is crucial as it helps customize their form-viewing experience.

After the initial setup, users have the flexibility to update this variable. They can either go back to their preferences and make changes there, or they can directly adjust the variable within the form itself. This flexibility allows users to adapt their form interactions based on changing needs or contexts without needing to navigate away from the form.

Overall, this approach ensures that users have control over their viewing preferences and can tailor their experience to suit their individual requirements.

In this blog lets see how to set the User Variables for the first time using a Groovy Business rule, so the users don't have set them manually and can change it latter as per their desire or requirement. 

This rule will help to save lot of time and let the end users to use the application without any hurdle. 

The Complete Code:

This code sets the user variable for all the users  

/*-------------------------------------------------------------------------------*/

/* Set User Variable for all the Users                                           */

/*-------------------------------------------------------------------------------*/

 // Get the username for the logged-in user

String userName = operation.user.getName();

println "Updating the User Variables for the user : " + userName

 /* Set user variables for all the users */

// Define user variable mappings for different users

Map userVarMap = [

    1: [username:'default',

            'CC':'Total Cost Centres',

            'Account-Account':'Net Income',

            'Scenario-CompareScenario':'Forecast',

            'Version-CompareVersion':'Final',

            'Years-CompareYear':'FY25',

            'Currency-Currency':'USD',

            'Entity-Entity':'No ENTITY',

            'Years-ForecastYear':'FY25',

            'Period-Period':'Dec',

            'Entity-Review Entity':'No ENTITY',

            'Scenario-Scenario':'Actual',

            'Version-Version':'Working',

            'Years-Years':'FY24',

            'Intercompany':'Total Intercompany'],

   This code sets the user variable for the specified the user

    // Set user variables for a specific user

    2: [username:'testuser@testdomain.com',

        'Years-Years':'FY26']

]

 // Find the user variable string for the logged-in user

String userVarString = userVarMap.find { it.value.username == userName }?.value

if (userVarString == null) {

    // Use default entry if user-specific entry not found

    userVarString = userVarMap.find { it.value.username == 'default' }?.value

}

 println "User Variables for $userVarString"

 // Convert user variable string entry back to Map object type

Map userMap = [:]

userMap += userVarString.replaceAll('\\{|\\}', '').split(',').collectEntries { entry ->

    def pair = entry.split('=')

    [(pair.first().trim()): pair.last().trim()]

}

 // Set user variables for the application

for (i in userMap) {

    // Skip 'username' entry

    if(i.key == 'username')

        continue

       // Extract dimension and user variable information

    String mapKey = i.key.toString()

    def dimPair = mapKey.split('-')

    def dimName = dimPair.first().trim()

    def userVar = dimPair.last().trim()

    String userVarValue = i.value.toString()

     // Get dimension, user variable, and member objects

    Dimension appDim = operation.application.getDimension(dimName)

    UserVariable appUserVar = operation.application.getUserVariable(userVar)

    Member appUserVarMember = appDim.getMember(userVarValue)

   

    // Set user variable value and print status

    String status = operation.application.setUserVariableValue(appUserVar,appUserVarMember)

    println "User Variable set as Dimension: $dimName, User Variable: $userVar, User Variable Value: $userVarValue"

} 

User Variables before running the rule

 


Case 1: Running the rule for the specific User to update only a specific User variable. 



Job Details



User Variables after running the rule



Case 2: Running the rule for the all the Users for updating all the specified User Variables.


Job Details



User Variables after running the rule. 


Hope this Helps, Happy days on the Cloud!!!



2 comments:

  1. This is an excellent use-case for Groovy and addresses a user-experience issue that a lot of people complain about. Nice work!

    ReplyDelete