Copy a journal with a business rule

ChristianW
Valued Contributor

Hi all

Can I copy a journal using a business rule?

Cheers

2 ACCEPTED SOLUTIONS

ChristianW
Valued Contributor

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

 

View solution in original post

ChrisLoran
Valued Contributor

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

View solution in original post

5 REPLIES 5

ChristianW
Valued Contributor

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

 

People asked me, how to get a list of journals for a specific workflow/time/scenario. Here it is:

Dim wfClusterPk As New WorkflowUnitClusterPk()

wfclusterpk.ProfileKey = MyProfileKey
wfclusterpk.ScenarioKey = MyScenarioKey
wfclusterpk.TimeKey = MyTimeKey

Dim journalsCollection As JournalsAndTemplatesForWorkflow = BRApi.Journals.Metadata.GetJournalsAndTemplates(si,wfClusterPK)

For Each oJournal As Journalsummaryinfo In journalsCollection.Journals

…

Next

or for the actual workflow selection:

Dim wfClusterPk As WorkflowUnitClusterPk = si.WorkflowClusterPk

Dim journalsCollection As JournalsAndTemplatesForWorkflow = BRApi.Journals.Metadata.GetJournalsAndTemplates(si,wfClusterPK)

For Each oJournal As Journalsummaryinfo In journalsCollection.Journals

…

Next

 

Thanks this is great. Why line 16 is required if the strOldName is already getting only the Journal Name in Line 14. 

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))
journalObjectHeader.Name = $"RF_{strOldName}{strPostfix}"

 

Thanks
Krishna

ChristianW
Valued Contributor

I use it to remove workflow-scenario-time information from the real name. 

ChrisLoran
Valued Contributor

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