How to Retrieve All Vendor Location IDs in X++ (Beyond Primary Addresses) in Microsoft Dynamics 365 Finance & Operations

How to Retrieve All Vendor Location IDs in X++ (Beyond Primary Addresses) in Microsoft Dynamics 365 Finance & Operations

This method builds a custom lookup to fetch all location IDs linked to a vendor, regardless of whether they are marked as primary.


✅ Key Idea

Instead of relying on flags like IsPrimary, this approach:

  • Starts from LogisticsLocation (root)
  • Joins through party and vendor tables
  • Filters only by Vendor Account
  • Returns all related locations
💻 Code with Explanation

public void lookup()
{
    // Create lookup object for LogisticsLocation table
    SysTableLookup sysTableLookup = SysTableLookup::newParameters(tableNum(LogisticsLocation), this);

    // Initialize query
    Query query = new Query();

    // Declare data sources (tables)
    QueryBuildDataSource qbdsLogisticsLocation, qbdsDirParty, qbdsVendTable,
             qbdsTaxInfo, qbdsTaxReg;

    // ✅ ROOT DATASOURCE
    // Start from LogisticsLocation to ensure we fetch ALL locations
    qbdsLogisticsLocation = query.addDataSource(tableNum(LogisticsLocation));

    // 🔗 Join DirPartyPostalAddressView (connects location to party)
    qbdsDirParty = qbdsLogisticsLocation.addDataSource(tableNum(DirPartyPostalAddressView));
    qbdsDirParty.relations(false); // Disable auto relations
    qbdsDirParty.joinMode(JoinMode::InnerJoin);
    qbdsDirParty.addLink(
        fieldNum(LogisticsLocation, RecId), 
        fieldNum(DirPartyPostalAddressView, Location)
    );

    // 🔗 Join VendTable (connect party to vendor)
    qbdsVendTable = qbdsDirParty.addDataSource(tableNum(VendTable));
    qbdsVendTable.relations(false);
    qbdsVendTable.joinMode(JoinMode::InnerJoin);
    qbdsVendTable.addLink(
        fieldNum(DirPartyPostalAddressView, Party), 
        fieldNum(VendTable, Party)
    );

    // ✅ Filter by Vendor Account
    qbdsVendTable.addRange(fieldNum(VendTable, AccountNum))
        .value(queryValue(MvdVendorInvoiceHeader.VendAccount));

    // 🔗 Join TaxInformation_IN (location-based GST info)
    qbdsTaxInfo = qbdsDirParty.addDataSource(tableNum(TaxInformation_IN));
    qbdsTaxInfo.relations(false);
    qbdsTaxInfo.joinMode(JoinMode::InnerJoin);
    qbdsTaxInfo.addLink(
        fieldNum(DirPartyPostalAddressView, Location), 
        fieldNum(TaxInformation_IN, RegistrationLocation)
    );

    // 🔗 Join TaxRegistrationNumbers_IN (GST registration details)
    qbdsTaxReg = qbdsTaxInfo.addDataSource(tableNum(TaxRegistrationNumbers_IN));
    qbdsTaxReg.relations(false);
    qbdsTaxReg.joinMode(JoinMode::InnerJoin);
    qbdsTaxReg.addLink(
        fieldNum(TaxInformation_IN, GSTIN), 
        fieldNum(TaxRegistrationNumbers_IN, RecId)
    );

    // ✅ Optional filter: Registration Number
    if (MvdVendorInvoiceHeader.RegistrationNumber)
    {
        qbdsTaxReg.addRange(fieldNum(TaxRegistrationNumbers_IN, RegistrationNumber))
            .value(queryValue(MvdVendorInvoiceHeader.RegistrationNumber));
    }

    // 🔽 Sort results by LocationId
    qbdsLogisticsLocation.addSortField(
        fieldNum(LogisticsLocation, LocationId), 
        SortOrder::Ascending
    );

    // 📌 Define fields to display in lookup
    sysTableLookup.addLookupfield(fieldNum(LogisticsLocation, LocationId));
    sysTableLookup.addLookupfield(fieldNum(LogisticsLocation, Description));

    // 🔗 Assign query to lookup
    sysTableLookup.parmQuery(query);

    // 🚀 Execute lookup
    sysTableLookup.performFormLookup();
}

Comments

Popular posts from this blog

Product Tax Rate Search Report (HSN & SAC) in D365 F&O

Fetching Mandatory Default Dimensions from Account Structure in D365 F&O