Tag Archives: D365

D365 FO : Developing Business Events with X++ from scratch

Microsoft Dynamics AX/D365 F&O Technical blog

Business events provide a mechanism that allow external system receives notifications from Microsoft Dynamics 365 Finance and operations. For example – When you are posting a purchase invoice then you can send notifications/ payload / message to an external system.

You can use existing standard business events which are available in the D365 FO or you can also develop and customize new business events as per your need.

There are 2 ways you can consume business events . 1. Power automate (Microsoft Flow) 2. Azure messaging services.

Scenario – In this example, I am going to show you how we can develop a new custom business event from scratch. In my scenario, business event in D365 FO should be triggered when a user is posting purchase invoice in Microsoft dynamics 365 for finance and operations.

There a 2 standard and base classes which i will use to develop custom business events –

  1. BusinessEventsContract ( Use for developing/defining payload message)
  2. BusinessEventsBase ( Use for triggering custom business event)

Create a PAAPurchInvoiceJournalPostBusinessEventContract class

[DataContract]
class PAAPurchInvoiceJournalPostBusinessEventContract extends BusinessEventsContract
{
private VendAccount vendAccount;
private PurchId purchid;
private InvoiceId invoiceId;

private void initialize(VendInvoiceJour _vendInvoiceJour)
{
vendAccount = _vendInvoiceJour.OrderAccount;
purchid = _vendInvoiceJour.PurchId;
invoiceId = _vendInvoiceJour.InvoiceId;
}

public static PAAPurchInvoiceJournalPostBusinessEventContract newFromVendInvoiceJour(VendInvoiceJour _vendInvoiceJour)
{
PAAPurchInvoiceJournalPostBusinessEventContract contract = new PAAPurchInvoiceJournalPostBusinessEventContract();
contract.initialize(_vendInvoiceJour);
return contract;
}

private void new()
{
}

[DataMember(‘VendAccount’), BusinessEventsDataMember(‘VendAccount’)]
public vendAccount parmvendAccount(vendAccount _vendAccount = vendAccount)
{
vendAccount = _vendAccount;
return vendAccount;
}

[DataMember(‘PurchId’), BusinessEventsDataMember(“PurchId”)]
public PurchId parmPurchId(PurchId _purchId = purchId)
{
purchId = _purchId;
return purchId;
}

[DataMember(‘InvoiceId’), BusinessEventsDataMember(“InvoiceId”)]
public InvoiceId parmInvoiceId(InvoiceId _invoiceId = invoiceId)
{
invoiceId = _invoiceId;
return invoiceId;
}

}

Create a PAAPurchInvoiceJournalPostBusinessEvent class

[BusinessEvents(classStr(PAAPurchInvoiceJournalPostBusinessEventContract),
“Custom Vendor Invoice Post Business Event”,
“This business event is triggering during the time purchase invoice posting”,ModuleAxapta::AccountsPayable)]
class PAAPurchInvoiceJournalPostBusinessEvent extends BusinessEventsBase
{
private VendInvoiceJour vendInvoiceJour;

static public PAAPurchInvoiceJournalPostBusinessEvent newFromVendInvoiceJour(VendInvoiceJour _vendInvoiceJour)
{
PAAPurchInvoiceJournalPostBusinessEvent businessEvent = new PAAPurchInvoiceJournalPostBusinessEvent();

businessEvent.parmVendInvoiceJour(_vendInvoiceJour);
return businessEvent;
}

private VendInvoiceJour parmVendInvoiceJour(VendInvoiceJour _vendInvoiceJour = vendInvoiceJour)
{
vendInvoiceJour = _vendInvoiceJour;
return vendInvoiceJour;
}

private void new()
{
}

[Wrappable(true), Replaceable(true)]
public BusinessEventsContract buildContract()
{
return PAAPurchInvoiceJournalPostBusinessEventContract::newFromVendInvoiceJour(vendInvoiceJour);
}

}

you must add below block of code in purchase invoice posting routine class. Also business must be activated or enabled in that company. Business event will trigger when purchase invoice posting will take place.

VendInvoiceJour vendInvoiceJour = this; // vendInvoiceJour buffer

if(BusinessEventsConfigurationReader::isBusinessEventEnabled(classStr(PAAPurchInvoiceJournalPostBusinessEvent)))
{
PAAPurchInvoiceJournalPostBusinessEvent::newFromVendInvoiceJour(this).send () ;

}

Build your project solution and navigate to path -[System administration–> Setup –> Business Events –> Business Events Catalog] to see and activate your business event.

Archive inventory transactions (Purge InventTrans) in Microsoft Dynamics 365 for finance and operations

As we know, “InventTrans” table is one of the largest table in Microsoft D365 for finance and operations and this table keep growing through out the ERP lifecycle and consume more space. BUT ! here is the good news that Microsoft has released a standard feature and a standard batch job to archive inventory transactions and purge InventTrans table data.

Here is the Microsoft document link – https://docs.microsoft.com/en-us/dynamics365/supply-chain/inventory/archive-inventory-transactions

cheers,

Piyush Adhikari

D365 FO – Create deep links, shareable, secured URLs in Microsoft Dynamics 365 for finance & operations

A complete guide to mobile app deep linking | Adjust

What is deep links in Microsoft Dynamics 365 for finance and operation? Deep links are shareable and secures URLs to a specific form. Optional filters can be passed so that when user will open the deep link and navigate to a specific form then it will show filtered data ore records. In Microsoft D365 FO, below library – Microsoft.Dynamics.AX.Framework.Utilities.UrlHelper.UrlGenerator is used to generate deep links using X++ .

Sample Code – Below is the sample code of developing & generating deep links for D365 FO forms and records using filter and ranges in form data source. In below example, i am generating the sharable URL (deep link ) of a form – CustAccount and applying a “CustAccount” filter on custTable datasource.

using Microsoft.Dynamics.AX.Framework.Utilities; // setting library/Dll reference

class DeepLinkTest
{
    public static void main(Args _args)
    {
        MenuItemNameDisplay menuItemName                = menuItemDisplayStr(CustTable);  // customer form
        MenuItemType        menuItemtype                = MenuItemType::Display;
        str                 filterformDataSource        = formDataSourceStr(CustTable, CustTable);  // filer datasource
        str                 filterFormDataSourceField   = fieldStr(CustTable, AccountNum); // filter field

        UrlHelper.UrlGenerator generator = new UrlHelper.UrlGenerator();
        System.Uri currentHost = new System.Uri(UrlUtility::getUrl());
 
        generator.HostUrl = currentHost.GetLeftPart(System.UriPartial::Authority);
        generator.Company = curExt();
        generator.MenuItemName = menuItemName;
        generator.MenuItemType = menuItemtype;
        generator.Partition = getCurrentPartition();
        generator.EncryptRequestQuery = true; 
        if(filterformDataSource)
        {
            UrlHelper.RequestQueryParameterCollection requestQueryParameterCollection;
 
            requestQueryParameterCollection = generator.RequestQueryParameterCollection;
            requestQueryParameterCollection.UpdateOrAddEntry(filterformDataSource, filterFormDataSourceField, "CustAccount"); // applying filter
        } 

        System.Uri fullURI = generator.GenerateFullUrl();
 
        Info(fullURI.AbsoluteUri); // getting deep link
    }

}

Date functions snippet in D365 Finance & Operations & Microsoft Dynamics AX

  • Get maximum date : TransDate maximumDate = dateMax();
  • Get minimum date: TransDate minimumDate= datenull();
  • Get Current or today’s Date: TransDate todayDate = today();
  • Convert string to date:  TransDate transdate = str2Date(’26-06-2020′, 123);(day = 1, month = 2, year = 3)
  • Convert date to string: str dateStr = date2Str(today(), 123, DateDay::Digits2, DateSeparator::Hyphen, DateMonth::Digits2, DateSeparator::Hyphen, DateYear::Digits4, 0);
  • Convert String to datetime: utcdatetime dateTime = str2Datetime(‘26.06.2020 11:00:00 pm’, 123);
  • Get current system date time : utcdatetime currentDateTime = DateTimeUtil::getSystemDateTime();
  • Get current system date : Transdate currentDate = DateTimeUtil::getSystemDate();
  • getCompanyTimeZone = DateTimeUtil::getCompanyTimeZone();
  • getUserPreferredTimeZone = DateTimeUtil::getUserPreferredTimeZone();
  • getClientMachineTimeZone = DateTimeUtil::getClientMachineTimeZone();// get clie
  • getOriginatingTimeZone = DateTimeUtil::getOriginatingTimeZone(transDateTime); // get originating time zone
  • dateTime = DateTimeUtil::minvalue(); // get minimum value of date time
  • dateTime = DateTimeUtil::maxvalue(); // get maximum value of date time

D365 FO/AX7: Sharepoint File Upload using X++

Requirement :

Uploading a file in Sharepoint online using X++ and inside from Microsoft Dynamics 365 finance and operations.

Prerequisites:

The current user id of Microsoft Dynamics 365 Finance and operations should have proper rights and privileged in Sharepoint Online.

Sample Code:

In this scenario i am creating a text file in Microsoft Dynamics 365 finance and operations and then uploading its memory Stream in Sharepoint online using X++ . You can modify the X++ code in D365 finance and operations as per your need.

class RunnableClassRun
{
/// /// Runs the class with the specified arguments. ///
/// The specified arguments.
public static void main(Args _args)
{
System.UriBuilder builder = new System.UriBuilder(“https://XXXXXX.sharepoint.com”);
str extId = xUserInfo::getExternalId();
Microsoft.Dynamics.AX.Framework.FileManagement.SharePointDocumentStorageProvider provider;
Microsoft.Dynamics.AX.Framework.FileManagement.DocumentLocation documentLocation = new Microsoft.Dynamics.AX.Framework.FileManagement.DocumentLocation();
provider = new Microsoft.Dynamics.AX.Framework.FileManagement.SharePointDocumentStorageProvider(“XXXXXX.sharepoint.com”, “sites/dummy”, “Shared%20Documents/MyFolder”, extId);
System.Byte[] reportBytes = new System.Byte[0]();
System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
reportBytes = enc.GetBytes(“YOUR STRING/TEXT”);
System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(reportBytes);
//memoryStream = comma.getStream();
documentLocation = provider.SaveFile(newGuid(), ‘TextFile.txt’, System.Web.MimeMapping::GetMimeMapping(‘TextFile.txt’), memoryStream);
}

}

}

D365 FO/AX7/Class Extension: COC (Chain of command) & Method Wrapping With Example

  • Introduction COC (chain of command) & Method Wrapping In Microsoft Dynamics 365 finance and operations –
  • Microsoft D365 Finance and operations supports very powerful feature called COC or method wrapping.
  • In past, Microsoft Dynamics AX/Axapta supports overlayering where you can overlayer Microsoft classes, table methods etc directly by placing your code in the middle, bottom or top if standard code. Below is screenshot and example for your reference. In below screenshot i placed my custom code in the middle of standard method by using overlayering.
  • In Microsoft Dynamics 365 finance and operations (After version 8.0) , you cannot overlayer code as everything is sealed now. We have to use COC or method wrapping.Microsoft has improved the Functionality of Class extension for D365 FO by adding wrap logic around methods which are defined in the base class that you are augmenting. You can Wrap or do Chain of command (COC) of public and protected methods.
  • In below screenshot i am using method wrapping or class extension of standard class which is different from Microsoft Dynamics AX overlayering approach.

  • Next keyword means to call the orginal method “ChecItemPostingDate” of standard class “InventUpdate”
  • You can get values of Global Variables of in your extension class of your parent class or main class.
  • Final Modifier means The method can’t be overridden in any class that derives from its class.
  • Here are one more example where we are using COC (chain of commands) in Table methods.

Benefits Of Using Change Of Commands instead of Old Overlayering approach in Microsoft Dynamics 365 finance and operations

  1. Easy Future upgrades and version enhancements as you are not doing overlayering in standard classes or methods or objects & keeping you separate extension classes.
  2. Neat & Clean development as no need to compile whole code librarary.

D365 FO/AX7: Merge Branches Or Moving Changes from One Branch To Another in VSTS/Azure DevOps

Requirement: In last post i demonstrated how can we create A New Production Branch In VSTS (Visual Studio Team Services)/Azure DevOps & Microsoft Dynamics 365 finance and operations. To See my last post please click here. In this post i will show how can we Merge Branches in VSTS/Azure DevOps and Microsoft Dynamics D365 finance and operations or how can we move change or CHANGESets from one dev branch to another prod branch. Before starting this tutorial i will recommend you to please go through my last post.To See my last post please click here

  • Right Click on SOURCE Branch & choose option MERGE
  • Browse TARGET Branch
  • You can choose 2 option for branch changes you would like to merge.First one is “ALL CHANGES UPTO SPECIFIC VERSION” & second one is “SELCTED CHANGESETS
  • Click NEXT and FINISH

D365 FO/AX7: Multithreading With Example Using Pessimistic LOCK & BatchHeader/BatchTask Class

Background – In Microsoft Dynamics D365 Finance and Operations, most of the processes & batch jobs run in a single thread and supports only single thread execution not multi-threading. Generally Single Thread batch Jobs or processes work well in Microsoft Dynamics 365 Fins and ops. But there are many scenarios where we require fast performance and quick competition of processes and batch jobs. Multithreading Batch Jobs approach is very useful in those scenarios.

Two Different Ways In Microsoft Dynamics D365 Finance and Operations,We can develop Multithreaded Batch Jobs in two ways. The first approach is using PESSIMISTIC LOCKS OR PESSIMISTIC SELECT in Microsoft Dynamics D365 Finance and Operations.

The second is to create BATCH TASKs. Batch Task approach you can find in standard classes like RetailStatementCalculate_Multi, RetailStatementPost_Multi Etc. which are using RunBaseBatch Framework (Not SYSOperation Framework) inside Microsoft Dynamics D365 Finance and Operations.

Lets begin with first approach i.e. using PESSIMISTIC LOCKS OR PESSIMISTIC SELECT in Microsoft Dynamics D365 Finance and Operations.By default, D365 fins and ops supports Optimistic Concurrency model (OCC) during the time SELECT or fetching records from database of Microsoft Dynamics D365 Finance and Operations. Optimistic Concurrency Model or SELECT only locks records during the time of Update whereas Pessimistic Concurrency Control (PCC).

Below code you can utilize in batch jobs and you run multiple thread of same batch job class as we a

Lets move to second approach , which include create and adding RUNTIME task using BATCHHEADER class . Here a single Runbase Batch Job is used for creating multiple threads or Runtime Tasks.