Tuesday, February 26, 2019

D365FO get datasource and FormRun from FormDataObject

When creating extension of any field of form datasource, the event handler passes FormDataObject as default parameter. We then have only this to get our any reference to datasource to which it belongs and the form it belongs. Here is an example to get any form related values:


    //Here my target field is Category which is inside <c>TrvRequisitionLine</c> 
    //datasouce of form <c>TrvRequisition</c>

    [FormDataFieldEventHandler(formDataFieldStr(TrvRequisition, TrvRequisitionLine, Category),
    FormDataFieldEventType::Modified)]
    public static void Category_OnModified(FormDataObject sender, FormDataFieldEventArgs e)
    {
       
        FormDataSource              trvRequisitionLine_DS;
        TrvRequisitionTable         requisitionTable;
        TrvRequisitionLine          requisitionLine;
        ormReferenceGroupControl    formControlLocationDetail;
        //Get datasouce
        trvRequisitionLine_DS = sender.datasource();

        //Get the formRun
        formRun = TrvRequisitionLine_DS.formRun();

        //Get currently selected record
        requisitionLine = trvRequisitionLine_DS.cursor();

        //Get any other datasouce you want form form with help of relation
        requisitionTable = TrvRequisitionTable::find(requisitionLine.TrvRequisitionTable);
       
        //Set any value of record field value based on your calculation
        requisitionLine.AccountingCurrencyAmount = 0;//TODO::Some calculations you need to do and set value of amount

        //Change state of any control on form
        formControlLocationDetail = 
        formRun.design().controlName(formControlStr(TrvRequisition,PerdiemTrvLocations_Details));

        formControlLocationDetail.mandatory(false);
        formControlLocationDetail.enabled(false);

    }

No comments:

Post a Comment