I was working on a groovy script to update all the user variables at once without setting those from the ‘User Preferences’. The code worked for the only user who triggered the rule; i.e. the user variable was updated for admin only if the admin triggered the rule, while for the rest of the users, the variables were not updated. Can anyone give any input/suggestion on how to fix the code to make it work for all the users, i.e. if admin triggers the rule, the variables should be updated for all the users and not the admin only.
Below is the code:
//Initial setup of User Variables
//Created: Sep
String userName = operation.user.getName(); //get username for logged in user
println userName
/*** Set user variables based on username or default ****/
Map userVarMap = [
1: [username:'default',
'Years-PMYear':'FY21',
'Period-Month':'Sep',
],
]
String userVarString = userVarMap.find { it.value.username == userName }?.value // find a single entry for logged in user
if (userVarString == null) {
userVarString = userVarMap.find { it.value.username == 'default' }?.value // find default entry if user not found
}
println "User Variables for $userVarString"
Map userMap=[:]
userMap += userVarString.replaceAll('\\{|\\}', '').split(',').collectEntries { entry ->
def pair = entry.split('=')
[(pair.first().trim()): pair.last().trim()]
} // convert userVarString entry back to Map object type
for (i in userMap) {
if(i.key == 'username')
continue
String mapKey = i.key.toString()
def dimPair = mapKey.split('-')
def dimName = dimPair.first().trim()
def userVar = dimPair.last().trim()
String userVarValue = i.value.toString()
Dimension appDim = operation.application.getDimension(dimName)
UserVariable appUserVar = operation.application.getUserVariable(userVar)
Member appUserVarMember = appDim.getMember(userVarValue)
String status = operation.application.setUserVariableValue(appUserVar,appUserVarMember)
println "User Variable set as Dimension: $dimName, User Variable: $userVar, User Variable Value: $userVarValue"
}