AX / D365FO – Calling Datasource modified() method programatically

This example shows hot to change Form datasource field value and call its modified() methods throught X++ code

    [FormControlEventHandler(formControlStr(CustOpenTrans, MarkTrans), FormControlEventType::Clicked)]
    public static void MarkTrans_OnClicked(FormControl sender, FormControlEventArgs e)
    {

        FormCheckBoxControl     callerButton        = sender as FormCheckBoxControl;
        FormRun                 form                = callerButton.formRun();
        FormDataSource          taxWithholdTrans_ds = form.dataSource(formDataSourceStr(TaxWithholdTrans, TaxWithholdTrans)) as FormDataSource;
        FormDataSource          custTrans_ds = form.dataSource(formDataSourceStr(CustTrans, CustTrans)) as FormDataSource;
        ProjInvoiceJour         projInvoiceJour;
        TaxWithholdTrans        taxWithholdTrans;
        CustTrans custTrans = custTrans_ds.cursor();
        
        
        select firstonly projInvoiceJour
            where projInvoiceJour.ProjInvoiceId == custTrans.Invoice;

        if(projInvoiceJour)
        {
            if(projInvoiceJour.Al0TaxWithholdAmount != 0 && projInvoiceJour.Al0TaxWithholdCode != '')
            {
                taxWithholdTrans_ds.create();
                taxWithholdTrans = taxWithholdTrans_ds.cursor();
                taxWithholdTrans.TaxWithholdCode = projInvoiceJour.Al0TaxWithholdCode;    //Change value of the data source field
                taxWithholdTrans_ds.object(fieldnum(taxWithholdTrans, TaxWithholdCode)).modified();        //call modified method of the changed field
                
                taxWithholdTrans_ds.reread();
                taxWithholdTrans_ds.refresh();

                //taxWithholdTrans = taxWithholdTrans_ds.cursor();
                //taxWithholdTrans.TaxWithholdCode = projInvoiceJour.Al0TaxWithholdCode;
                Info(strFmt('Tax Withhold value : %1, Tax Withhold code : %2', projInvoiceJour.Al0TaxWithholdAmount, projInvoiceJour.Al0TaxWithholdCode));
            }
        }
    }
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s