Forum Discussion

AlainFuhrer's avatar
AlainFuhrer
New Contributor II
4 years ago

Journal 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...
  • ChristianW's avatar
    ChristianW
    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

     

     

  • AlainFuhrer's avatar
    AlainFuhrer
    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)