Saturday, May 1, 2021

D365 - creating an externalItemId Lookup for Sales Orders, Purchase Orders and Sales Quotations

 For some reason, the external item field on these forms do not have lookups. If you want to set the external item manually, you would need a lookup on the field. Standard AX allows the user to type any string into the externalItemId field, it doesn't actually have to be in the external item table. We had a requirement to keep that functionality. If you wanted to limit the selection to only the items in the table you would need to add a relation on the salesLine, salesquotationline, and purchline tables to the CustVendExternalItem table and make sure validation is set to yes.

Doing this is not all that difficult. First you need to create a lookup on the custVendExternalItem table and then override the lookup on the form(s) you want to use it.

1. Here is the lookup code on the custVendExternalItem table

[ExtensionOf(tableStr(CustVendExternalItem))]
final class ModelCustVendExternalItem_Extension
{
    public static void modelLookupExternalItem(FormControl _formControl, CustVendRel _accountNum, ItemId _itemid)
    {
        Query query = new Query();
        QueryBuildDataSource qbds;
        QueryBuildRange qbr;

        SysTableLookup sysTableLookup = SysTableLookup::newParameters(tableNum(CustVendExternalItem),_formControl);

        sysTableLookup.addLookupfield(fieldNum(CustVendExternalItem, ExternalItemId),true);

        sysTableLookup.addLookupfield(fieldNum(CustVendExternalItem, ItemId));

        sysTableLookup.addLookupfield(fieldNum(CustVendExternalItem, CustVendRelation));


        qbds = query.addDataSource(tableNum(CustVendExternalItem));

        qbds.addRange(fieldNum(CustVendExternalItem, custvendrelation)).value(_accountNum);

        qbds.addRange(fieldNum(CustVendExternalItem, ItemId)).value(_itemid);

        sysTableLookup.parmQuery(query);

        sysTableLookup.performFormLookup();        

    }

}

2. Here is the code for the sales order form
[ExtensionOf(formStr(SalesTable))]
final class ModelSalesTableForm_Extension
{
    void init()
    {
        next init();

        Sales_ExternalItemId.registerOverrideMethod(methodStr(FormDataObject, lookup), formMethodStr(SalesTable, modelExternalItemLookup));
    }

    public void modelExternalItemLookup(FormStringControl _callingControl)
    {
        CustVendExternalItem::modelLookupExternalItem(_callingControl,SalesTable.CustAccount,SalesLine.itemId);
    }

}

3. The code for the purchase order form is very similar
[ExtensionOf(formStr(PurchTable))]
final class ModelPurchTableForm_Extension
{
    void init()
    {
        next init();

        PurchLine_ExternalItemId.registerOverrideMethod(methodStr(FormDataObject, lookup), formMethodStr(PurchTable, modelExternalItemLookup));
    }

    public void modelExternalItemLookup(FormStringControl _callingControl)
    {
        CustVendExternalItem::modelLookupExternalItem(_callingControl,PurchTable.OrderAccount,PurchLine.itemId);
    }

}

4. The code for the sales quotation form is also similar:

[ExtensionOf(formStr(SalesQuotationTable))]
final class modelSalesQuotationTableForm_Extension
{
    void init()
    {
        next init();

        Sales_ExternalItemId.registerOverrideMethod(methodStr(FormDataObject, lookup), formMethodStr(SalesQuotationTable, modelExternalItemLookup));
    }

    public void modelExternalItemLookup(FormStringControl _callingControl)
    {
        CustVendExternalItem::modelLookupExternalItem(_callingControl,SalesQuotationTable.custAccount,SalesQuotationLine.itemId);
    }

}

No comments:

Post a Comment