Thursday, April 30, 2026

Update Values During Data Entity Import in D365FO

Update Values During Data Entity Import in D365FO

Updating Field Values During Data Entity Import in Dynamics 365 Finance & Operations

In Dynamics 365 Finance & Operations (D365FO), there are scenarios where data needs to be enriched or recalculated during data import. This can be achieved by overriding the mapEntityToDataSource() method in the Data Entity.

This blog explains a real-world example where the Vendor Name is populated automatically while importing vendor invoice data through a data entity.

Business Requirement

  • Vendor invoice data is received from an external system
  • Only the Vendor Tax ID is provided in the import file
  • Vendor Name should be derived automatically during the import

Technical Approach

We override the mapEntityToDataSource() method and update the target datasource buffer before the record is written to the database.

Key Point: The mapEntityToDataSource method is executed during import and is ideal for deriving or modifying values dynamically.

X++ Code Implementation

public void mapEntityToDataSource(
    DataEntityRuntimeContext _entityCtx,
    DataEntityDataSourceRuntimeContext _dataSourceCtx)
{
    super(_entityCtx, _dataSourceCtx);

    if (_dataSourceCtx.name() ==
        dataEntityDataSourceStr(TESTEdicomIncomingDocument, TESTVendInvoiceProcess))
    {
        TESTVendInvoiceProcess  TESTVendInvoiceProcess = _dataSourceCtx.getBuffer();
        LTMVendTable           ltmVendTable;
        VendTable              vendTable;
        DirPartyTable          dirParty;

        select firstOnly ltmVendTable
            where ltmVendTable.CountryDocNum == this.TaxId
            join firstOnly vendTable
                where vendTable.AccountNum == ltmVendTable.AccountNum;

        select firstOnly dirParty
            where dirParty.RecId == vendTable.Party;

        TESTVendInvoiceProcess.VendorName = dirParty.Name;
    }
}

When to Use mapEntityToDataSource()

  • Derive values during import
  • Apply default values
  • Validate or adjust incoming data
  • Populate fields not provided by the source system

Conclusion

The mapEntityToDataSource() method is a powerful extension point in D365FO data entities. Using it correctly helps ensure clean, consistent, and business-ready data during imports.

This pattern is especially useful in integration scenarios where source systems provide minimal data and enrichment is required on the D365FO side.

Monday, May 12, 2025

How to Create Multiselection Lookup in D365FO

How to Create Multiselection Lookup in D365FO

How to Create Multiselection Lookup in D365FO

In this blog post, we will walk through the steps to create a multiselection lookup in Dynamics 365 for Finance and Operations (D365FO). We will use a sample code to demonstrate the process. Also, you can refer to the standard class 'DemoMultiSelectionLookupUIBuilder'.

Step 1: Create the Data Contract Class

First, we need to create a data contract class. This class will hold the list of project IDs that we want to select.


internal final class DemoMultiSelectionLookupContract implements SysOperationValidatable, SysOperationInitializable
{
    private List projIdList;

    [DataMemberAttribute("ProjectId"), 
    AifCollectionTypeAttribute("ProjectId", Types::String),
    SysOperationLabelAttribute(literalStr("@SYS103748")),
    SysOperationDisplayOrderAttribute('7')]
    public List parmProjIdList(List _listProjId = projIdList)
    {
        projIdList = _listProjId;
        return projIdList;
    }
    
    public void initialize()
    {

    }
}
    

Step 2: Create the UI Builder Class

Next, we create a UI builder class to add the dialog field for the project ID selection.


public final class DemoMultiSelectionLookupUIBuilder extends SysOperationAutomaticUIBuilder
{
    protected DialogField addDialogField(IdentifierName _methodName, Object _dataContract = this.dataContractObject())
    {
        DialogField dialogField;

        switch (_methodName)
        {
            case methodStr(DemoMultiSelectionLookupContract, parmProjIdList):
                projIdField = this.dialog().addField(extendedTypeStr(ProjId), "@SYS103748", "Select project id those invoice want to export");
                dialogField = projIdField;
                break;

            default:
                dialogField = super(_methodName, _dataContract);
                break;
        }

        return dialogField;
    }

    public void postRun()
    {
        DemoMultiSelectionLookupContract dataContract = this.dataContractObject() as DemoMultiSelectionLookupContract;
        container selectFieldSources = this.selectField();
        super();

        ctrlProjId = SysLookupMultiSelectCtrl::constructWithQueryRun(
            this.dialog().formRun(),
            projIdField.control(),
            new QueryRun(queryStr(ProjTable)),
            true,
            selectFieldSources);
    }

    public container selectField()
    {
        return [tableNum(ProjTable), fieldNum(ProjTable, ProjId)];
    }

   
}
    

Step 3: Create the Service Class

Finally, we create a service class to handle the business logic for the multiselection lookup.


internal final class DemoMultiSelectionLookupService extends SysOperationServiceBase
{
    System.Exception ex;
    internal void runExport(DemoMultiSelectionLookupContract _exportAttachmentsContract)
    {
        try
        {
            List projectList = _exportAttachmentsContract.parmProjIdList();
        }
        catch (ex)
        {
            this.logException(ex);
        }
    }
}
    

Step 4: Create the Controller Class

Finally, we create a controller class to call the service class.


public class DemoMultiSelectionLookupController extends SysOperationServiceController implements BatchRetryable
{
    public static void main(Args _args)
    {
        DemoMultiSelectionLookupController controller = DemoMultiSelectionLookupController::construct();
        controller.parmDialogCaption("@ApplicationFoundation:ExportAttachments");
        
        controller.startOperation();
    }

    public static DemoMultiSelectionLookupController construct(SysOperationExecutionMode _executionMode = SysOperationExecutionMode::Synchronous)
    {
        return new DemoMultiSelectionLookupController(
            classStr(DemoMultiSelectionLookupService),
            methodStr(DemoMultiSelectionLookupService, runExport),
            _executionMode);
    }

    [Hookable(false)]
    public final boolean isRetryable()
    {
        return false;
    }
}
    

And that's it! You now have a multiselection lookup in D365FO. This example demonstrates how to create a data contract, UI builder, service class, and controller class to achieve this functionality.

Friday, April 11, 2025

How to show default value or first value for the field which is not part of group and returning more than one values

Grouping Data by Certain Fields and Handling Cases Where a Specific Field Returns More Than One Value in D365FO SSRS Reports

Grouping Data by Certain Fields and Handling Cases Where a Specific Field Returns More Than One Value in D365FO SSRS Reports

When working with SQL Server Reporting Services (SSRS) in Dynamics 365 Finance and Operations (D365FO), you might encounter scenarios where you need to group data by certain fields and handle cases where a specific field, not part of the group, returns more than one value. In this blog post, we'll explore how to manage such scenarios by conditionally displaying default values for that field.

Scenario

Consider a report with the following fields:

  • Field 1
  • Field 2
  • Field 3
  • Field 4

We want to group the data by certain fields: Field 2, Field 3, and Field 4. However, if a specific Field 1 that is not part of the group returns more than one value, in that case we want to display a default value, such as a blank or "Unknown".

Solution

To achieve this, we can use an SSRS expression to check if there are multiple values for the specific field within the group and conditionally display a default value.

Expression for Blank Default Value

=IIF(CountDistinct(Fields!Field1.Value, "YourGroupName") > 1, "", Fields!Field1.Value)

Expression for "Unknown" Default Value

=IIF(CountDistinct(Fields!Field1.Value, "YourGroupName") > 1, "Unknown", Fields!Field1.Value)

Expression for FirstValue

=First(Fields!Field1.Value, "YourGroupName")

Conclusion

By using these approaches, you can effectively manage the display of specific fields in your D365FO SSRS reports when dealing with multiple values within a group, ensuring that your report remains clear and easy to read.

We hope this solution helps you in your D365FO SSRS reporting tasks. If you have any questions or need further assistance, feel free to reach out!

Wednesday, April 9, 2025

How to Add $filter, $top, $select, cross-company=true and $orderby in in OData URL

How to Add $filter, $top, cross-company=true, and $select in OData URL

How to Add $filter, $top, $select, cross-company=true and $orderby in in OData URL

OData URLs in Dynamics 365 Finance and Operations (D365FO) provide a flexible way to query data. In this blog post, we will demonstrate how to use the $filter, $top, cross-company=true,$select and $orderby query options to retrieve specific records based on certain criteria.

Using $filter

The $filter query option allows you to specify criteria to filter the data returned by the OData service also you need to add "and" operator between 2 filter values and "&" between 2 diffrent options ($filter, $orderby). Below is an example:

{{resource}}/data/PurchaseOrderHeadersV2?$filter=PurchaseOrderNumber eq 'PO12345' and dataAreaId eq 'USMF'&Cross-company=true

This URL filters the PurchaseOrderHeadersV2 entity to only include records where PurchaseOrderNumber is 'PO12345' and dataAreaId is 'USMF', and includes data from all companies.

Using $top

The $top query option specifies the maximum number of records to return in the response. It can be combined with other query options like $filter and $orderby. Below is an example:

{{resource}}/data/VendPackingSlipTransBiEntities?$top=2&$filter=OrigPurchid eq 'PO12345' and dataAreaId eq 'USMF'&Cross-company=true

This URL returns the top 2 records from the VendPackingSlipTransBiEntities entity where OrigPurchid is 'PO12345' and dataAreaId is 'USMF', ordered by DeliveryDate in descending order, and includes data from all companies.

Using $select

The $select query option allows you to specify which properties to include in the response. This can help reduce the amount of data returned and improve performance. Below is an example:

{{resource}}/data/VendPackingSlipTransBiEntities?$select=dataAreaId,OrigPurchid,PackingSlipId&$top=2&$filter=OrigPurchid eq 'PO12345' and dataAreaId eq 'USMF'&Cross-company=true

This URL returns only the dataAreaId, OrigPurchid, and PackingSlipId properties from the VendPackingSlipTransBiEntities entity where OrigPurchid is 'PO12345' and dataAreaId is 'USMF', and includes data from all companies.

Using $orderby

The $orderby query option allows you to sort the data returned by the OData service based on specified properties. Below is an example:

{{resource}}/data/VendPackingSlipTransBiEntities?$top=2&$filter=OrigPurchid eq 'PO12345' and dataAreaId eq 'USMF'&$orderby=DeliveryDate desc&Cross-company=true

This URL returns the top 2 records from the VendPackingSlipTransBiEntities entity where OrigPurchid is 'PO12345' and dataAreaId is 'USMF', ordered by DeliveryDate in descending order, and includes data from all companies.

Conclusion

Using OData URLs to query data in D365FO is a powerful and flexible method to retrieve the exact records you need. The $filter, $top, cross-company=true, $select, and $orderby query options allow you to refine your queries and optimize data retrieval. By combining these options, you can efficiently manage and retrieve data to meet your specific requirements.

Thursday, April 3, 2025

Project Hour Journal Creation and Invoice Posting using x++ code

Project Hour Journal Creation and Invoice Posting

Project Hour Journal Creation, posting , Invoice proposal creation and Posting invoice

Step-by-Step Guide

1. Creating the Project Hour Journal

The first step is to create a project hour journal. This involves initializing the journal table and transaction data, setting various properties, and validating the entries before inserting them into the database.


public journalId createJournal()
{
   
    ProjJournalTable jourTable;
    ProjJournalTableData jourTableData;
    ProjJournalTransData jourTransData;
    ProjJournalStatic jourStatic;
    ProjTable projtable;
    Qty qty;
    ProjLinePropertySetup projLinePropertySetup;
    TestSFIntegrationParameters TestSFIntegrationParameters = TestSFIntegrationParameters::find(); // Custom table.
    ProjLineProperty projLineProperty;
    ProjJournalTrans jourTrans;
    Amount totalAmount;
    JournalId journalId;

  
    select firstonly reverse projLinePropertySetup
        join projLineProperty
            where projLineProperty.LinePropertyId == projLinePropertySetup.LinePropertyId
            && projLinePropertySetup.CategoryRelation == TestSFIntegrationParameters.RevenueCategory
            && projLinePropertySetup.ProjCode == TableGroupAll::All;

    if (!projLineProperty.ToBeInvoiced)
    {
        throw error(strFmt("The chargeable line property is missing for the specified hour category '%1'.", TestSFIntegrationParameters.RevenueCategory));
    }

    jourTableData = JournalTableData::newTable(jourTable);
    jourTable.JournalId = jourTableData.nextJournalId();
    jourTable.JournalType = ProjJournalType::Hour;
    jourTable.JournalNameId = TestSFIntegrationParameters.RevenueJournalNameId;
    jourTableData.initFromJournalName(ProjJournalName::find(jourTable.JournalNameId));
    jourTable.Description = "Header description";
    jourStatic = jourTableData.journalStatic();

    if (jourtable.validatewrite())
    {
        jourtable.insert();
    }
    else
    {
        throw error("Validation failed to create journal header");
    }

    // Create journal transactions
    if (jourtable)
    {
        jourTransData = jourStatic.newJournalTransData(jourTrans, jourTableData);
        projtable = Projtable::find("ProjId0001");

        jourTrans.clear();
        jourTransData.initFromJournalTable();
        jourTrans.initValue();
        jourTrans.ProjId = projtable.ProjId;
        jourTrans.initFromProjTable(projtable);
        jourTrans.TransDate = DateTimeUtil::getSystemDate(DateTimeUtil::getUserPreferredTimeZone());
        jourTrans.ProjTransDate = jourTrans.TransDate;
        jourTrans.SalesPrice = 20.0;
        jourTrans.Txt = "Line description";
        jourTrans.Qty = 200;
        jourTrans.CurrencyId = 'CAN';
        jourTrans.CategoryId = TestSFIntegrationParameters.RevenueCategory;
        jourTrans.LinePropertyId = projLinePropertySetup.LinePropertyId;
        jourTrans.TaxItemGroupId = TestSFIntegrationParameters.TaxItemGroupId;

        if (jourTrans.validateWrite())
        {
            jourTransData.create(false);
            qty += jourTrans.Qty;
        }
        else
        {
            throw error("@ENG_Labels:ValidationFailedToCreateJournalLines");
        }

        jourTable.NumOfLines = real2int(jourTrans.LineNum);
        jourTable.ProjQty = qty;
        jourTable.doUpdate();
    }
    return jourTable.JournalId;
}
    

2. Posting the Journal

Once the journal is created, the next step is to post it. This ensures that the journal entries are finalized and recorded in the system.


public void postJournal(JournalId _journalId)
{
    ProjJournalCheckPost jourPost;
    jourPost = ProjJournalCheckPost::newJournalCheckPost(true, true, JournalCheckPostType::Post, tableNum(ProjJournalTable), _journalId);
    jourPost.runOperation();
}
    

3. Generating the Invoice Proposal

After posting the journal, we generate an invoice proposal based on the journal entries. This involves selecting the appropriate funding source and creating the proposal data.


public ProjProposalId invoiceProposalCreation(JournalId _journalId)
{
    ProjJournalTrans projJournalTrans;
    ProjTable projTable, parentprojTable;
    ProjFundingSourceRefId fundingSource;
    ProjFundingSource ProjFundingSource;
    ProjProposalId proposalId;

    select firstonly ParentId, defaultfundingsource, ProjInvoiceProjId from projTable
        join ProjId from projJournalTrans
            where projTable.ProjId == projJournalTrans.ProjId
               && ProjJournalTrans.JournalId == _journalId
               && ProjJournalTrans.LineNum == 1;

    select firstonly ProjInvoiceProjId, ProjGroupId, defaultfundingsource from parentprojTable
        where parentprojTable.ProjId == projTable.ParentId;

    fundingSource = projTable.DefaultFundingSource != 0 ? projTable.DefaultFundingSource : parentprojTable.DefaultFundingSource;

    if (!fundingSource)
    {
        select firstonly projFundingSource
            where projFundingSource.ContractId == projTable.ProjInvoiceProjId
            && projFundingSource.CustAccount == projTable.CustAccount
            && projFundingSource.FundingType == ProjFundingType::Customer;

        if (!projFundingSource)
        {
            throw error("Couldn't find funding source (Invoice account) for given parent and child project and on assigned contract.");
        }
        fundingSource = projFundingSource.RecId;
    }

    proposalId = TestCreateInvoiceProposal::createDataInTmp(fundingSource, _journalId);
    return proposalId;
}
    

4. Posting the Invoice Proposal

Finally, we post the invoice proposal to generate the actual invoice. This involves updating the proposal journal and running the form letter operation.


public InvoiceId postInvoiceProposal(ProjProposalId _proposalId)
{
    ProjProposalJour projProposalJour;
    ProjFormLetter projFormLetter;

    select forupdate projProposalJour order by RecId desc
        where projProposalJour.ProposalId == _proposalId;

    projProposalJour.PSAInvoiceTxtPre = "Invoice header text";
    projProposalJour.LineProperty = ProjLinePropertyCode::Approved;
    projProposalJour.editInvReportFormat(true, "PSAProjInvoice.Report");
    projProposalJour.update();

    projFormLetter = ProjFormLetter::construct(DocumentStatus::ProjectInvoice);
    projFormLetter.createParmLine(projProposalJour);
    projFormLetter.runOperation();

    projProposalJour = projProposalJour::find(projProposalJour.ProposalId);
    return projProposalJour.ProjInvoiceId;
}
    

Additional Class: TestCreateInvoiceProposal

Below is the additional class code for creating invoice proposals:


class TestCreateInvoiceProposal extends projInvoiceProposalInsertLines
{

    public void createInvoiceProposals(PSATmpProjProposalTrans _tmpProjProposalTrans)
    {
        int i;
        ProjProposalJour proposalJour;
        ProjTable projTable;

        this.progressInit("@SYS54552", 0);
        while select _tmpProjProposalTrans
        {
            if (i == 0)
            {
                this.parmDefaultDimension(_tmpProjProposalTrans.DefaultDimension);
                this.setProjProposalJour(_tmpProjProposalTrans.ProjInvoiceProjId, _tmpProjProposalTrans.ProjId,
                _tmpProjProposalTrans.FundingSourceRefId, _tmpProjProposalTrans.CurrencyCode, 
                _tmpProjProposalTrans.TaxInformation_IN, _tmpProjProposalTrans.FixedExchRate);
                i++;
            }

            this.setProjProposalJourPost(_tmpProjProposalTrans);

            select firstonly RecId from projTable
                where projTable.ProjId == _tmpProjProposalTrans.ProjId &&
                        projTable.ProjInvoiceProjId == _tmpProjProposalTrans.ProjInvoiceProjId
                exists join proposalJour
                    where proposalJour.ProjInvoiceProjId == projTable.ProjInvoiceProjId &&
                            proposalJour.ProposalId == projProposalJour.ProposalId;

            if (projTable.RecId)
            {
                this.doEmpl(_tmpProjProposalTrans.ProjInvoiceProjId, _tmpProjProposalTrans.RefRecId);
            }

            if (!this.parmSkipRecalculateProposalTotals())
            {
                this.updateInvoice();

                // When an invoice proposal is generated, the system will
                // automatically create retainage withholding records and/or retainage billing records at the project level.
                this.calcRetention();
            }
        }
    }

    public static ProjProposalId createDataInTmp(ProjFundingSourceRefId _fundingSource, JournalId _journalId)
    {
        ProjInvoiceProposalCreateLinesParams proposalCreateLinesParams = ProjInvoiceProposalCreateLinesParams::construct();
        ProjInvoiceProposalCreateLines proposalCreateLines;
        ProjInvoiceProposalInsertLines projInvoiceProposalInsertLines;
        ProjEmplTransSale ProjEmplTransSale;
        PSATmpProjProposalTrans tmpProjProposalTrans;
        ProjEmplTrans ProjEmplTrans;
        ProjJournalTrans projJournalTrans;
        TransDate invoiceDate = DateTimeUtil::getToday(DateTimeUtil::getUserPreferredTimeZone());
        ProjProposalId proposalId;

        proposalCreateLinesParams.parmInvoiceDate(invoiceDate);
        proposalCreateLinesParams.parmInvoiceTypeSelection(ProjInvoiceTypeSelection::Both);

        proposalCreateLines = ProjInvoiceProposalCreateLines::newStandard(proposalCreateLinesParams.pack());
        proposalCreateLines.parmProposalCreateLinesParams().parmInvoiceDate(invoiceDate);

        projInvoiceProposalInsertLines = new ProjInvoiceProposalInsertLines(proposalCreateLines, false);

        select firstonly Voucher from ProjJournalTrans
            where ProjJournalTrans.JournalId == _journalId;

        while select ProjEmplTrans
            where ProjEmplTrans.VoucherJournal == ProjJournalTrans.Voucher
        {
            select firstonly ProjEmplTransSale
                where ProjEmplTransSale.TransId == ProjEmplTrans.TransId
                   && ProjEmplTransSale.FundingSource == _fundingSource;

            tmpProjProposalTrans.initFromProjEmplTrans(ProjEmplTrans);
            tmpProjProposalTrans.FundingSourceRefId = _fundingSource;
            tmpProjProposalTrans.LineAmount = ProjEmplTransSale.LineAmount * -1;
            tmpProjProposalTrans.SalesPrice = ProjEmplTransSale.SalesPrice;
            tmpProjProposalTrans.Selected = true;
            tmpProjProposalTrans.RefRecId = ProjEmplTransSale.RecId;
            tmpProjProposalTrans.RefTableId = ProjEmplTransSale.TableId;
            tmpProjProposalTrans.IndirectAmount = ProjEmplTransSale.PSAIndirectInvoice;
            tmpProjProposalTrans.insert();
        }

        new TestCreateInvoiceProposal(proposalCreateLines, false).createInvoiceProposals(tmpProjProposalTrans);

        select firstonly ProjEmplTrans
            where ProjEmplTrans.VoucherJournal == ProjJournalTrans.Voucher
        join ProjEmplTransSale
            where ProjEmplTransSale.TransId == ProjEmplTrans.TransId
               && ProjEmplTransSale.FundingSource == _fundingSource;

        proposalId = ProjTrans::newProjEmplTransSale(ProjEmplTrans, ProjEmplTransSale).proposalId();

        return proposalId;
    }
}
    

Main Method to Call the Above Code

Below is the main method to call the above code from a runnable class:


public class TestCreateHourJournalPostAndCreateInvProposalAndPostInvoice
{
    public static void main(Args _args)
    {
        JournalId journalId;
        ProjProposalId 	proposalId;
        InvoiceId 	invoiceId;
        
        TestCreateHourJournalPostAndCreateInvProposalAndPostInvoice objCreateHourJourAndCreateAndPostInvoice = new TestCreateHourJournalPostAndCreateInvProposalAndPostInvoice();

        journalId = objCreateHourJourAndCreateAndPostInvoice.createJournal();
        objCreateHourJourAndCreateAndPostInvoice.postJournal(journalId);
        proposalId = objCreateHourJourAndCreateAndPostInvoice.invoiceProposalCreation(journalId);
        invoiceId = objCreateHourJourAndCreateAndPostInvoice.postInvoiceProposal(proposalId);
        info(strfmt("invoice %1 posted",invoiceId);
    }
}
    

Wednesday, April 2, 2025

How to set background color for alternate row in SSRS report table D365FO

Setting Background Color for Alternate Rows in SSRS Report Table

Setting Background Color for Alternate Rows in SSRS Report Table

In this blog post, we will explore how to set the background color for alternate rows in an SSRS report table in Dynamics 365 Finance and Operations. This can be achieved using two different expressions:

1. Based on Unique Value in Table

To set the background color based on a unique value in the table, you can use the following expression:

=IIF(RUNNINGVALUE(Fields!FieldName.Value, CountDistinct, Nothing) Mod 2, "#ffffff", "#f0f0f0")

2. Based on Row Number

To set the background color based on the row number, you can use the following expression:

=IIF(RowNumber(Nothing) Mod 2, "#ffffff", "#f0f0f0")

Implementation

To implement these expressions in your SSRS report:

  1. Open your report in design in visual studio.
  2. Select the table where you want to apply the alternate row background color.
  3. Right-click on the data row (after table header) row and select Properties.
  4. In the BackgroundColor property, enter one of the expressions mentioned above.
  5. Click OK to apply the changes.

By following these steps, you can easily set the background color for alternate rows in your SSRS report table.

Please note - if you do the grouping on table then these background colors will also not show properly.

How to access extension class method on data entity field property

How to Use Extension Class Methods on Field Properties of Data Entity

How to Use Extension Class Methods on Field Properties of Data Entity

If you want to use your extension class method on your field property, then please use the following syntax:

extension class name :: method name

Example:

TestCustCustomerV3Entity_Extension::testCollectionAgent

Note: Brackets are not required.

Custom business event in D365FO

Implementing Custom Business Events in D365FO

Implementing Custom Business Events in D365FO

In this blog post, we'll walk through the steps to implement custom business events in Dynamics 365 Finance and Operations (D365FO). We'll cover creating a contract, defining a business event class, triggering the business event, building the solution, and rebuilding the business event catalog. Let's dive in!

Step 1: Create the Contract

First, we need to create a contract class that will define the data structure for our business event. Here's an example:


[BusinessEvents(classStr(TestCustTableUpdateBusinessEventContract), 'Test:TestCustTableUpdateBusinessEventContract', 'Test description', ModuleAxapta::AccountsReceivable)]
public class TestCustTableUpdateBusinessEventContract extends BusinessEventsContractBase
{
    CustTable custTable;

    public static TestCustTableUpdateBusinessEventContract newFromCustTable(CustTable _custTable)
    {
        TestCustTableUpdateBusinessEventContract contract = new TestCustTableUpdateBusinessEventContract();
        contract.parmcustTable(_custTable);
        return contract;
    }

    private CustTable parmcustTable(CustTable _custTable = custTable)
    {
        custTable = _custTable;
        return custTable;
    }
}
    

Step 2: Define the Business Event Class

Next, we'll define the business event class that will handle the event logic. Here's an example:


public class TestCustomerDataUpdateBusinessEvent extends BusinessEventsBase
{
    CustTable custTable;

    public static TestCustomerDataUpdateBusinessEvent newFromCustTable(CustTable _custTable)
    {
        TestCustomerDataUpdateBusinessEvent businessEvent = new TestCustomerDataUpdateBusinessEvent();
        businessEvent.parmcustTable(_custTable);
        return businessEvent;
    }

    private CustTable parmcustTable(CustTable _custTable = custTable)
    {
        custTable = _custTable;
        return custTable;
    }

    [Wrappable(true), Replaceable(true)]
    public BusinessEventsContract buildContract()
    {
        // Note: This method will be called only if the business event is activated.
        return TestCustTableUpdateBusinessEventContract::newFromCustTable(custTable);
    }
}
    

Step 3: Trigger the Business Event

Finally, we'll trigger the business event from the CustTable update method. Here's how you can do it:


[ExtensionOf(tableStr(CustTable))]
final class TestCustTable_Extension
{
    void update(boolean _updateSmmBusRelTable, boolean _updateParty)
    {
        next update(_updateSmmBusRelTable, _updateParty);

        // Call business event
        if (BusinessEventsConfigurationReader::isBusinessEventEnabled(classStr(TestCustomerDataUpdateBusinessEvent)))
        {
            CustTable custTable = CustTable::find(this.AccountNum);
            TestCustomerDataUpdateBusinessEvent::newFromCustTable(custTable).send();
        }
    }
}
    

Step 4: Build the Solution

After making the necessary changes, build the solution to ensure that all components are correctly compiled and integrated.

Step 5: Rebuild the Business Event Catalog

Now, go to System administration > Setup > Business events > Business event catalog form and rebuild the business event catalog. This step ensures that your new business event is registered and available for use.

Conclusion

By following these steps, you can implement custom business events in D365FO. This allows you to extend the functionality of your system and integrate with external systems or processes. Happy coding!

Tuesday, April 1, 2025

How to add Personnel Number field on the data entity D365FO

How to Add Personnel Number Field on the Data Entity in D365FO

How to Add Personnel Number Field on the Data Entity in D365FO

Follow these steps to add the Personnel Number field to the CustCustomerV3Entity data entity in Dynamics 365 Finance and Operations.

Step 1: Create Extension for CustCustomerV3Entity

Step 2: Add New Fields

Add two new fields: TestHcmWorkerRecId,TestPersonnelNumber.

Repeat this step for the staging table as well.

Step 3: Create Code Extension for CustCustomerV3Entity

Create a new code extension for CustCustomerV3Entity and add the following code:

[ExtensionOf(tableStr(CustCustomerV3Entity))] final class TESTCustCustomerV3Entity_Extension { /// <summary> /// Provides the query to be used to compute the value of TestPersonnelNumber field. /// </summary> /// <returns>A query to be used to compute the value of TestPersonnelNumber field.</returns> public static str testCollectionAgent() { return smmUtility::workerPersonnelNumberQuery( tablestr(CustCustomerV3Entity), dataEntityDataSourceStr(CustCustomerV3Entity, CustTable), fieldstr(CustTable, TestHcmWorkerRecId)); } public void mapEntityToDataSource(DataEntityRuntimeContext _entityCtx, DataEntityDataSourceRuntimeContext _dataSourceCtx) { if (_dataSourceCtx.name() == dataEntityDataSourceStr(CustCustomerV3Entity, CustTable)) { this.TestHcmWorkerRecId = smmUtility::getEntityWorkerRecId(this.testCollectionAgent); } next mapEntityToDataSource(_entityCtx, _dataSourceCtx); } }

Step 4: Configure Field Properties

On the TestPersonnelNumber field, set the DataEntityViewMethod property to TESTCustCustomerV3Entity_Extension::testCollectionAgent.

Step 5: Build and Synchronize

Build and synchronize the project.

Step 6: Test Using Postman

Test the changes using Postman to ensure everything is working correctly.

How to register purchase orders in D365FO using X++ code

Registering a Purchase Order in X++

Code Explanation


internal final class TESTJobToCheckRegistraion
{
    PurchFormLetterParmData          purchFormLetterParmData;
    PurchParmTable                   purchParmTable;
    PurchParmLine                    purchParmLine;
    PurchParmUpdate                  purchParmUpdate;
    PurchTable                       purchTable;
    PurchFormLetter                  purchFormLetter;
    TransDate                        packingSlipDate;
    PackingSlipId                    packingSlipId;

    public static void main(Args _args)
    {
        PurchTable    purchTable;
        PurchLine     purchLine;
        TransDate     packingSlipDate;
        PackingSlipId packingSlipId;
        container     lineNums;
        container     purchLineRecIds;
        container     inventBatchIds;
        container     qtys;
        int           i;

        purchTable      = PurchTable::find('002357-POR-UK01');
        packingSlipDate = DateTimeUtil::date(DateTimeUtil::utcNow());
        packingSlipId   = 'PK0001';

        lineNums       = [100]; // purch line numbers
        inventBatchIds = ['Batch001']; //batch numbers to register
        qtys           = [100]; // quantity to register

        for (i = 1; i <= conLen(lineNums); i++)
        {
            purchLine = PurchLine::find(purchTable.PurchId, conPeek(lineNums, i));
            purchLineRecIds = conIns(purchLineRecIds, conLen(purchLineRecIds) + 1, purchLine.RecId);
        }

        TESTJobToCheckRegistraion TESTJobToCheckRegistraion = new TESTJobToCheckRegistraion();
        TESTJobToCheckRegistraion.insertParmLineData(purchLineRecIds, inventBatchIds, qtys);
    }

    private void registerInventory(PurchParmLine _purchParmline, InventDim _inventDim)
    {
        InventTransWMS_Register inventTransWMS_register;
        TmpInventTransWMS       tmpInventTransWMS;
        InventDim               inventBatchCheck;
        InventTrans             inventTranslocal;
        InventDim               inventDimlocal;

        inventTranslocal = InventTrans::findTransId(_purchParmline.InventTransId, true);
        inventDimlocal   = inventTranslocal.inventDim();
        inventDimlocal.inventBatchId    = _inventDim.inventBatchId;
        inventDimlocal.InventLocationId = _inventDim.InventLocationId;
        inventDimlocal.InventSiteId     = _inventDim.InventSiteId;
        inventDimlocal                  = InventDim::findOrCreate(inventDimlocal);
        inventTransWMS_register         = inventTransWMS_register::newStandard(tmpInventTransWMS);

        inventTranslocal.inventDimId    = inventDimlocal.inventDimId;
        tmpInventTransWMS.clear();
        tmpInventTransWMS.initFromInventTrans(inventTranslocal);
        tmpInventTransWMS.ItemId    = inventTranslocal.ItemId;
        tmpInventTransWMS.InventQty = _purchParmline.ReceiveNow;
        tmpInventTransWMS.insert();

        inventTransWMS_register.writeTmpInventTransWMS(tmpInventTransWMS, inventTranslocal, inventDimlocal);
        inventTransWMS_register.updateInvent(inventTranslocal);
    }

    private void insertParmLineData(container _purchLineRecIds, container _inventBatchIds, container _qtys)
    {
        PurchLine purchLine;
        InventDim inventDim;
        int       i;

        for (i = 1; i <= conLen(_inventBatchIds); i++)
        {
            purchLine.clear();
            purchLine = PurchLine::findRecId(conPeek(_purchLineRecIds, i));
            purchParmLine.InitFromPurchLine(purchLine);
            inventDim = purchLine.inventDim();
            purchParmLine.ReceiveNow  = decRound(conPeek(_qtys, i), 2);
            purchParmLine.InventDimId = inventDim.inventDimId;
            purchParmLine.ParmId      = purchParmTable.ParmId;
            purchParmLine.TableRefId  = purchParmTable.TableRefId;
            purchParmLine.setQty(DocumentStatus::PackingSlip, false, true);
            purchParmLine.setLineAmount();
            purchParmLine.insert();
            inventDim.inventBatchId   = conPeek(_inventBatchIds, i);
            inventDim = inventDim::findOrCreate(inventDim);
            this.registerInventory(purchParmLine, inventDim);
        }
    }
}

   


Saturday, March 29, 2025

After product receipt from x++ code system is not creating project transactions

Resolving Project Transactions Issue in X++

Resolving Issue: Project Transactions Not Created After Product Receipt in X++

Introduction

When receiving a product receipt in Microsoft Dynamics 365 Finance and Operations via X++ code, you might encounter a scenario where project transactions are not generated, even when a valid project ID is associated with the purchase order. Debugging revealed that the system is not calling the runProjectPostings() method of the PurchFormLetter_PackingSlip class, which prevents the project packing slip from being posted. To resolve this, I created the runRemainProjectUpdates method in the code below.

Code Implementation

Product Receipt Creation


private TestPOReceiptResponseContract createProductReceipt(TestPOReceiptRequestContract _requestContract)
{
    PurchFormLetter purchFormLetter;
    PurchParmTable purchParmTable;
    PurchParmLine purchParmLine;
    PurchFormletterParmData purchFormLetterParmData;
    TestOrderLineRequestContract orderLineContract = _requestContract.parmOrderLineContract();
    PurchTable purchTable = PurchTable::find(orderLineContract.parmPurchaseOrderId());
    PurchLine purchLine = PurchLine::find(purchTable.PurchId, orderLineContract.parmLineNumber());

    purchFormLetterParmData = PurchFormletterParmData::newData(DocumentStatus::PackingSlip, VersioningUpdateType::Initial);
    purchFormLetterParmData.parmOnlyCreateParmUpdate(true);
    purchFormLetterParmData.createData(false);

    PurchParmUpdate purchParmUpdate = purchFormLetterParmData.parmParmUpdate();

    purchParmTable.clear();
    purchParmTable.TransDate = _requestContract.parmProducReceiptDate();
    purchParmTable.Ordering = DocumentStatus::PackingSlip;
    purchParmTable.ParmJobStatus = ParmJobStatus::Waiting;
    purchParmTable.Num = _requestContract.parmCoupaId();
    purchParmTable.PurchId = purchTable.PurchId;
    purchParmTable.insert();
    
    purchParmLine.initFromPurchLine(purchLine);
    purchParmLine.ReceiveNow = _requestContract.parmTotal();
    purchParmLine.ParmId = purchParmTable.ParmId;
    purchParmLine.insert();

    purchFormLetter = PurchFormLetter::construct(DocumentStatus::PackingSlip);
    purchFormLetter.transDate(_requestContract.parmProducReceiptDate());
    purchFormLetter.run();

    // Generate project item transactions
    if (purchTable.ProjId != '')
    {
        this.runRemainProjectUpdates(purchFormLetter, purchParmTable.ParmId);
    }
    
    return this.getResponseContract("@ENG_Labels:ProductReceiptCreated", true);
}
    

Explicitly Calling runRemainProjectUpdates to Create Project Transactions


public void runRemainProjectUpdates(PurchFormLetter _purchFormLetter, ParmId _parmId)
{
    FormletterOutputContract outputContract = _purchFormLetter.getOutputContract();
    VendPackingSlipVersion localVendPackingSlipVersion;
    VendPackingSlipJour localVendPackingSlipJour;
    VendPackingSlipTrans localVendPackingSlipTrans;
    PurchLine localPurchLine;
    SalesLine localSalesLine;
    SalesFormLetter salesFormLetter;

    if (ProjParameters::find().AutomaticItemConsumption == NoYes::Yes)
    {
        select firstonly RecId, AccountingDate from localVendPackingSlipVersion
            where localVendPackingSlipVersion.ParmId == _parmId
            exists join localVendPackingSlipJour
                where localVendPackingSlipVersion.VendPackingSlipJour == localVendPackingSlipJour.RecId
                exists join localVendPackingSlipTrans
                    where localVendPackingSlipTrans.VendPackingSlipJour == localVendPackingSlipJour.RecId
                    exists join localPurchLine
                        where localPurchLine.InventTransId == localVendPackingSlipTrans.InventTransId
                        && localPurchLine.ItemRefType == InventRefType::Sales
                        exists join localSalesLine
                            where localSalesLine.InventTransId == localPurchLine.InventRefTransId
                            && localSalesLine.ProjId;

        if (localVendPackingSlipVersion.RecId)
        {
            salesFormLetter = SalesFormLetter::newFromPurchFormLetter_PackingSlip(
                SysOperationHelper::base64Encode(outputContract.parmJournalLinesPacked()),
                DocumentStatus::ProjectPackingSlip);

            if (localVendPackingSlipVersion.AccountingDate)
            {
                salesFormLetter.transDate(localVendPackingSlipVersion.AccountingDate);
            }

            salesFormLetter.runOperation();
        }
    }
}
    

How to register return sales order lines using X++ code

How to Register Return Sales Order Lines in X++

The TestSalesReturnOrderLineRegister Class in X++

The TestSalesReturnOrderLineRegister class serves as an entry point to the product registration process, specialized for the sales return order process. It handles the return disposition code and manages sales return order specifics, such as creating inventory transactions.

Code Overview



class TestSalesReturnOrderLineRegister extends TradeOrderLineRegister
{
    ReturnDispositionCodeId returnDispositionCodeId;
    DialogField dialogDispositionCodeId;
    SalesLine salesLine;
    boolean reverseInventTrans;
    boolean recreateReservationLine;
    boolean inRegistration;

    public boolean init()
    {
        boolean ret = super();
        salesLine = salesPurchLine;
        return ret;
    }

    public void run()
    {
        this.runPreSuper();
        super();
        this.runPostSuper();
    }

    public void runPostSuper()
    {
        salesLine = SalesLine::findInventTransId(salesLine.InventTransId, true);
        if (inRegistration)
        {
            if (salesLine.ReturnStatus == ReturnStatusLine::Awaiting)
            {
                if (reverseInventTrans && !salesLine.ReturnAllowReservation)
                {
                    SalesLine::changeReturnOrderType(salesLine.InventTransId, null, true);
                    salesLine = SalesLine::findInventTransId(salesLine.InventTransId, true);
                }
                if (salesLine.ReturnDispositionCodeId)
                {
                    salesLine.ReturnDispositionCodeId = '';
                    salesLine.update();
                }
            }
        }
        else 
        {
            if (salesLine.ReturnStatus == ReturnStatusLine::Registered && recreateReservationLine)
            {
                salesLine.createReturnReservationLine();
                salesLine = SalesLine::findInventTransId(salesLine.InventTransId, true);
            }
        }
    }
}
    

Calling the TestSalesReturnOrderLineRegister Class


public void registerReturnSOLine(SalesId _salesId, ReturnDispositionCodeId _dispositionCode)
{
    TradeOrderLineRegister tradeOrderlineRegister;
    TestSalesReturnOrderLineRegister salesReturnOrderLineRegister;
    InventTransWMS_Register inventTransWMS_register;
    TmpInventTransWMS TmpInventTransWMS;
    SalesLine salesLine;
    InventTrans inventTrans;
    InventTransOrigin inventTransOrigin;
    returnDispositionCode returnDispositionCode;

    Args args = new Args();
    while select forUpdate salesLine where salesLine.SalesId == _returnSOTable.SalesId
    {
        args.record(salesLine);
        tradeOrderLineRegister = TestSalesReturnOrderLineRegister::construct();
        tradeOrderLineRegister.parmArgs(args);
        tradeOrderLineRegister.init();
        salesReturnOrderLineRegister = tradeOrderLineRegister;
        salesReturnOrderLineRegister.runPreSuper();
        
        select firstOnly returnDispositionCode
            where returnDispositionCode.DispositionAction == DispositionAction::Credit;
        
        salesLine.ReturnDispositionCodeId = returnDispositionCode.DispositionCodeId;
        salesLine.update();

        select firstOnly crossCompany inventTrans
            join RecId, InventTransId from inventTransOrigin
                where inventTransOrigin.InventTransId == salesLine.InventTransId
                && inventTrans.InventTransOrigin == inventTransOrigin.RecId;

        inventTransWMS_register = inventTransWMS_register::newStandard(tmpInventTransWMS);
        tmpInventTransWMS.clear();
        tmpInventTransWMS.initFromInventTrans(inventTrans);
        tmpInventTransWMS.InventDimId = inventTrans.InventDimId;
        tmpInventTransWMS.insert();

        inventTransWMS_register.writeTmpInventTransWMS(tmpInventTransWMS, inventTrans, inventTrans.inventDim());
        if (!inventTransWMS_register.updateInvent(salesLine))
        {
            throw error("Error during registration of sales line.");
        }
    }
}
    

Wednesday, March 26, 2025

How to create COC for form methods , How to get formRun , How to make control visible false on form in D365FO

How to create COC for form method in D365FO

How to create COC for form method in D365FO

Example Code

[ExtensionOf(formStr(ProjInvoiceListPage))]
final class TestProjInvoiceListPage_Extension
{
    public void init()
    {
        next init();

        FormRun formRun = this as FormRun;

        if(ProjParameters::find().TestPrintPGGDesignForProjectInvoice)
        {
            FormControl         stdViewCopyBtn   = formrun.design(0).controlName('PrintProjInvoiceCopyButton');
            FormControl         stdViewOrigBtn   = formrun.design(0).controlName('PrintProjInvoiceButton');
            stdViewCopyBtn.visible(false);
            stdViewOrigBtn.visible(false);
        }
        else
        {
            FormControl         customViewCopyBtn   = formrun.design(0).controlName('TestPSAProjInvoiceCopy');
            FormControl         customViewOrigBtn   = formrun.design(0).controlName('TestPSAProjInvoiceOriginal');

            customViewCopyBtn.visible(true);
            customViewOrigBtn.visible(true);
        }
    }
}

Thursday, February 6, 2025

Fix Dynamics 365FO Synchronization Error - Transaction Log Full

Fix Dynamics 365FO Synchronization Error - Transaction Log Full

Fix: The transaction log for database is full due to 'log_backup' - Dynamics 365FO

If you're encountering the error "The transaction log for database is full due to 'log_backup'" while working with Dynamics 365 Finance and Operations (D365FO), it means your SQL Server transaction log has reached its maximum capacity.

Solution

Follow these steps to resolve the issue:

Step 1: Check the Database Files

Run the following SQL query to check the database file details:

    SELECT * FROM sys.database_files
    

Note down the log file name from the result and use that same name in the second statement of the step 2 SQL query.

Query result screenshot:

Database files query result

Step 2: Shrink the Transaction Log

Execute the following commands to shrink the transaction log and free up space:

    ALTER DATABASE AxDB SET RECOVERY SIMPLE;
    DBCC SHRINKFILE('AxDb_New_log', 0, TRUNCATEONLY);
    ALTER DATABASE AxDB SET RECOVERY FULL;
    

Replace 'AxDb_New_log' with the actual log file name you noted earlier.

Step 3: Start DB synchronization again...this time it should work

..

Follow "Dynamics World" blog also for some other approach

Tip: Always ensure you have proper database backups before making changes to recovery models.

Wednesday, January 22, 2025

Get Description of financial dimension value from SQL DB of D365FO

Retrieve Financial Dimension Value Description using SQL Query in D365FO

How to Retrieve Financial Dimension Value Description with SQL in D365FO

In this post, we will explore how to retrieve the description of a financial dimension value in an ERP or financial management system using a specific SQL query in D365FO.

The Query


SELECT DIMATTVALUE.DISPLAYVALUE AS 'DIMENSIONVALUE',
       DIMATT.NAME AS 'FINANCIALDIMENSION',
       DIMATTVALUE.ISSUSPENDED,
       HCMWORKER.PERSONNELNUMBER AS 'OWNER',
       DimFinTag.DESCRIPTION
FROM DIMENSIONATTRIBUTEVALUE AS DIMATTVALUE
LEFT JOIN HCMWORKER ON DIMATTVALUE.OWNER = HCMWORKER.RECID
JOIN DIMENSIONATTRIBUTE AS DIMATT ON DIMATT.RECID = DIMATTVALUE.DIMENSIONATTRIBUTE
JOIN DIMENSIONATTRIBUTEDIRCATEGORY dimAttDirCategory 
ON dimAttDirCategory.DIMENSIONATTRIBUTE = DIMATTVALUE.DIMENSIONATTRIBUTE
JOIN DIMENSIONFINANCIALTAG DimFinTag ON DIMATTVALUE.DISPLAYVALUE = DimFinTag.VALUE 
AND DimFinTag.FINANCIALTAGCATEGORY = dimAttDirCategory.DIRCATEGORY
WHERE DIMATTVALUE.ISDELETED = 0
  AND DIMATTVALUE.ISSUSPENDED = 0
        

Feel free to leave a comment below if you have any questions or need further clarification on the query or its results!

Monday, January 13, 2025

Call custom service with JSON body in D365FO

Call Custom Service with JSON Body in D365FO

Call Custom Service with JSON Body in D365FO

        
FileUploadTemporaryStorageResult fileUpload = File::GetFileFromUser() as FileUploadTemporaryStorageResult;
System.IO.Stream stream = fileUpload.openResult();
str responceJson;

using (var reader = new System.IO.StreamReader(stream))
{
    responceJson = reader.ReadToEnd();

    TESTPOReceiptRequestContract contract = FormJsonSerializer::deserializeObject(
        classnum(TESTPOReceiptRequestContract), responceJson);

    new TESTPOReceiptService().processRecord(contract);
}
        
    

Friday, July 19, 2024

How to Post Partial Product Receipt in D365FO Using X++ Code

How to Post Partial Product Receipt in D365FO Using X++ Code

How to Post Partial Product Receipt in D365FO Using X++ Code

Handling partial product receipts in Microsoft Dynamics 365 for Finance and Operations (D365FO) can be crucial for businesses that receive shipments in stages. In this blog, we'll explore how to post a partial product receipt using X++ code. We'll refer to a specific code example to guide us through the process.

Introduction

Partial product receipt refers to the process of receiving a portion of the goods ordered rather than the entire quantity. This is common in scenarios where suppliers deliver products in multiple shipments. Posting partial product receipts accurately in D365FO ensures that inventory records reflect the actual quantities received.

Code Example Overview

Here's an example of X++ code that demonstrates how to post a partial product receipt. This example focuses on receiving a specific quantity of a purchase order line.


internal final class POReceiptJobTest
{
    /// 
    /// 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.
    /// 
    /// The specified arguments.
    public static void main(Args _args)
    {
       PurchTable                  purchTable;
       PurchFormLetter             purchFormLetter;
       PurchFormletterParmData     purchFormLetterParmData;
       PurchParmTable              purchParmTable;
       PurchLine                   purchLine;
       PurchParmLine               purchParmLine;

       ttsBegin;

       // Find the purchase order
       purchTable = PurchTable::find('000039');

       // Create PurchParamUpdate table
       purchFormLetterParmData = PurchFormletterParmData::newData(
                        DocumentStatus::PackingSlip,
                        VersioningUpdateType::Initial);

       purchFormLetterParmData.parmOnlyCreateParmUpdate(true);
       purchFormLetterParmData.createData(false);
       PurchParmUpdate purchParmUpdate = purchFormLetterParmData.parmParmUpdate();

       // Set PurchParmTable table
       purchParmTable.clear();
       purchParmTable.TransDate                = SystemDateGet();
       purchParmTable.Ordering                 = DocumentStatus::PackingSlip;
       purchParmTable.ParmJobStatus            = ParmJobStatus::Waiting;
       
       purchParmTable.Num                      = 'Test234567'; // PR
       purchParmTable.PurchId                  = purchTable.PurchId;
       purchParmTable.PurchName                = purchTable.PurchName;
       purchParmTable.DeliveryName             = purchTable.DeliveryName;
       purchParmTable.DeliveryPostalAddress    = purchTable.DeliveryPostalAddress;
       purchParmTable.OrderAccount             = purchTable.OrderAccount;
       purchParmTable.CurrencyCode             = purchTable.CurrencyCode;
       purchParmTable.InvoiceAccount           = purchTable.InvoiceAccount;
       purchParmTable.ParmId                   = purchParmUpdate.ParmId;
       purchParmTable.insert();

       // Set PurchParmLine table
       select purchLine
           where purchLine.PurchId     == purchTable.PurchId &&
           purchLine.LineNumber  == 1;
    
       purchParmLine.initFromPurchLine(purchLine);

       purchParmLine.ReceiveNow    = 5; // Partial quantity received
       purchParmLine.ParmId        = purchParmTable.ParmId;
       purchParmLine.TableRefId    = purchParmTable.TableRefId;
       purchParmLine.setQty(DocumentStatus::PackingSlip, false, true);
       purchParmLine.setLineAmount();
       purchParmLine.insert();

       // Post the packing slip
       purchFormLetter = PurchFormLetter::construct(DocumentStatus::PackingSlip);
       purchFormLetter.transDate(systemDateGet());
       purchFormLetter.proforma(false);
       purchFormLetter.specQty(PurchUpdate::All);
       purchFormLetter.purchTable(purchTable);
       purchFormLetter.parmParmTableNum(purchParmTable.ParmId);
       purchFormLetter.parmId(purchParmTable.ParmId);
       purchFormLetter.purchParmUpdate(purchFormLetterParmData.parmParmUpdate());
       purchFormLetter.run();

       ttsCommit;
    }
}

    

If you have any questions or need further assistance, feel free to leave a comment below!

Monday, July 15, 2024

How to Return Date Difference in Dynamics 365 Finance and Operations (D365FO) Computed Column

How to Return Date Difference in Dynamics 365 Finance and Operations (D365FO) Computed Column

In Dynamics 365 Finance and Operations (D365FO), computed columns can dynamically calculate and display values derived from other fields or system values. This guide will show you how to create a computed column that calculates the difference in days between a record's creation date and the current date.

Steps to Create a Computed Column and Method

1. Define the Computed Column in the AOT

  1. Open Visual Studio and navigate to the Application Object Tree (AOT).
  2. Expand the Data Dictionary node.
  3. Right-click on Views and select the custom view / Extension of standad view, where you want to add the computed column (e.g., AllPOLinesView).
  4. Click on New > Computed Column.
  5. Name your computed column (e.g., ApprovalDays).
  6. Set the ComputedColumnExpression property to the method you will define for calculating the date difference (e.g., AllPOLinesView::compColumnApprovalDays()).

2. Implement the Computed Column Method (compColumnApprovalDays)

Add the following method to your view's code to calculate the date difference:

private static server str compColumnApprovalDays()
{
    return SysComputedColumn::getDateDiff(
                SysComputedColumn::returnField(tableStr(AllPOLinesView), identifierStr(PurchLine),
                                        fieldId2name(tableNum(PurchLine), fieldNum(PurchLine, CreatedDateTime))),
                SysComputedColumn::getCurrentUtcDate(),
                SysComputedColumnDatePart::Day);
}
    

Explanation of the Method:

  • SysComputedColumn::returnField(tableStr(AllPOLinesView), identifierStr(PurchLine), fieldId2name(tableNum(PurchLine), fieldNum(PurchLine, CreatedDateTime))): Retrieves the CreatedDateTime field from the PurchLine data source in the AllPOLinesView.
  • SysComputedColumn::getCurrentUtcDate(): Gets the current UTC date.
  • SysComputedColumn::getDateDiff(...): Computes the difference in days between the CreatedDateTime field and the current UTC date.
  • SysComputedColumnDatePart::Day: Specifies that the date difference should be calculated in days.

3. Compile and Synchronize

  • Compile your changes in Visual Studio.
  • Synchronize your database to reflect the changes in D365FO.

Using the Computed Column

After setting up the computed column, it will automatically calculate and display the approval days based on the creation date (CreatedDateTime field) for each record in the AllPOLinesView.

Example Usage

Here’s how you can use the computed column in a form or report:

  1. Navigate to the form or report that displays the AllPOLinesView, also make sure ApprovalDays is added on report/Form.
  2. The ApprovalDays computed column will show the number of days between the creation date and the current date for each purchase order line.

Summary

By following these steps, you can create a computed column in D365FO to calculate date differences dynamically. This method enhances data presentation and provides valuable insights based on the computed values. Computed columns like this one can automate calculations and streamline your business processes in D365FO.

Understanding RecVersion, RecId, and Partition Fields in Dynamics 365FO Tables

Understanding RecVersion, RecId, and Partition Fields in Dynamics 365FO Tables

In Dynamics 365 Finance and Operations (D365FO), the RecVersion, RecId, and Partition fields play crucial roles in the data management and integrity of tables. Here’s a detailed explanation of each:

RecVersion

Purpose: The RecVersion field, also known as the "row version" field, is used for optimistic concurrency control.

Functionality: It ensures data integrity when multiple users or processes attempt to update the same record simultaneously.

How it works: When a record is updated, the system checks the RecVersion value. If the RecVersion in the database matches the RecVersion of the record being updated, the update proceeds, and the RecVersion is incremented. If the values do not match, it means another user has updated the record, and the update will be rejected or retried, depending on the application logic.

Example:

public void updateRecord(TableName tableName, int someRecId, int expectedRecVersion, anytype newValue) 
{
    select forupdate tableName where tableName.RecId == someRecId;
    if (tableName.RecVersion == expectedRecVersion) 
    {
        tableName.SomeField = newValue;
        tableName.doUpdate();
    } 
    else 
    {
        // Handle the conflict, e.g., by retrying or notifying the user
    }
}

RecId

Purpose: The RecId field is a unique identifier for each record in a table.

Functionality: It acts as the primary key for the table and is automatically managed by the system.

How it works: When a new record is inserted, the system assigns a unique RecId. This ensures that each record can be uniquely identified and referenced.

Use cases:

  • Foreign key references in related tables.
  • Efficient lookups and indexing.

Example:

public TableName findRecord(int someRecId) 
{
    select tableName where tableName.RecId == someRecId;
    if (tableName.RecId) 
    {
        // Record found, perform operations
    }
    return tableName;
}

Partition

Purpose: The Partition field is used for data partitioning in multi-tenant environments.

Functionality: It allows for logical separation of data for different tenants within the same database.

How it works: Each tenant's data is tagged with a unique partition value. This enables the system to quickly segregate and manage data for different tenants.

Note: In the current versions of D365FO, the use of the Partition field has been deprecated, but it's still present for backward compatibility and in the underlying table structure.

Example:

public TableName findRecordByPartition(int currentPartition, int someRecId) 
{
    select tableName where tableName.Partition == currentPartition && tableName.RecId == someRecId;
    return tableName;
}

Summary

  • RecVersion: Ensures data integrity by preventing conflicts during simultaneous updates.
  • RecId: Provides a unique identifier for each record, facilitating efficient data retrieval and relationships between tables.
  • Partition: Used for data segregation in multi-tenant setups, though now deprecated in favor of other data isolation mechanisms.

These fields are integral to maintaining the performance, scalability, and integrity of data in Dynamics 365 Finance and Operations.

Wednesday, July 3, 2024

Data management package REST API - Import package in dynamics 365FO using postman

Data Management Package REST API - Import Package in Dynamics 365FO Using Postman

Data Management Package REST API - Import Package in Dynamics 365FO Using Postman

Welcome to our comprehensive guide on using the Data Management Package REST API to import data into Dynamics 365 for Finance and Operations (Dynamics 365FO) efficiently. This tutorial will walk you through each step of the process using Postman.Please ensure your Bearer token is generated before proceeding.

This same URL which i am going to use can be trigger from any third party system to integrate with dynamics D365 Finance and operations.

Step 1: Create Import Project

Create an import project in Data Management as shown below.

Create Import Project

Step 2: Get Azure Write URL

Post the GetAzureWriteUrl request to the following endpoint:

POST - {{resource}}/data/DataManagementDefinitionGroups/Microsoft.Dynamics.DataEntities.GetAzureWriteUrl

Body:

{
    "uniqueFileName": "CustomerGroupTestFile"
}

A successful response from GetAzureWriteUrl will provide the necessary URL for the next steps.

Successful response of GetAzureWriteUrl

Step 3: Generate Package

You can generate the package in two ways:

  1. Generate the package as shown in this blog.
  2. Generate the package by exporting it directly from the DMF project as shown below.

Steps to Export Package:

  1. Create an export project.
  2. Run the export project.
  3. Download the package as shown below.
Create Export Project Run Export Package

Prepare your template in an Excel file.

Download Package

Save it as a Zip. Ensure the Zip contains only the three files shown below.

Extracted Files

Step 4: Upload Package

Copy the highlighted blob URL from the previous response of GetAzureWriteUrl highlited in below screen response section..

Highlighted Blob URL

Paste the URL in the URL section select Put method.

Paste URL and Set Parameters

Set the below parameter x-ms-blob-type to BlockBlob.

Set Blob Type

After sending the request, you will receive the response shown below:

Response After Send

Step 5: Import From Package

Post the ImportFromPackage request with the following body:

POST : {{resource}}/data/DataManagementDefinitionGroups/Microsoft.Dynamics.DataEntities.ImportFromPackage

Body:
{
    "packageUrl": "URL on which we have uploaded the package.",
    "definitionGroupId": "VendorGroupsFileImport",
    "executionId": "",
    "execute": true,
    "overwrite": true,
    "legalEntityId": "USMF"
}
Import From Package

Headers for ImportFromPackage

Headers for ImportFromPackage

The response of ImportFromPackage

Response of ImportFromPackage

New batch history record created under the import group.

Headers for ImportFromPackage

Conclusion

By following these detailed steps, you can effectively manage data imports using the Data Management Package REST API in Dynamics 365FO. This approach not only ensures accuracy but also enhances the efficiency of handling large data sets within your organization.

Update Values During Data Entity Import in D365FO

Update Values During Data Entity Import in D365FO Updating Field Values During Data Entity Import in Dynamics 365 Finance ...