Posted in AX 365

D365 bits and Pieces for daily use

Example to Get args() in Chain of command method

public void modifyWHSInventReserveAvailRange(boolean _useQueryRunQueryIfExists)
{
   next modifyWHSInventReserveAvailRange(_useQueryRunQueryIfExists); 
   Args args = this.args();

   if( args.record()
      && ( args.record().TableId == tableNum(SalesLine))           
   {
     //Logic here
    }
}

Example to get data source objects in Form Control event handler methods

 [FormControlEventHandler(formControlStr(WHSInventOnHandReserve, DXC_RemoveZeroQty), FormControlEventType::Clicked)]
public static void DXC_RemoveZeroQty_OnClicked(FormControl sender, FormControlEventArgs e)
{
   // Accessing DataSource
  sender.formRun().dataSource(formDataSourceStr(WHSInventOnHandReserve, WHSInventReserve)).research(true);
}

Example to get Form control in Form control event handler

 [Hookable(false), FormEventHandler(formStr(CustCollectionsPoolsListPage), FormEventType::Initialized)]
public static void CredManCustCollectionsPoolsListPage_OnInitialized(xFormRun _sender, FormEventArgs _e)
{
    if (CredManFeatureManagementVisibilityManager::isEnabled())
    {
      FormRun custCollectionsListPage = _sender as FormRun;

      FormButtonControl credManLinkedCustomerGroupButtonControl = custCollectionsListPage.design().controlName(formControlStr(CustCollectionsPoolsListPage, CredManLinkedCustomerGroup)) as FormButtonControl;
      if (credManLinkedCustomerGroupButtonControl)
      {
        credManLinkedCustomerGroupButtonControl.visible(true);
      }

      FormStringControl credManStringControl = custCollectionsListPage.design().controlName(formControlStr(CustCollectionsPoolsListPage, CredManCustTableCreditLimitTableView_CreditLimitId)) as FormStringControl;
       if (credManStringControl)
       {
          credManStringControl.visible(true);
        }

    FormRealControl credManBalanceRealControl = custCollectionsListPage.design().controlName(formControlStr(CustCollectionsPoolsListPage, CustTable_EffectiveCreditMax)) as FormRealControl;
         if (credManBalanceRealControl)
         {
            credManBalanceRealControl.visible(true);
         }
     }
}

Example to get args and form run objects in Post event handler methods

[PostHandlerFor(formStr(WHSInventOnHandReserve), formMethodStr(WHSInventOnHandReserve, init))]
public static void WHSInventOnHandReserve_Post_init(XppPrePostArgs args)
    {
        FormRun sender = Args.getThis();
        FormCheckBoxControl myCheckBox;

        if ((sender.args().record() && sender.args().record().TableId == tableNum(SalesLine))
            && SalesParameters::find().DXC_ShowDimReserrvation)
        {
            sender.control(sender.controlId(formControlStr(WHSInventOnHandReserve, DXC_RemoveZeroQty ))).Visible (true);
            myCheckBox =  sender.control(sender.controlId(formControlStr(WHSInventOnHandReserve, DXC_RemoveZeroQty )));
            myCheckBox.value(NoYes::Yes);
        }
    }

Add Range on a form data source at runtime using OnQueryExecuting DataSource

 [FormDataSourceEventHandler(formDataSourceStr(VendTableLookup, VendTable), FormDataSourceEventType::QueryExecuting)]
    public static void VendTable_OnQueryExecuting(FormDataSource sender, FormDataSourceEventArgs e)
    {
        FormRun callerForm = sender.formRun().args().caller();

        if (callerForm.name() == formStr(PurchCreateOrder) && VendParameters::find().RestrictVendorSelection_INT)
        {
            sender.query().dataSourceName(sender.name()).addRange(fieldnum(VendTable, PORequired_INT)).value(queryValue(NoYes::Yes));
        }
    }

Overwrite Original lookup and call Custom Lookup

[FormControlEventHandler(formControlStr(PurchReqTable, PurchReqLine_VendAccount), FormControlEventType::Lookup)]
    public static void PurchReqLine_VendAccount_OnLookup(FormControl sender, FormControlEventArgs e)
    {
        FormControlCancelableSuperEventArgs event;
        PurchReqLine        purchReqLine;

        event = e as FormControlCancelableSuperEventArgs;
        purchReqLine = sender.formRun().dataSource(formdatasourcestr(PurchReqTable, PurchReqLine)).cursor() as PurchReqLine;

        PurchReqLine::lookupVendAccount(sender, purchReqLine); //orignal logic for vendor lookup 
        event.CancelSuperCall();
    }