Thursday, March 21, 2024

How to add custom Open in excel using custom entity in D365F&O X++

 Add a custom template to export Expense journals to Excel. All operations(create,update,delete)  needs to be done using opening excel and viceversa.

Instead of  creating a new entity, I have duplicated the entity which was previously used for export and made changes. Now this is my custom entity.



Note :The fields which are in Auto report, only those fields will be available in excel.

Create a class :

Take the extension of form where custom template needs to be available.

using Microsoft.Dynamics.Platform.Integration.Office;

using Microsoft.Dynamics.Platform.Integration.Office.FilterBinaryNode;

[ExtensionOf(formStr(LedgerJournalTransCost))]

internal final class DaxLedgerJournalTransCost_Extension

{

   [FormEventHandler(formStr(LedgerJournalTransCost), FormEventType::Initializing)]

   public static void ComponentSalesMasterListPage_OnInitializing(xFormRun sender, FormEventArgs e)

   {

       const str CustomExportTHSSalesMasterToExcelOptionId = "Dax Custom entity export";

       FormRun formRun = sender as FormRun;

       if (formRun)

       {

           OfficeFormRunHelper officeHelper = formRun.officeHelper();

           if (officeHelper)

           {

               officeHelper.OfficeMenuInitializing += eventhandler(DaxLedgerJournalTransCost_Extension::officeMenuInitializingHandler);

           }

       }

   }

   private static void officeMenuInitializingHandler(FormRun formRun, OfficeMenuEventArgs eventArgs)

   {

       const str CustomExportLinesToExcelOptionId = "Dax Custom entity export";

       OfficeMenuOptions menuOptions = eventArgs.menuOptions();

       // Find the entity options if they were included by default

       OfficeMenuDataEntityOptions entityOptions = menuOptions.getOptionsForEntity(tableStr(DaxExpenseJournalLineEntity));

       if (!entityOptions)

       {

           // The entity options were not included. Add them.

           entityOptions = OfficeMenuDataEntityOptions::construct(tableStr(DaxExpenseJournalLineEntity));

           menuOptions.dataEntityOptions().addEnd(entityOptions);

       }

       OfficeGeneratedExportMenuItem salesMasterMenuItem = OfficeGeneratedExportMenuItem::construct(tableStr(DaxExpenseJournalLineEntity),CustomExportLinesToExcelOptionId);

       salesMasterMenuItem.displayName('Dax Custom entity export');

       salesMasterMenuItem.getDataEntityContext +=  eventhandler(DaxLedgerJournalTransCost_Extension::getDataEntityContextHandler);

       menuOptions.customMenuItems().addEnd(salesMasterMenuItem);

       entityOptions = OfficeMenuDataEntityOptions::construct(tableStr(DaxExpenseJournalLineEntity));

       eventArgs.menuOptions().dataEntityOptions().addEnd(entityOptions);

   }

   private static void getDataEntityContextHandler(OfficeGeneratedExportMenuItem _menuItem, FormRun formRun, ExportToExcelDataEntityContext dataEntityContext)

   {

       ExportToExcelDataEntityContext context = dataEntityContext;

       const str CustomExportLinesToExcelOptionId = 'Dax Custom entity export';

       LedgerJournalTrans ledgerJournalTrans = formRun.dataSource(formDataSourceStr(LedgerJournalTransCost, LedgerJournalTrans)).cursor();

       if (_menuItem.id() == CustomExportLinesToExcelOptionId )

       {

           context.addEntityDefault(tableStr(DaxExpenseJournalLineEntity));

           ListEnumerator enumerator = context.entities().getEnumerator();

           while (enumerator.moveNext())

           {

               ExportToExcelDataEntityInfo entity = enumerator.current();

               if (entity.entityName() == tableStr(DaxExpenseJournalLineEntity))

               {

                   ExportToExcelFilterTreeBuilder filterBuilder = new ExportToExcelFilterTreeBuilder(tableStr(DaxExpenseJournalLineEntity));

                   FilterNode filterExpression = filterBuilder.areEqual(fieldStr(DaxExpenseJournalLineEntity, JournalBatchNumber), ledgerJournalTrans.JournalNum);

                   entity.filter(filterExpression);

 

                   //FilterNode filterExpression1 = filterBuilder.areEqual(fieldStr(DaxExpenseJournalLineEntity, DataareaId), curExt() );                   // entity.filter(filterExpression);

                   //FilterCollectionNode collectionNode = new FilterCollectionNode();

                   //collectionNode.Operator =  Microsoft.Dynamics.Platform.Integration.Office.FilterOperators::And;

                   //var collection = collectionNode.Collection;

                   //if (ledgerJournalTrans.JournalNum)

                   //{

                   //    collection.Add(filterExpression);

                   //}

                   //if (ledgerJournalTrans.DataAreaId == 'USMF')

                   //{

                   //    collection.Add(filterExpression1);

                   //}

                   //entity.filter(collectionNode);

               }

           }

       }

   }

}

..................

After build, our template is added to form.


Click on our custom template then lines will be exported.


Create a new record in Excel and click on publish, then it is going to be updated in Form.












No comments:

Post a Comment