11-16-2021 12:55 PM - last edited on 05-02-2023 10:45 AM by JackLacava
Solved! Go to Solution.
11-17-2021 05:11 AM - edited 11-17-2021 08:12 AM
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
11-17-2021 06:18 AM
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)
11-16-2021 04:49 PM
Yes, you can use the journal event handler for this.
11-17-2021 05:11 AM - edited 11-17-2021 08:12 AM
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
11-17-2021 06:18 AM
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)
11-17-2021 08:10 AM
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
11-17-2021 08:16 AM
Hi Christian, I see the use of LINQ more and more. is there a good resource to learn the syntax?
11-17-2021 08:39 AM
I found all the information on the internet. The trick is the usage of the bold part
Microsoft has a lot of information on its website, but mainly for c#, but if you know how the bold part works, it is easy to adapt for VB.net.