Create a custom export to excel template for the ledger journal form

 Today we have a requirement to show the custom template when we export the ledger journal from the form.


Below are the code that's helps to achieve this  :- 


using Microsoft.Dynamics.Platform.Integration.Office;


/// <summary>

/// The <c>LedgerDailyJournalExcelTemplate</c> is the supporting class for the Daily journal excel template.

/// </summary>

public class AVALedgerDailyJournalExcelTemplate extends DocuTemplateRegistrationBase implements LedgerIJournalExcelTemplate

{

    private const DocuTemplateName ExcelTemplateName = resourceStr(WGLedgerJournalLineEntryTemplate);

    private const DataEntityName LineEntityName = tableStr(LedgerJournalLineEntity);

    private const FieldName LineEntityJournalNum = fieldStr(LedgerJournalLineEntity, JournalBatchNumber);

    private const FieldName LineEntityDataAreaId = fieldStr(LedgerJournalLineEntity, dataAreaId);

    private const DataEntityName HeaderEntityName = tableStr(LedgerJournalHeaderEntity);

    private const FieldName HeaderEntityJournalNum = fieldStr(LedgerJournalHeaderEntity, JournalBatchNumber);

    private const FieldName HeaderEntityDataAreaId = fieldStr(LedgerJournalHeaderEntity, dataAreaId);

    private const GlobalObjectCacheScope LedgerDailyJournalCacheScope = 'LedgerDailyJournalCacheScope';

    private const int64 JournalValidationFailed = -1;

    private const int64 JournalNotValidated = 0;


    private static boolean getCacheValue(GlobalObjectCacheScope _scope, DataAreaId _dataAreaId, JournalId _journalId)

    {

        SysGlobalCache c;


        c = classfactory.globalCache();


        return c.get(_scope, _dataAreaId+_journalId);

    }


    private static void insertCacheValue(GlobalObjectCacheScope _scope, DataAreaId _dataAreaId, JournalId _journalId, boolean _value)

    {

        SysGlobalCache c;


        c = classfactory.globalCache();


        c.set(_scope, _dataAreaId+_journalId, _value);

    }


    public boolean isJournalTypeSupported(LedgerJournalType _ledgerJournalType)

    {

        return _ledgerJournalType == LedgerJournalType::Daily;

    }


    public DocuTemplateName documentTemplateName()

    {

        return ExcelTemplateName;

    }


    public void applyCustomTrimming(Excel.IWorkbookManager _templateManager, Excel.WorkbookSettingsManager _settingsManager, LedgerJournalTable _ledgerJournalTable)

    {

    }


    public Set supportedAccountTypes()

    {

        Set accountTypeSet = new Set(Types::Integer);


        accountTypeSet.add(LedgerJournalACType::Ledger);

        accountTypeSet.add(LedgerJournalACType::Bank);

        accountTypeSet.add(LedgerJournalACType::Cust);

        accountTypeSet.add(LedgerJournalACType::Vend);

        

        return accountTypeSet;

    }


    public Set supportedOffsetAccountTypes()

    {

        Set offsetAccountTypeSet = new Set(Types::Integer);


        offsetAccountTypeSet.add(LedgerJournalACType::Ledger);

        offsetAccountTypeSet.add(LedgerJournalACType::Bank);

        offsetAccountTypeSet.add(LedgerJournalACType::Cust);

        offsetAccountTypeSet.add(LedgerJournalACType::Vend);

        

        return offsetAccountTypeSet;

    }


    public boolean validateJournalForTemplate(LedgerJournalTable _ledgerJournalTable)

    {

        return LedgerJournalExcelTemplate::validateJournalForTemplate(_ledgerJournalTable, this);

    }


    public void registerTemplates()

    {

        this.addTemplate(

            OfficeAppApplicationType::Excel,

            ExcelTemplateName,

            ExcelTemplateName,

            literalStr("@AVAExtension:GeneralLedgerExcelTemplate"),

            literalStr("@AVAExtension:GeneralLedgerExcelTemplate"),

            NoYes::No,

            NoYes::No,

            NoYes::No);

    }


    public DataEntityName headerEntityName()

    {

        return HeaderEntityName;

    }


    public DataEntityName lineEntityName()

    {

        return LineEntityName;

    }


    public FieldName headerJournalBatchNumberFieldName()

    {

        return HeaderEntityJournalNum;

    }


    public FieldName headerDataAreaFieldName()

    {

        return HeaderEntityDataAreaId;

    }


    public FieldName lineJournalBatchNumberFieldName()

    {

        return LineEntityJournalNum;

    }


    public FieldName lineDataAreaFieldName()

    {

        return LineEntityDataAreaId;

    }


    public FilterCollectionNode appendHeaderEntityFilters(FilterCollectionNode _headerFilter, ExportToExcelFilterTreeBuilder _headerFilterBuilder)

    {

        return _headerFilter;

    }


    public FilterCollectionNode appendLineEntityFilters(FilterCollectionNode _lineFilter, ExportToExcelFilterTreeBuilder _lineFilterBuilder)

    {

        return _lineFilter;

    }


    /// <summary>

    /// Validates whether a journal is valid for use with the <c>LedgerDailyJournalExcelTemplate</c>. The state of the journal is

    /// checked to verify inserts and updates are allowed.

    /// </summary>

    /// <param name = "_journalId">The name of the journal to validate.</param>

    /// <returns>True if the journal type is valid for use with the template; otherwise false.</returns>

    /// <remarks>Validation results are cached to improve import performance and eliminate redundant checks.</remarks>

    public static boolean validateDailyJournal(JournalId _journalId)

    {

        int64 cacheValue = AVALedgerDailyJournalExcelTemplate::getCacheValue(LedgerDailyJournalCacheScope, curExt(), _journalId);

        boolean ret;


        switch (cacheValue)

        {

            case AVALedgerDailyJournalExcelTemplate::JournalValidationFailed:

                break;


            case AVALedgerDailyJournalExcelTemplate::JournalNotValidated:

                LedgerJournalTable journalTable = LedgerJournalTable::find(_journalId);


                if (journalTable.JournalType != LedgerJournalType::Daily)

                {

                    AVALedgerDailyJournalExcelTemplate::insertCacheValue(LedgerDailyJournalCacheScope, curExt(), _journalId, AVALedgerDailyJournalExcelTemplate::JournalValidationFailed);

                    throw error(strFmt("@SYS114718", journalTable.JournalName, LedgerJournalType::Daily));

                }


                // validate the header for the journal line entity record

                LedgerJournalHeaderEntity header;


                select firstonly header

                    where header.JournalBatchNumber == _journalId;


                ret = LedgerJournalEntityBase::validateJournal(header);

    

                // cache the validate results

                AVALedgerDailyJournalExcelTemplate::insertCacheValue(LedgerDailyJournalCacheScope, curExt(), _journalId, journalTable.RecId);

                break;


            default:

                ret = true;

                break;

        }


        return ret;

    }


}

=================================================================

Comments

Popular posts from this blog

Customization on Sales invoice Report in D365 F&O

75) COC - Create a coc of the table modified method

46) D365 FO: SHAREPOINT FILE UPLOAD USING X++