Oct 19, 2023

TABLE FIELD MODIFIED EVENT HANDLER IN D365 FO

Below is an example to trigger a function on field modified event 

1) Go to AssetTable --> Events --> onModifiedField 

2) Right click on the onModifiedField  --> Copy event handler

3) Create a new class and paste the copied eventhandler signature

4

 Class My_AssetTable_EventHandler

    /// <summary>

    /// 

    /// </summary>

    /// <param name="sender"></param>

    /// <param name="e"></param>

    [DataEventHandler(tableStr(AssetTable), DataEventType::ModifiedField)]

    public static void AssetTable_onModifiedField(Common sender, DataEventArgs e)

    {

       

        ModifyFieldEventArgs fieldEvent = e as DataEventArgs;

        AssetTable assetTable = sender as AssetTable;

        FieldId         fieldId = fieldEvent.parmFieldId();

        

        switch(fieldId)

        {

            case(fieldNum(AssetTable, Name)):

                if(assetTable.AdditionalName)

                {                    

                    if(Box::yesNo("Do you want to update Asset additional description with same value",DialogButton::No))

                    {

                        assetTable.AdditionalName = assetTable.Name;

                    }                    

                }

                else

                {

                    assetTable.AdditionalName = assetTable.Name;

                }

            break;

            default:

                break;

        }

    }

}

May 19, 2022

Rename .mdf and .ldf files


Try the below steps to rename the .mdf and .ldf files

1) Run the below command to get the logical file name's and physical file names of the DB.

USE AXDB

GO

SELECT file_id, name as [logical_file_name], physical_name

FROM sys.database_files

 Result : 



2) Disconnect the existing connecting and bring the DB to offline by running below scripts

USE [master];

GO

--Disconnect all existing session.

ALTER DATABASE AXDB SET SINGLE_USER WITH ROLLBACK IMMEDIATE

GO

--Change database in to OFFLINE mode.

ALTER DATABASE AXDB SET OFFLINE

3)  Refresh the DB's and check the status of AXDB should be offline

4)  Go to file locations and change the files names

5) then run the below cmd

ALTER DATABASE AXDB MODIFY FILE (Name='AXDBUAT', FILENAME='G:\MSSQL_DATA\AXDB.mdf')

GO

ALTER DATABASE AXDB MODIFY FILE (Name='AXDBUAT_log', FILENAME='H:\MSSQL_LOGS\AXDB_log.ldf')

GO

6) To bring back the DB online run below cmd

USE [master];

GO

ALTER DATABASE AXDB SET ONLINE

Go

ALTER DATABASE AXDB SET MULTI_USER

Go

7) Run the below cmd to check and confirm the status of the DB 

Select name as [AXDB],state_desc from sys.databases 



8) ReRun the below cmd to see the changed names



Apr 20, 2022

Sample excel export job (X++)

 static void CustomerDetailsExport(Args _args)

{

CustTable CustTable;

SysExcelApplication application;

SysExcelWorkbooks workbooks;

SysExcelWorkbook workbook;

SysExcelWorksheets worksheets;

SysExcelWorksheet worksheet;

SysExcelCells cells;

SysExcelCell         cell;

int                          row;

DimensionAttributeValueSetStorage  dimstorage;


int         i = 0;

str         fileName    = "C:\\Users\\XXX\\Desktop\\CustDetailsWithFinDim.xlsx";

;


application = SysExcelApplication::construct();

workbooks = application.workbooks();

workbook = workbooks.add();

worksheets = workbook.worksheets();

worksheet = worksheets.itemFromNum(1);

cells = worksheet.cells();

cells.range('A:A').numberFormat('@');


cell = cells.item(1,1);

cell.value("Customer");

cell = cells.item(1,2);

cell.value("Name");

cell = cells.item(1,3);

cell.value("Warehouse");

cell = cells.item(1,4);

cell.value("CostCenter");

cell = cells.item(1,5);

cell.value("Department");

cell = cells.item(1,6);

cell.value("BusinessUnit");

cell = cells.item(1,7);

cell.value("SalesType");


row = 1;

while select CustTable

    where CustTable.AccountNum 

{

    row++;

    cell = cells.item(row, 1);

    cell.value(CustTable.AccountNum);

    cell = cells.item(row, 2);

    cell.value(CustTable.name());


    dimstorage = DimensionAttributeValueSetStorage::find(CustTable.DefaultDimension);

    for(i=1;i <= dimstorage.elements();i++)

    {

       if(DimensionAttribute::find(dimstorage.getAttributeByIndex(i)).Name == 'Warehouse')

       {

            cell = cells.item(row, 3);

            cell.value(dimstorage.getDisplayValueByIndex(i));

       }

       if(DimensionAttribute::find(dimstorage.getAttributeByIndex(i)).Name == 'CostCenter')

       {

            cell = cells.item(row, 4);

            cell.value(dimstorage.getDisplayValueByIndex(i));

       }

       if(DimensionAttribute::find(dimstorage.getAttributeByIndex(i)).Name == 'Department')

       {

            cell = cells.item(row, 5);

            cell.value(dimstorage.getDisplayValueByIndex(i));

       }

        if(DimensionAttribute::find(dimstorage.getAttributeByIndex(i)).Name == 'BusinessUnit')

       {

            cell = cells.item(row, 6);

            cell.value(dimstorage.getDisplayValueByIndex(i));

       }

        if(DimensionAttribute::find(dimstorage.getAttributeByIndex(i)).Name == 'SalesType')

       {

            cell = cells.item(row, 7);

            cell.value(dimstorage.getDisplayValueByIndex(i));

       }

    }

}

    workbook.saveAs(fileName);

    application.visible(true);

    info(strFmt('Exported %1 records',row-1));

}