Search This Blog

Tuesday, August 23, 2022

Create display method on the existing table using Extension in Dynamics 365 Finance and Operation

Display method used to create when we need to do some calculation on numeric or some string operation and then we need to display it on the form. Try to avoid creating display method as it causes performance overhead. 

Example :

suppose we wanted to display new field on the customer group form which will show the customer group id with the desciption. To achieve the same, we need to create the display method. We can create by creating extension of the form or of the table. 

I have created code extension of the CustGroup table and created the below code.

/// <summary>

/// Extension of the CustGroup table has been created to add some display method

/// </summary>

[ExtensionOf(tableStr(CustGroup))]

final class CustGroup_FOTraining_Extension

{

    /// <summary>

    /// Display method

    /// </summary>

    /// <returns>Customer group with name</returns>

    [SysClientCacheDataMethodAttribute(true)]

    public display TRNGroupWithName displayGroupWithName()

    {

        //your logic goes here

        return this.CustGroup + this.Name;

    }

 

}


And now we will create the extension of the CustGroup form to add the new field which will display the above values

so we can create the new string type field with the below properties. 






and the output will be as per below 


















How to create label or label file in Dynamics 365 Finance and Operation

In the Dynamics 365 Finance and Operation, for each hard code string, we need to create the label for that like Label on the Extended data type, Base enums, Menu items, Caption on the forms or any other string used to create the info, warning or error in the code. There are more scenario, where we need to create label instead of hard code. 

Label support language translation as well so that when user change the language so that user can see all the labels on the front end in respective language. 

To create the label, first we need to create the label file. Every model should a label file. 

How to create label file

To create label file, Just right click on your project and click add new time and select the label file as per below screenshot. 

Click next 


Click next 



By default en-us will be selected. you can select the multiple language based on your requirement and click on next. 

it will show you the summary



click next and label file has been created.

when you double click the created label file, it will show you the below grid. 
here you can create the new label id and copy it and paste in your code or on the object properties. for example, below label id will be 

@FOTraining:NewLabel0






Every time while creating the label, we should search the existing label and we can use it. 


Monday, August 15, 2022

Read excel file using x++ in Dynamics 365 F&O

 How to read excel file using x++ in Dynamics 365 Finance and operation

Below is the Runnable class we can use to read the excel file. 

internal final class TestReadExcelFile

{

    FileUploadTemporaryStorageResult        fileUploadResult;

    int                                     totalRows;

 

    /// <summary>

    /// Parm method for the file upload storage

    /// </summary>

    /// <param name = "_fileUplaodResult">File upload storage</param>

    /// <returns>File upload storage</returns>

    public FileUploadTemporaryStorageResult parmFileUploadResult(FileUploadTemporaryStorageResult   _fileUplaodResult = fileUploadResult)

    {

        fileUploadResult = _fileUplaodResult;

 

        return fileUploadResult;

    }

 

    /// <summary>

    /// Class entry point. The system will call this method when a designated menu

    /// is selected or when execution starts and this class is set as the startup class.

    /// </summary>

    /// <param name = "_args">The specified arguments.</param>

    public static void main(Args _args)

    {

        TestReadExcelFile       readExcelFile = new TestReadExcelFile();

        try

        {

            FileUploadTemporaryStorageResult    tempStorageResult = File::GetFileFromUser() as FileUploadTemporaryStorageResult;

 

            if (tempStorageResult && tempStorageResult.getUploadStatus())

            {

                readExcelFile.parmFileUploadResult(tempStorageResult);

                readExcelFile.readExcelFile();

            }

        }

        catch

        {

            Error("Error");

        }

    }

 

    /// <summary>

    /// Get the excel ranges to read the excel

    /// </summary>

    /// <returns>Excel range</returns>

    private OfficeOpenXml.ExcelRange getRange()

    {

        int                             firstRow, lastRow;

        System.IO.Stream                stream;

        OfficeOpenXml.ExcelPackage      excelPackage;

        OfficeOpenXml.ExcelRange        range;

        OfficeOpenXml.ExcelWorksheet    excelWorksheet;

 

 

        stream = fileUploadResult.openResult();

 

        excelPackage = new OfficeOpenXml.ExcelPackage(stream);

        excelPackage.Load(stream);

        //get the first worksheet. here you can specify the sheet name as well

        excelWorksheet = excelPackage.get_Workbook().get_Worksheets().get_Item(1);

        firstRow  = excelWorksheet.Dimension.Start.Row;

        lastRow   = excelWorksheet.Dimension.End.Row;

        totalRows = lastRow - firstRow + 1;

        range     = excelWorksheet.Cells;

        return range;

    }

 

    /// <summary>

    /// Read excel file

    /// </summary>

    public void readExcelFile()

    {

        int                     recordsUpdated;

        CustGroup               custGroup;

        OfficeOpenXml.ExcelRange range = this.getRange();

 

        //First row is header so we we reading the values from second row.

        for (int i = 2; i <= totalRows; i++)

        {

           

            CustGroupId                custGroupLocal           = range.get_Item(i,1).Value;

            Description                custGroupName            = range.get_Item(i,2).Value;

 

            custGroup.clear();

            custGroup.CUstGroup = custGroupLocal;

            custGroup.Name      = custGroupName;

            custGroup.insert();           

        }

        Info(strFmt("Record %1 created", totalRows-1));

    }

 

}


Friday, August 5, 2022

How to connect Postman with Dynamics 365 Finance and Operation

Connect postman with Dynamics 365 finance and operation


Azure application registration

First we need to register application on the azure portal Go to portal.azure.com and click on the respective subscription. in the Search bar type application registration and click on new application registration




















Specify the below detail as per your requirement 












































Please make a copy of the client id and Tenant id because we will use it further. Click on the Add a certificate or secret 

















Please make a copy of the client secret id and secret value. you will not be able to see the client secret value after closing the dialog. 

Add user in F&O

In the finance and Operation , Navigate to the System administration --> Setup --> Azure active directory application and create the record as per below. Client id should be used over here. 




















Setting in Postman 

First we need to get the token and after we will be using this token to do operation with F&O.

To do the setup in the postman, we need the below values 

   
Key Value
client_id     Application id copied from Azure portal
grant_type     client_credentials
Scope     F&OHomepageURL/.default
client_secret     secret id copied from azure portal
Resource         F&O Home page URL

As per below 






















You will receive the access_token by running above script and you can copy that token or can use environment variable in the postman to store the access_token
as per below 




























TTo test that everything is working fine or not. you can use the token generated above and can get the few customer record as per below



Monday, August 1, 2022

Create lookup method using x++ in Dynamics 365 FO

 void lookup(FormStringControl _control)

    {

        SysTableLookup  tableLookup = SysTableLookup::newParameters(tableNum(EntAssetObjectTable), _control);


        Query                   query           = new Query();

        QueryBuildDataSource    qbdsAssetTable  = query.addDataSource(tableNum(EntAssetObjectTable));


        tableLookup.parmQuery(query);

        tableLookup.addLookupfield(fieldNum(EntAssetObjectTable, SerialID));

        tableLookup.addLookupfield(fieldNum(EntAssetObjectTable, ObjectID));

        tableLookup.addLookupfield(fieldNum(EntAssetObjectTable, Name));        

    

        tableLookup.performFormLookup();

    }