Thursday, December 3, 2020

Applying range on the lookup based on the selection of value on other field X++

Hi folks,


Since, I have been working on Dynamics 365 Finance and Operations development for a while. I would like to share some important tips and tricks related to D365 FO development.

I had been given a scenario where I needed to show a filtered value based on the selection of value on the other field.

Since, you guys are already familiar on how to create lookups in Dynamics AX 2012 by overriding lookup method. In D365, there is a slight change in creating lookups in standard objects because overlayering is no more allowed.

You need to first create form extension class. 
[ExtensionOf(formStr(MarkupTrans))]
final class MarkupTransForm_Extension
{
}

Copy the event handler method signature, paste it in your extension class and then write the logic below:
    /// <summary>
    /// Added a custom field "Charges categories" in MarkupTable which is used for setting Charges codes. Lookup values of Charges codes will be populated based on the selection of Charges categories that are setup in Charges code setup form.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    [FormControlEventHandler(formControlStr(MarkupTrans, MarkupTrans_MarkupCode), FormControlEventType::Lookup)]
    public static void MarkupTrans_MarkupCode_OnLookup(FormControl sender, FormControlEventArgs e)
    {
        FormRun                              element;
        FormControl                         formCtrl;
        Query                                   query;
        QueryBuildDataSource        markupTrans;
        SysTableLookup                  sysTableLookup;
        
        element  = sender.formRun();
        formCtrl = element.design().controlName(formControlStr(MarkupTrans,        ChargesCategory));

        sysTableLookup = SysTableLookup::newParameters(tableNum(MarkupTable), sender);
        
        query = new Query();
        markupTrans = query.addDataSource(tableNum(MarkupTable));
        sysTableLookup.addLookupfield(fieldNum(MarkupTable, MarkupCode));

        //below code is used to get the filtered lookup based on the other field value.
        markupTrans.addRange(fieldNum(MarkupTable, ChargesCategory)).value(formCtrl.valueStr());

        sysTableLookup.parmQuery(query);
        sysTableLookup.performFormLookup();

        FormControlCancelableSuperEventArgs event = e as FormControlCancelableSuperEventArgs;
        event.CancelSuperCall();
    }

No comments:

Post a Comment

Enable and Disable Vendor invoice button on PO list and details page X++

Hi folks, This blog will be useful where vendor invoice process is required to be controlled based on certain conditions. Requirements: Proc...