Lock a scenario with groovy is just a fancy way of saying move a scenario member under a parent member with security setting.
I have done this at a client before and they wanted a rule to configure the security rather than going through the native security setting in EPBCS at the end of a forecast or budget cycle.
The setup
The security is set up once on two parents, Open Scenarios and Closed Scenarios. Users get Write on the descendants of Open Scenarios and Read on the descendants of Closed Scenarios. Each scenario sits under Plan Scenarios as the base member, with a shared member under Open or Closed. To lock a scenario, you move the shared member from Open to Closed.
Here is my example. Plan A and Plan B are both open:

You can’t just add Plan A under Closed Scenarios. A shared member gets its access from its base member and the least restrictive access (Write) wins if it is under two different parents. If Plan A is under both parents, users still have Write. That’s why the rule removes it from the old parent.
Here is the script:
/*RTPS: {RTP_Scenario} {RTP_Lock}*///Define CubeCube cube = operation.application.getCube("Plan1")Dimension dimScenario = operation.application.getDimension("Scenario", cube)//What the user pickedString sScenario = rtps.RTP_Scenario.member.nameboolean bLock = rtps.RTP_Lock.enteredValue == "1"//Lock moves the scenario from Open to Closed, unlock moves it backString sFrom = bLock ? "Open Scenarios" : "Closed Scenarios"String sTo = bLock ? "Closed Scenarios" : "Open Scenarios"Member mbrFrom = dimScenario.getMember(sFrom, cube)Member mbrTo = dimScenario.getMember(sTo, cube)//Add it as a shared member under the new parent, shared in every cubeif (!mbrTo.hasChild(sScenario)) { Map<String, Object> mapNew = mbrTo.newChildAsMap(sScenario) mapNew["Data Storage"] = "Shared" operation.application.getCubes().each { Cube c -> mapNew["Data Storage (" + c.name + ")"] = "Shared" mapNew["Aggregation (" + c.name + ")"] = "+" } dimScenario.saveMember(mapNew)}//Remove the shared member from the parent it is leavingif (mbrFrom.hasChild(sScenario)) { mbrFrom.getChild(sScenario).delete()}println sScenario + (bLock ? " locked" : " unlocked") + " by " + operation.user.fullName
RTP_Scenario is a member prompt limited to @Relative(“Plan Scenarios”, 0), so users can only pick a base scenario. RTP_Lock is an integer prompt, 1 to lock and 0 to unlock.
newChildAsMap copies the properties of the parent, so that’s why I set Data Storage to Shared for the default Data Storage. It needs to be applied to all cubes, if you miss one, you get “Shared members must have Shared Data Storage for all cubes” or “A Base Member cannot be changed to a Shared Member”.
The rule adds the new shared member first, then deletes the old one. If the add fails, the scenario is still under the old parent. I found this out during testing, when my first attempt deleted the member first and left Plan A outside of both Open Scenarios and Closed Scenarios.
getChild returns the shared member under that parent, so delete() only removes that one. The base member under Plan Scenarios stays along with its data.
The Result



Plan A is now under Closed Scenarios, and Plan B is still open.
Be careful with the security and the refresh. Don’t give users Write on Plan Scenarios or on the base members, otherwise the least restrictive access wins again.
The rule changes metadata, so run it as an admin and refresh the database afterwards is recommendated. Otherwise, the new access might not take effect.
Hope this is helpful!
Leave a Reply