Forum Discussion
AlainFuhrer
OneStream Employee
4 years agoJournal validation
Is there a way to validate the Total Debit or Total Credit amount of a journal and send back a warning/fail message if the total is over a certain threshold when we try to post/save the journal?
Th...
- 4 years ago
here is a sample:
Public Class MainClass Public Function Main(ByVal si As SessionInfo, ByVal globals As BRGlobals, ByVal api As Object, ByVal args As JournalsEventHandlerArgs) As Object Try Dim returnValue As Object = args.DefaultReturnValue args.UseReturnValueFromBusinessRule = False args.Cancel = False If args.OperationName = BREventOperationType.Journals.PostJournal Then Me.HandlePostJournal(si, globals, api, args) End If Return returnValue Catch ex As Exception Throw ErrorHandler.LogWrite(si, New XFException(si, ex)) End Try End Function #Region "Post Helpers" Private Sub HandlePostJournal(ByVal si As SessionInfo, ByVal globals As BRGlobals, ByVal api As Object, ByVal args As JournalsEventHandlerArgs) '------------------------------------------------------------------------------------------------------------ 'Reference Code: HandlePostJournal ' 'Description: ' '------------------------------------------------------------------------------------------------------------ Try If (args.IsBeforeEvent = True) Then Dim scenarioTypeId As Integer = BRApi.Workflow.General.GetScenarioTypeId(si, si.WorkflowClusterPk) Dim journalID As Guid = CType(args.Inputs(0), Guid) Dim journal As JournalEx = BRApi.Journals.Metadata.GetJournalOrTemplate(si, journalID) If Not journal Is Nothing Then Dim debitAmount As Decimal = 0 Dim creditAmount As Decimal = 0 For Each oneJournal As JournalLineItemEx In journal.LineItems debitAmount += oneJournal.LineItem.DebitAmount.Amount creditAmount += oneJournal.LineItem.CreditAmount.Amount Next If Math.Abs(debitAmount - creditAmount) > 100 Then Throw New Exception("Don't mess with Debit and Credit") End If End If End If Catch ex As Exception Throw ErrorHandler.LogWrite(si, New XFException(si, ex)) End Try End Sub #End Region End Class - 4 years ago
After the exception the screen is not updating and the journal isn't rejected but still approve state.
But you can force the reject with the following line just before the Throw line
brapi.Journals.Process.ExecuteReject(si, journalID)
ChristianW
OneStream Employee
4 years agoYes, you can use the journal event handler for this.
- ChristianW4 years ago
OneStream Employee
here is a sample:
Public Class MainClass Public Function Main(ByVal si As SessionInfo, ByVal globals As BRGlobals, ByVal api As Object, ByVal args As JournalsEventHandlerArgs) As Object Try Dim returnValue As Object = args.DefaultReturnValue args.UseReturnValueFromBusinessRule = False args.Cancel = False If args.OperationName = BREventOperationType.Journals.PostJournal Then Me.HandlePostJournal(si, globals, api, args) End If Return returnValue Catch ex As Exception Throw ErrorHandler.LogWrite(si, New XFException(si, ex)) End Try End Function #Region "Post Helpers" Private Sub HandlePostJournal(ByVal si As SessionInfo, ByVal globals As BRGlobals, ByVal api As Object, ByVal args As JournalsEventHandlerArgs) '------------------------------------------------------------------------------------------------------------ 'Reference Code: HandlePostJournal ' 'Description: ' '------------------------------------------------------------------------------------------------------------ Try If (args.IsBeforeEvent = True) Then Dim scenarioTypeId As Integer = BRApi.Workflow.General.GetScenarioTypeId(si, si.WorkflowClusterPk) Dim journalID As Guid = CType(args.Inputs(0), Guid) Dim journal As JournalEx = BRApi.Journals.Metadata.GetJournalOrTemplate(si, journalID) If Not journal Is Nothing Then Dim debitAmount As Decimal = 0 Dim creditAmount As Decimal = 0 For Each oneJournal As JournalLineItemEx In journal.LineItems debitAmount += oneJournal.LineItem.DebitAmount.Amount creditAmount += oneJournal.LineItem.CreditAmount.Amount Next If Math.Abs(debitAmount - creditAmount) > 100 Then Throw New Exception("Don't mess with Debit and Credit") End If End If End If Catch ex As Exception Throw ErrorHandler.LogWrite(si, New XFException(si, ex)) End Try End Sub #End Region End Class- AlainFuhrer4 years ago
OneStream Employee
After the exception the screen is not updating and the journal isn't rejected but still approve state.
But you can force the reject with the following line just before the Throw line
brapi.Journals.Process.ExecuteReject(si, journalID)
- ChristianW4 years ago
OneStream Employee
Here is a shorter version of the function using LINQ:
#Region "Post Helpers" Private Sub XFR_HandlePostJournal(ByVal si As SessionInfo, ByVal globals As BRGlobals, ByVal api As Object, ByVal args As JournalsEventHandlerArgs) '------------------------------------------------------------------------------------------------------------ 'Description: ' 'Usage: ' 'Changed By: Onestream 'Date Created: 11-17-2021 '------------------------------------------------------------------------------------------------------------ Try If (args.IsBeforeEvent = True) Then Dim scenarioTypeId As Integer = BRApi.Workflow.General.GetScenarioTypeId(si, si.WorkflowClusterPk) Dim journalID As Guid = CType(args.Inputs(0), Guid) Dim journal As JournalEx = BRApi.Journals.Metadata.GetJournalOrTemplate(si, journalID) If Not journal Is Nothing Then Dim deltaDC As Decimal = math.Abs(journal.LineItems.sum(Function(x) x.lineItem.DebitAmount.amount - x.lineItem.CreditAmount.amount)) If deltaDC > 100 Then Throw New Exception($"Don't mess with Debit and Credit the difference {deltaDC:N2} is larger than 100") End If End If End If Catch ex As Exception Throw ErrorHandler.LogWrite(si, New XFException(si, ex)) End Try End Sub #End Region- MarcR4 years agoContributor II
Hi Christian, I see the use of LINQ more and more. is there a good resource to learn the syntax?
Related Content
- 3 years ago
- 3 months ago
- 3 years ago