Tuesday, August 29, 2017

Dynamics 365 -- Custom Lookups

I just started working on Dynamics 365 over the last few weeks.  It's been a bit of a rocky ride working with the new extension model.  I recently had a requirement to add the ProjId to the PurchaseReqTable form and limit the option to projects that have been assigned to the current user.

In order to do this, I created a lookup in-memory table and populated it with the correct data.  However, when I wrote the lookup code, I kept on getting an error,  “More than one form was opened at once for the lookup control.”  I looked and looked and looked and could not find a resolution.  I had thought it was because the ProjId has an EDT relation to the ProjTable and also had a predefined lookup form.  This would have meant that all of this would have be undone.  Furthermore, it would mean that the customized lookup would have to be used on all forms that used a ProjId.  This was not a good approach.  By sheer luck, I finally found a post at https://ievgensaxblog.wordpress.com/2016/05/16/ax-7-how-to-override-form-control-methods-using-extensions/  that explained how to suppress the system generated lookup.

My version of the code looked like:

[FormControlEventHandler(formControlStr(PurchReqTable, PurchReqLine_ProjId), FormControlEventType::Lookup)]
 public static void PurchReqLine_ProjId_OnLookup(FormControl sender, FormControlEventArgs e)
    {
        CPLookupTmp lookup;
        SysTableLookup sysTableLookup;
        FormControlCancelableSuperEventArgs csea = e as FormControlCancelableSuperEventArgs;
        ;
        lookup = CPLookupTmp::populatePropertiesForUser();
        lookup.setTmpData(lookup);
        sysTableLookup = SysTableLookup::newParameters(tableNum(CPLookupTmp), sender);
        sysTableLookup.addLookupfield(fieldNum(CPLookupTmp,CPId));
        sysTableLookup.setLabel('@CP:PropertyId');
        sysTableLookup.addLookupfield(fieldNum(CPLookupTmp,CPValue));
        sysTableLookup.setLabel('@CP:PropertyName');
        sysTableLookup.parmTmpBuffer(lookup);
        sysTableLookup.performFormLookup();
        csea.CancelSuperCall();
    }

The important point here is the new class: FormControlCancelableSuperEventArgs .  The CancelSuperCall method is what stops the base lookup form from being called.

No comments:

Post a Comment