Friday, April 23, 2021

Get display value from ledger dimension in D365FO

 Last time, I have shared the code about how to Get Main Account from Ledger Dimension in D365FO. In this article, I am sharing the one line code to get the display value using LedgerDimensionFacade class. See the following code:

//Get Display value from LedgerDimension
//The argument (LedgerDimensionValue) is LedgerDimension which you can get from table or any other source as per your requirement

info(strFmt("%1",LedgerDimensionFacade::getDisplayValueForLedgerDimension(LedgerDimensionValue)));

Get main account from ledger dimension in D365FO

 In this article, you will get the one line code to get the Main Account from Ledger Dimension using LedgerDimensionFacade class. See the following code:

//Get MainAccountId from LedgerDimension
//The argument (LedgerDimensionValue) is LedgerDimension which you can get from table or any other source as per your requirement
info(strFmt("%1",LedgerDimensionFacade::getMainAccountFromLedgerDimension(LedgerDimensionValue).MainAccountId));

//Get Name of MainAccountId from LedgerDimension
//The argument (LedgerDimensionValue) is LedgerDimension which you can get from table or any other source as per your requirement
info(strFmt("%1",LedgerDimensionFacade::getMainAccountFromLedgerDimension(LedgerDimensionValue).Name));
                            

Assign default values to form in D365FO

 

Scenario:

I am assigning a value to a field in HcmEmploymentLeave form using OnInitValue form data source event handler.

Lets start!

  • Run Visual Studio
  • Search your desired form in Application Explorer
  • Right click the form -> click Open designer
  • Expand the Data Sources node
  • Expand your desired data source node -> Events -> Select OnInitValue event -> Right click and copy event handler method
  • Create a new class
  • Paste the event handler method
  • Add code in it like below:
[FormDataSourceEventHandler(formDataSourceStr(HcmEmploymentLeave, HcmEmploymentLeave), FormDataSourceEventType::InitValue)]
public static void HcmEmploymentLeave_OnInitValue(FormDataSource sender, FormDataSourceEventArgs e)
{
     FormRun formRun = sender.formRun();
     Object hcmEmploymentLeave_ds = formRun.dataSource(formDataSourceStr(HcmEmploymentLeave, HcmEmploymentLeave)) as FormDataSource;
     HcmEmploymentLeave hcmEmploymentLeave = hcmEmploymentLeave_ds.cursor();
     
     //Assigning value to the field
     hcmEmploymentLeave.FIELDNAME= VALUE;
}

Above code is just an example to show you how it works. You can apply these steps to any form.

Monday, March 22, 2021

 

How to: Create Query with Range through X ++


I had a hard time trying to find how use range between dates so I hope to help not only who wants to create a query but also who wants to know how to use range with date.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// Create a new Job
static void CustDateRangeQuery(Args _args)
{
    Query                   query;
    QueryRun                queryRun;
    QueryBuildDataSource    qbds;
    QueryBuildRange         qbr;
    CustTable               custTable;
    ;
 
    // Instance the class Query
    query =  new Query();
 
    // Add DataSource to Query
    qbds = query.addDataSource( tableNum(CustTable));
 
    // Add a range
    qbr = qbds.addRange( fieldNum(CustTable,CreatedDateTime));
 
    // Set range value
    qbr.value(SysQuery::range( "01/01/2012","30/12/2012" ));
 
    // Run Query
    queryRun = new QueryRun(query);
 
    // Retrieves the next record from the query.
    while(queryRun.next())
    {
        // Get Result
        custTable = queryRun.get( tableNum(CustTable));
 
        // Show AccountNum
        info(custTable.AccountNum);
    }
}