Friday, August 27, 2021

DMF import records for multiple companies

 Say, we need to import Bank reconciliation records from an Excel file for many target companies at once.

The standard DMF approach does not allow to do so via DMF data entities out of the box.

However, we can easily customize Data Entity copyCustomStagingToTarget method to loop through all companies encountered in an imported file lines.

/// <summary>
    /// Performs a custom copy from the staging table to the target environment.
    /// </summary>
    /// <param name = "_dmfDefinitionGroupExecution">The definition group.</param>
    /// <returns>A container of counts of [new records, updated records].</returns>
    /// <remarks>
    /// When doing set-based inserts, the full defaulting logic from LedgerJournalTrans is not
    /// run. In order to get full defaulting, row-by-row processing must be performed. Since
    /// this method is called specifically from the DIXF framework, the entity in
    /// DIXF can be marked as AllowSetBased=False in order to force row-by-row defaulting
    /// and validation. The trade off is a significant degradation in copy performance.
    /// </remarks>
    public static container copyCustomStagingToTarget(DMFDefinitionGroupExecution _dmfDefinitionGroupExecution)
    {
        CDPBankReconStaging     staging;
        Set                     companySet;
        DMFStagingValidationLog log;


        log.skipDataMethods(true);
        delete_from log
            where 
            log.DefinitionGroupName == _dmfDefinitionGroupExecution.DefinitionGroup && 
            log.ExecutionId         == _dmfDefinitionGroupExecution.ExecutionId;


        update_recordset staging setting TransferStatus = DMFTransferStatus::NotStarted
            where
            staging.TransferStatus  == DMFTransferStatus::Validated &&
                staging.DefinitionGroup == _dmfDefinitionGroupExecution.DefinitionGroup &&
                staging.ExecutionId     == _dmfDefinitionGroupExecution.ExecutionId;

        int64 updatedRecords = 0;
        int64 newRecords = 0;
        // Validating companies
        myBankReconDE_Helper::validateCompany(_dmfDefinitionGroupExecution);
        // Getting the company list to loop through
        companySet = myBankReconDE_Helper::getStagingCompanySet(_dmfDefinitionGroupExecution);

        // the party begins here!
        SetEnumerator se = companySet.getEnumerator();
        while (se.MoveNext())
        {
            SelectableDataArea currCompany = se.current();
            changecompany(currCompany)
            {
                ttsbegin;

                myBankReconDE_Helper::validateTransactionCurrency(currCompany, _dmfDefinitionGroupExecution);
                // do any other necessary logic
                //...
                newRecords += myBankReconDE_Helper::createBankAccountTransactions(currCompany, _dmfDefinitionGroupExecution);
                // making DMF happy
                myBankReconDE_Helper::updateStagingTransferStatusToCompleted(currCompany, _dmfDefinitionGroupExecution);

                ttscommit;
                // Posting
                myBankReconDE_Helper::reconcileAccountStatement(currCompany, _dmfDefinitionGroupExecution);
            }
        }

        return [newRecords, updatedRecords];
    }

Getting company list

/// <summary>
    /// gets a set of all legal entities present in the staging
    /// </summary>
    /// <param name = "_dmfDefinitionGroupExecution"></param>
    /// <returns></returns>
    public static Set getStagingCompanySet(DMFDefinitionGroupExecution _dmfDefinitionGroupExecution)
    {
        myBankReconStaging staging;
        Set companySet = new Set(Types::String);
        while select myCompany from staging
            group by myCompany
            where
             staging.DefinitionGroup == _dmfDefinitionGroupExecution.DefinitionGroup &&
             staging.ExecutionId     == _dmfDefinitionGroupExecution.ExecutionId &&
            (staging.TransferStatus == DMFTransferStatus::NotStarted || staging.TransferStatus == DMFTransferStatus::Validated)
        {
            companySet.add(staging.myCompany);
        }
        return companySet;
    }

No comments: