Forum Discussion

Richard_Mayo's avatar
Richard_Mayo
New Contributor III
10 months ago

Pass in parameters to a public function

Morning All, I'm working on a new solution to avoid having many, many separate confirmation rules with their own code which is almost identical in each case, and instead move to using a public func...
  • Henning's avatar
    10 months ago

    Hi,

    just add the parameter in whichever form you need to the function and pass it on like that. E.g. by adding myParameterDictionary  as an object.

    Public Function SecondFunction(ByVal si As SessionInfo, ByVal globals As BRGlobals, ByVal api As FinanceRulesApi, ByVal args As FinanceRulesArgs, ByVal myParameterDictionary As Object) As Object

    You then call this function like that:

    SharedRule.SecondFunction(si,globals,api,args,myParameterDictionary)

    If you do not get it to work right away, maybe try passing on a "ByVal mySingleStringParameter as String" first and write that into the error log. 

  • RobbSalzmann's avatar
    10 months ago

    Richard_Mayo Business Rules are Namespaces.  Namespaces contain Classes.  Classes contain Methods (functions and subs).  all of this lends itself well for your Object Oriented Programming (OOP) goal here of Code Reuse.

    Code reuse is a central design philosophy of proper OOP coding. 

    Here is a sample way to use this design format to create parameterized, reusable Confirmation Rule logic.  Doing it this way can significantly reduce development time and maintenance effort by reducing code complexity and repetition.

    Here is a very simple use of a Finance rule to containerize a set of parameterized Confirmation Rule functions:

    Namespace OneStream.BusinessRule.Finance.ConfirmationRules
    	Public Class CubeDataValidations
    		Public Function ValidateCubeDataMonthly(si As SessionInfo, api As FinanceRulesApi, args As FinanceRulesArgs, accountName As String) As (result As String, message As String)
    		    Try
    		        If accountName.Equals("SomeAccount", StringComparison.CurrentCultureIgnoreCase) Then  
    		            Return ("Pass", "Validation succeeded")
    		        Else
    		            Return ("Warning", "Account name does not match 'SomeAccount'")
    		        End If
    		    Catch ex As Exception
    		        ErrorHandler.LogWrite(si, New XFException(si, ex))
    		        Return ("Fail", "An error occurred during validation")
    		    End Try
    		End Function
    	End Class
    End Namespace
    

    This code is then consumed repetitively in Confirmation Rules:

     

  • RobbSalzmann's avatar
    RobbSalzmann
    10 months ago

    Hi Richard_Mayo  you got it!.

    The return type of ValidateCubeDataMonthly is called a "Tuple".  A tuple is a nice way to return a few things in one neat package when referencing a custom object at both ends of the code isn't feasible.

      In your new case you've created a tuple consisting of a string result, a string message, and a boolean value for passFail.
    It looks like this:

     

    (result As String, message As String, passfail as boolean)

     

    and it is assigned/returned like this:

     

    Dim result as String = "this is the result"
    Dim message as String = "This is the message"
    Dim isPass as Boolean = False 'this validation failed
    
    Return (result, message, isPass)
    

     

    Make sure you update the method signature of your function to use the new tuple:

     

    Public Function ValidateCubeDataMonthly(si As SessionInfo, api As FinanceRulesApi, args As FinanceRulesArgs, accountName As String) As (result As String, message As String, passfail as Boolean)

     

    Then in your calling code, in the confirmation rule, assign and consume the new tuple:

    Dim validationResult as (result As String, message As String, passfail As Boolean)
    
    validatinResult = cubeData.ValidateCubeDataMonthly(si, api, args, "SomeAccountName")