Tag Archives: Microsoft Dynamics AX

Microsoft Dynamics AX

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.

D365 FO – Generate next Number sequence in X++ using NumberSeq Class

Requirement – D365 FO – GENERATE NEXT NUMBER SEQUENCE IN X++ USING NUMBERSEQ CLASS

Continue reading D365 FO – Generate next Number sequence in X++ using NumberSeq Class

Entity store In Azure Data Lake – D365 FINANCE & OPERATIONS

What is Entity Store ?

In Microsoft Dynamics 365 finance and operations, Entity store is database (AxDW) which you can use in D365 FO POWER BI dashboards for realtime analytical reporting . Click here to read more about entity store.

What is Azure Data Lake ?

Microsoft Azure Data Lake is cloud based storage and analytics service . Azure Data lake allows data scientist and data engineer to store any kind of large data and performing analytics, machine learning & AI operations on it. Click here to read more about entity store.

Quick Steps Exporting Entity Store in Azure Data Lake

  • Deploy a new storage account in azure portal. Don,t forget to mention Account kind as  StorageV2. Most important, in Advance TAB Enable the Hierarchical namespaces feature else you will not be able to consume Azure Data Lake data in Power BI.
  • When the deployment is completed, go to your recently deployed storage account and navigate to Settings –> Access Keys . Note down the connection string value, it will be used in coming steps
  • Deploy a KEY VAULT resource in portal.azure.com as per your comfort.
  • After deployment of key vault , go to your Azure KEY VAULT resource and navigate to Settings — > Secrets.
  • Click on button + GENERATE/IMPORT & create a SECRET. Fill a name for the secret and not it somewhere as NAME will be used in next steps. In the VALUE Field please Enter the connection string i (connection string you obtained from previous steps inside STORAGE ACCOUNTS)
  • Secret KEY is created
  • Go to Azure portal, select Azure Active Directory, and then select App registrations. You need to register an App
  • After registering you APP, navigate to –>API permission and Configured permissions as mentioned below.
  • In your registered APP, navigate to –> Certificates & secrets. Generate a new client secret and copy it immediately somewhere.
  • Now go back to your KEY VAULT Resource and click on access polices.
  • In the Select principal field, select the name of the application that you previously registered.In the Key permissions field, select Get and List permissions.In the Secret permissions field, select Get and List permissions.
Get and List permissions
  • Now open your D365 finance and operations environment and go the SYSTEM PARAMETERS . Fill the below details
  • Application ID: Enter the application ID of the Azure AD application that you registered earlier.
  • Application Secret: Enter the application key (secret) for the Azure AD application.
  • DNS name: Enter the DNS name of Key Vault.
  • Secret name: Enter the name of the secret that you added to Key Vault together with connection string information.
  • Validate and Test AZURE KEY VAULT as well as AZURE DATA STORAGE
  • Refresh Entity Store
  • Now your entity store is available in Azure Data Lake.

References : entity-store-data-lake

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: Composite Data Entities Vs Data Entities With Example

  • Introduction – Data management framework of Microsoft Dynamics 365 finance and operations contains a very important technical asset called Data Entities. Data entities are conceptual combinations, abstraction and encapsulation of one of more underlying tables. Data entities in Microsoft D365 finance and operations are used to import and export data in Microsoft Dynamics finance and operations. Odata integration can also be achieved using D365 finance and operations data entities. We can customize standard data entity . Click Here to know about standard data entity customization.
  • Composite Data entity vs Simple Data Entity- There are some differences between Composite Data entity & Simple Data Entity. 1 ) First of all simple data entity contains one or more tables where as composite data entity contains multiple data entities 2) Simple Data entities support Odata & can be used in Odata integration whereas Composite data entities cannot be used Odata integration.
  • Where should we use Composite Data entity ?-Composite entity is used in scenarios where an entity can be represented as a single document, like Purchase Header/line , Sales header/line, Invoice header/line etc.
  • Example & Development of Composite Data entity steps are mentioned below :
  • In example i am taking 2 separate standard data entities i.e. PurchPurchaseOrderHeaderV2Entity & PurchPurchaseOrderLineV2Entity
  • PurchPurchaseOrderHeaderV2Entity contains purchase order header information & fields mainly purchTable Fields
  • PurchPurchaseOrderLineV2Entity contains purchase order Lines information & fields mainly PurchLine Fields
  • We will develop a new composite data entity by using above 2 data entities and compose data entity will represent a single purchase order document (Purchase Order Header + Purchase Order Line)
  • Now create a composite data entity in your project.
  • Extend PurchPurchaseOrderLineV2Entity and create relation with PurchPurchaseOrderHeaderV2Entity. (NOTE In my case standard relation is already there so no need to create any customize relation)
  • Right click on your composite data entity and add “New Root Data Entity Reference” and make sure property Data Entity should be equal to PurchPurchaseOrderHeaderV2Entity
  • Right click on your “New Root Data Entity Reference” PurchPurchaseOrderHeaderV2Entity and add “New Embedded Data Entity Reference” equals to PurchPurchaseOrderLineV2Entity & Property Relation should be equal to PurchaseOrderHeader.
  • Extend staging table (PurchPurchaseOrderLineV2Staging) of data entity PurchPurchaseOrderLineV2Entity and add relation with the staging table (PurchPurchaseOrderHeaderV2Staging) of data entity PurchPurchaseOrderHeaderV2Entity.(NOTE : In my case standard relation already exist so i am not creating any relation)
  • Add two columns, RowId and ParentRowId (type int), on all the staging tables i.e. PurchPurchaseOrderHeaderV2Staging & PurchPurchaseOrderLineV2Staging.
  • Create a cluster index on the staging tables which includes RowId, ParentRowid,DefinitionGroup, and ExecutionId for getting good performance .
  • Build and synchronize your project
  • Under “Data import/export framework parameters”, select fast tab “Entity Settings” and click on “Refresh Entity List”. Here is LINK AND TUTORIAL.

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: Create A New Prod Branch In VSTS/Azure DevOps

Introduction : In Microsoft Dynamics 365 & VSTS/Azue DevOps practice, it is always recommended to use a separate branch for production deployment else incomplete code of developer will get included in production deployment package.Developers checkedIn code or any development changes frequently in one dev branch but package should not be created from that branch in which developer is doing checkIn. Only required changes should be merged from dev branch to prod branch in Microsoft Dynamics 365 finance and operation & VSTS/Azue DevOps. Please click here to read my post – D365 FO/AX7: Merge Branches Or Moving Changes from One Branch To Another in VSTS/Azure DevOps

Requirement : Create A New Production Branch In VSTS (Visual Studio Team Services)/Azure DevOps & Microsoft Dynamics 365 finance and operations.

Once Branch of production will be created you can merge code from DEV branch to PROD branch as per requirement.In next tutorial i will show you how can we merge code from one branch to another.

  • Login to your Microsoft Dynamics 365 finance and operations development server.
  • Open visual studio in Microsoft Dynamics 365 finance and operations development server using run as administrator.
  • Click on VIEW tab and open TEAM EXPLORER in Visual studio of D365 fo development server.
  • Open SOURCE CONTROL EXPLORER
  • Create a NEW BRANCH.