Pages

Wednesday, April 26, 2017

Dynamics 365 for Operations: Override Form Control Lookup


Noted:  This content is based on Dynamics 365 for Operations Platform Update 5 


It's been really long time, right?   I changed job 2 years ago and was too busy with projects.  Since right now, we are moving to Dynamics 365.  While writing this blog, I'm doing self-learning (started 3 days ago) and work with a project together.  So, I want to make a note for later reference. 


Scenario: 

In FreeText invoice form, create a lookup invoice list of current customer in the freeText invoice line.

I'll skip how to create new field in Dynamics 365 since there are many blogs mentioned already.  This will show the event handler class for the field in form only.  

Referenced packages:

Because we are going to write code about Query and static class.  We need to reference some packages.  I prepared a model referencing below packages.   

  • ApplicationFoundation 
  • ApplicationPlatform
  • ApplicatonSuite
  • Directory
  • SourceDocumentation
  • SourceDocumentationTypes

How do you know which package we should reference?  Just notice from the model of class we want to call.  For example as below, I'm going to use SysQuery class belonging to "Application Foundation".   



Or when I want to create a static class, there are 3 packages are needed; Directory, SourceDocumentation, SourceDocumentationTypes.  Once the project needs more packages, it will show some errors when we build the project to inform you.   


Copy Event Handler method: 

I created a string control for the new field and want to override OnLookup event.  Under the form control, go to Events > OnLookup > right click > Copy event handler method.   


      


Event Handler Class: 

Create a class to project and paste code.  It will create a method with FormControlEventType::Loopup.  This lookup will be used in another form.  So, I create another extension class sending the FormControl, its EventAgrs and InvoiceAccount as parameters.  Notice the red text blow to retrieve a field value (InvoiceId) from form datasource.     

/// <summary>
/// Handles events raised by <c>CustFreeInvoice</c> form.
/// </summary>
[ExtensionOf(FormStr(CustFreeInvoice))]
final public class PKA_CustFreeInvoiceEventHandler_Extension
{
    

    /// <summary>
    /// Adds a lookup to the <c>RefInvId</c> control on <c>CustInvoiceLine</c> form.
    /// </summary>
    /// <param name="_sender">The source of the event.</param>
    /// <param name="_e">Arguments of the OnLookup event.</param>
    [FormControlEventHandler(formControlStr(CustFreeInvoice, CustInvoiceLine_RefInvId), FormControlEventType::Lookup)]
    public static void CustInvoiceLine_PKA_RefInvId_OnLookup(FormControl _sender, FormControlEventArgs _e)
    {
        FormRun                 form = _sender.formRun();
        FormDataSource          custInvoiceTable_ds = form.dataSource(formDataSourceStr(CustFreeInvoice, CustInvoiceTable)) as FormDataSource;
        CustInvoiceTable        custInvoiceTable = custInvoiceTable_ds.cursor(); 

        PKA_CustInvoiceTable_Extension::lookup_RefInvId(_sender, _e, custInvoiceTable.InvoiceAccount);
    }


}


/// <summary>
/// Method extension for <c>CustInvoiceTable</c> table.
/// </summary>
public static class PKA_CustInvoiceTable_Extension
{
    /// <summary>
    /// Adds an InvoiceId lookup from CustInvoiceJour.
    /// </summary>
    /// <param name="_sender">The source of the event.</param>
    /// <param name="_e">Arguments of the OnLookup event.</param>
    [SysClientCacheDataMethodAttribute(true)]
    public static void lookup_RefInvId(FormControl _sender, FormControlEventArgs _e, CustInvoiceAccount _invoiceAccount)
    {
        SysTableLookup sysTableLookup = SysTableLookup::newParameters(tablenum(CustInvoiceJour), _sender);
        Query query = new Query();

        sysTableLookup.addLookupfield(fieldnum(CustInvoiceJour, InvoiceId), true);
        sysTableLookup.addLookupfield(fieldnum(CustInvoiceJour, InvoiceAccount));
        sysTableLookup.addLookupfield(fieldnum(CustInvoiceJour, InvoiceDate));

        QueryBuildDataSource qbds = query.addDataSource(tablenum(CustInvoiceJour));
        QueryBuildRange qdr = qbds.addRange(fieldnum(CustInvoiceJour, InvoiceAccount));
        qdr.value(queryValue(_invoiceAccount));
        qdr = qbds.addRange(fieldnum(CustInvoiceJour, InvoiceId));
        qdr.value(sysQuery::valueNotEmptyString());
        
        sysTableLookup.parmQuery(query);

        sysTableLookup.performFormLookup();

        FormControlCancelableSuperEventArgs ce = _e as FormControlCancelableSuperEventArgs;

        //cancel super() to prevent error.
        ce.CancelSuperCall();
    }

}
  

No comments:

Post a Comment