Dayalan Punniyamoorthy Blog

Sunday, May 7, 2023

EPBCS Groovy Rule series-2, One rule for multiple Data forms do the calculation!

In this second part of the blog, let's explore how to reuse the same Calculation Manager Groovy rule by attaching the rule to multiple data forms to perform different actions in each data form.

The Groovy Calculation Manager rule is dynamic, it gets the account member from the data form, and depending on the selected account member the rule will perform a specific calculation. 




We can use the same rule and attach it to multiple Data forms. 

Let's see how the rule looks,

// Capture the edited periods and products
Set<String> editedMembers = []
operation.grid.dataCellIterator({DataCell cell -> cell.edited}, MemberNameType.ESSBASE_NAME).each { DataCell cell ->
        //editedMembers << cell.periodName << cell.getMemberName("Product")
        editedMembers << cell.periodName << cell.getMemberName("Years") << cell.getMemberName("Product") << cell.getMemberName("Customer")
    }
if(editedMembers.size() == 0){
    println("No edited cells found!")
    return;
}
Set<String> editedAccounts = []
operation.grid.dataCellIterator({DataCell cell -> cell.edited}, MemberNameType.ESSBASE_NAME).each { DataCell cell ->
        editedAccounts << cell.getMemberName("Account")
    }

This script captures the edited periods and products in a grid and generates a calculation script based on the edited accounts.

First, it initializes two sets to store the edited members and edited accounts respectively. It then iterates through each data cell in the grid and adds the period name, years, product, and customer members to the editedMembers set if the cell has been edited. 

It also adds the account member to the edited accounts set.

String calcScript = ""
// Generate the calc script to calculate revenue for the products whose volume was edited
if (editedAccounts.contains("Account 1")) {
calcScript = """
    Fix(${cscParams(operation.grid.pov, editedMembers)})
    "OEP_Plan"
    (
       Some Operations
    )
    EndFix;"""
}

else if (editedAccounts.contains("Account 2")) {
calcScript = """
    Fix(${cscParams(operation.grid.pov, editedMembers)})
    "OEP_Plan"
    (
       Some Operations
    )
    EndFix;"""
}

else if (editedAccounts.contains("Account 3")) {
calcScript = """
    Fix(${cscParams(operation.grid.pov, editedMembers)})
    "OEP_Plan"
    (
       Some Operations
    )
    EndFix;"""
}

else if (editedAccounts.contains("Account 4")) {
calcScript = """
    Fix(${cscParams(operation.grid.pov, editedMembers)})
    "OEP_Plan"
    (
        Some Operations
    )
    EndFix;"""
}

else if (editedAccounts.contains("Account 5")) {
calcScript = """
    Fix(${cscParams(operation.grid.pov, editedMembers)})
    "OEP_Plan"
    (
         Some Operations
    )
    EndFix;"""
}

Next, it generates a calculation script based on the edited accounts. If the edited accounts set contains "Account 1", "Account 2", "Account 3", "Account 4", or "Account 5", it sets the calc script variable to a corresponding fix statement with some operations. Otherwise, it sets the calc script to an empty string.

if(calcScript == "") {

    println("No matching edited accounts found!")

    return;

}

println("The following calc script was executed by $operation.user.fullName: \n $calcScript")

return calcScript.toString()


Finally, it prints the calc script to the console and returns it as a string. If no edited cells or no matching edited accounts are found, it prints a corresponding message to the console and returns nothing.

We modified the % for May to 5% and saved the Data From. 


If you notice the rule ran only for the May FY24 and ran the specific calculation.

The Output is given below, only for the May FY24.


The complete program is here.

 Pretty cool right, Happy days ahead with Cloud!!!

No comments:

Post a Comment