Journal validation

AlainFuhrer
New Contributor II
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?
Thanks
2 ACCEPTED SOLUTIONS

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

 

 

View solution in original post

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)

 

View solution in original post

6 REPLIES 6

ChristianW
Valued Contributor

Yes, you can use the journal event handler for this.

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

 

 

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)

 

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

Hi Christian, I see the use of LINQ more and more. is there a good resource to learn the syntax?

Marc Roest
OneStream consultant @Finext

ChristianW
Valued Contributor

I found all the information on the internet. The trick is the usage of the bold part

  • <collectionObject>.select(function(x) x.amount) <- returns a list of decimals
  • <collectionObject>.sum(function(x) x.amount) <- returns a decimals
  • <collectionObject>.findall(function(x) x.amount > 0) <- returns a list of decimals with positive numbers

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.