Wednesday, 12 December 2018

D365 OnLookup form method event handler

[FormControlEventHandler(formControlStr(PurchAgreement, PurchAgreementHeader_ParentPurchAgreement), FormControlEventType::Lookup)]
     public static void PurchAgreementHeader_ParentPurchAgreement_OnLookup(FormControl sender, FormControlEventArgs e)
     {
  
        SysTableLookup          sysTableLookup = SysTableLookup::newParameters(tablenum(PurchAgreementHeader), sender);
        FormRun                 element = sender.formRun();
        FormDataSource          purchAgreementHeader_ds;
        Query                   q   = new Query();
        QueryBuildDataSource    qbds;
        QueryBuildRange         qbr;
        PurchAgreementHeader    parentPurchAgreementHeader;

        purchAgreementHeader_ds = element.dataSource(formDataSourceStr(PurchAgreement, PurchAgreementHeader)) as FormDataSource;

        parentPurchAgreementHeader = purchAgreementHeader_ds.cursor();
        qbds   = q.addDataSource(tableNum(PurchAgreementHeader));
        qbr    = qbds.addRange(fieldNum(PurchAgreementHeader,VendAccount));
        qbr.value(parentPurchAgreementHeader.VendAccount);
        sysTableLookup.addLookupfield(fieldnum(PurchAgreementHeader, PurchNumberSequence), true);
        sysTableLookup.parmQuery(q);
        sysTableLookup.performFormLookup();

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

Post handler for overriding table methods modified field and validate Write

[PostHandlerFor(tableStr(InventTable), tableMethodStr(InventTable, modifiedField))]
public static void InventTable_Post_modifiedField(XppPrePostArgs args)
{
        //Getting the table buffer
        InventTable inventTable = args.getThis() as InventTable
       //Getting the field id method argument.
        FieldId fieldModified = args.getArg("_fieldId");
        switch (fieldModified)
        {
            //Here you can write your logic on modified field method
                break;
        }
}

D365 Post handler for overriding table methods modified field and validate Write

[PostHandlerFor(tableStr(InventTable), tableMethodStr(InventTable, validateWrite))]
public static void InventTable_Post_validateWrite(XppPrePostArgs args)
{
      InventTable inventTable = args.getThis() as InventTable
      boolean ret = true;
      // Override the validations here and set the return value accordingly.
       Args.setReturnValue(ret);
}

D365 Class method event handler

[PostHandlerFor(classStr(EcoResProductReleaseManager), methodStr(EcoResProductReleaseManager, release))]
public static void EcoResProductReleaseManager_Post_release(XppPrePostArgs args){
     EcoResProductReleaseManager releaseMgr;
    //Getting the class object
    releaseMgr     = args.getThis();
   //Getting the class parameter
   ItemId itemId  = releaseMgr.parmItemId();
   //Getting the method argument
    boolean itemCreation = args.getArg("_isCreation");
}

D365 Form Close method event handler

[FormEventHandler(formStr(EcoResAttributeValue), FormEventType::Closing)]
public static void EcoResAttributeValue_OnClosing(xFormRun sender, FormEventArgs e)
{
     FormDataSource ecoResProduct_ds   =          sender.dataSource(formDataSourceStr(EcoResAttributeValue, EcoResProductAttributeValue));
      EcoResProductAttributeValue      ecoResAttributeValue = ecoResProduct_ds.cursor();
}   

D365 Form data source event handler

[FormDataSourceEventHandler(formDataSourceStr(EcoResProductDetailsExtended, InventTable), FormDataSourceEventType::Written)]
public static void InventTable_OnWritten(FormDataSource sender, FormDataSourceEventArgs e)
{
    FormRun                 form           = sender.formRun();
    FormDataSource          InventTable_ds =       form.dataSource(formDataSourceStr(EcoResProductDetailsExtended,InventTable)) as FormDataSource;
   InventTable             inventTable    = InventTable_ds.cursor();
}

D365 Form button Control event handler

[FormControlEventHandler(formControlStr(HcmWorker, ArgsExampleButton), FormControlEventType::Clicked)]
    public static void ArgsExampleButton_OnClicked(FormControl sender, FormControlEventArgs e)
    {
        Args args = new Args();
        Form formToCall = new Form('ArgsDemo_Callee');
        FormButtonControl callerButton = sender as FormButtonControl;  //Retrieves the button that we're reacting to
        FormRun form = callerButton.formRun(); //Gets the running HcmWorker form
        //Get the HcmWorker that was selected in the HcmWorker form
        FormDataSource hcmWorker_ds = form.dataSource(formDataSourceStr(HcmWorker, HcmWorker)) as FormDataSource;
        HcmWorker hcmWorker = hcmWorker_ds.cursor();
        //Set up args with all of the information you've retrieved                        
        args.caller(form);
        args.object(formToCall );
        args.record(hcmWorker);
        form = ClassFactory.formRunClass(args);
        form.init();
        form.run();
        form.wait();
    }