Forum Discussion

ChristianW's avatar
ChristianW
Valued Contributor
4 years ago

Copy a journal with a business rule

Hi all Can I copy a journal using a business rule? Cheers
  • ChristianW's avatar
    4 years ago

    Yes, it is possible, here is a sample:

     

    Sub CopyJournal(ByVal si As SessionInfo)
    	Try
    		'retrieve the names of the workflow parameters and concatenate them together
    		Dim profilePostfix As String  = brapi.Workflow.Metadata.GetProfile(si,si.WorkflowClusterPk.ProfileKey).Name
    		Dim scenarioPostfix As String = brapi.Finance.Members.GetMemberName(si,dimtype.Scenario.Id, si.WorkflowClusterPk.ScenarioKey)
    		Dim timePostfix As String     =  brapi.Finance.Members.GetMemberName(si, dimtype.Time.Id, si.WorkflowClusterPk.TimeKey)
    		Dim strPostfix As String = $"{profilePostfix}_{scenarioPostfix}_{timePostfix}"
    
    		'The name of the journal to copy
    		Dim journalName As String ="Tax Accruals_Houston.journals_Actual_2022M3"
    		Dim oldJournalObject As JournalEx = BRApi.Journals.Metadata.GetJournalOrTemplate(si, journalName)
    		
    		'Remove the postfix from the old journal name to get the name
    		Dim strOldName As String = oldJournalObject.Header.Header.Name				
    		Dim oldProfileName As String = brapi.Workflow.Metadata.GetProfile(si,oldJournalObject.Header.Header.WorkflowProfileID).Name
    		strOldName = strOldName.Remove(strOldName.IndexOf(oldProfileName))
    
    		'create a copy of the journal header
    		Dim journalObjectHeader As New JournalHeader(oldJournalObject.Header.Header)
    		
    		'Update the required parameters
    		journalObjectHeader.Name = $"RF_{strOldName}{strPostfix}"
    		journalObjectHeader.Description    = $"Roll Forward from {oldJournalObject.Header.Header.name}: {oldJournalObject.Header.Header.Description}"
    		journalObjectHeader.UniqueID = Guid.NewGuid()
    		journalObjectHeader.MemberIds.Scenario = si.WorkflowClusterPk.ScenarioKey
    		journalObjectHeader.MemberIds.Time = si.WorkflowClusterPk.TimeKey
    		journalObjectHeader.WorkflowProfileID = si.WorkflowClusterPk.ProfileKey
    						
    		'Journal status as working
    		journalObjectHeader.JournalStatus = JournalStatus.Working
    
    		'Create a copy of the journal line item using linq
    		Dim journalObjectLineItems As list(Of JournalLineItem) = oldJournalObject.LineItems.Select(Function(x) New JournalLineItem(x.LineItem)).tolist
    		'Create a new journal object
    		Dim journalObject As New Journal(journalObjectHeader, journalObjectLineItems)
    		'Save it
    		BRApi.Journals.Metadata.SaveJournalOrTemplateUsingIds(si, journalObject, False, True)
    				
    	Catch ex As Exception
    		Throw ErrorHandler.LogWrite(si, New XFException(si, ex))
    	End Try					
    End Sub

     

  • ChrisLoran's avatar
    4 years ago

    This is very useful especially when you want recurring adjustments.  However one word of warning to anyone using automated rules top copy journals:   Be very careful you don't accidentally use such tools to roll-forward AutoReversing journals, because then you end up with the auto-reversals being posted in all future periods, in a way that is stuck in the database and you cannot remove them easily.  So anyone who wants to implement such a BR , I would recommend the following checks:  Check that the JournalType ( in the JournalSummaryInfo ) is not one of JournalType.AutoReversal,  or JournalType.AutoReversing