Wednesday, May 31, 2023

D365 F&O// X++: Adding zero numbers into Left/Right text values

 

Sometimes we need to add padding numbers in the files in AX. Ex: Postive pay files
I found the below post that helped to add the padding zero numbers in the positive pay file.

Original post: http://www.atomicax.com/content/zero-padding-numbers

X++ provides us with an easy way to pad strings with extra characters. A common situation where this is required is use a "fake" number sequence inside AX.
If you wish to increment a counter, but store or display it as a string such as "ABC-00001", "ABC-00002" etc then padding the counter with zeros is required.
We can use the strRFix() and strLFix() methods to achieve this

static void testZeroPadding(Args _args)
{
    int i = 1;
    str padded;
    str finalResult;
    ;
    // Create a string, with a length of 5, ending with the value of i and padded with zeros
    padded = strRFix(int2str(i), 5, "0");
    finalResult = strFmt("ABC-%1", padded);

}

Result will be displayed ABC-00001. If we use strLFix instead of strRFix, it will pad to the right with zeros, giving us ABC-10000

Another way, also common thoughts, the approach is below sample

static str zeroPad(str item, int numZero)
{
    int i;
    str out;
    ;
    for(i=0;i<numZero-strlen(item); i++)
    {
        out += "0";
    }

    return(out + item);
}

 

Tuesday, May 30, 2023

D365 F&O: Event Handler on Form


On DataSource on form CustTable

class CustTableFormEventHandler
{
    [FormDataSourceEventHandler(formDataSourceStr(CustTable, CustTable), FormDataSourceEventType::Activated)]
    public static void CustTable_OnActivated(FormDataSource sender, FormDataSourceEventArgs e)
    {
        CustTable                   custTable     = sender.cursor();
        FormDataSource      custTable_ds  = sender.formRun().dataSource("CustTable");
        FormRun                    element       = sender.formRun() as FormRun;
        FormControl              myNewButton   = element.design(0).controlName("MyNewButton");

        myNewButton.enabled(false);
    }

}


On method on form SalesEditLines

-- Method closeOk

[PostHandlerFor(formStr(SalesEditLines), formMethodStr(SalesEditLines, closeOk))]

    public static void SalesEditLines_Post_closeOk(XppPrePostArgs args)

    {

        FormRun sender = args.getThis();

        SalesParmTable salesParmTable;

        SalesTable salesTable;

        SalesDocumentStatus docStatus;

        ;

        salesParmTable = sender.dataSource(formdatasourcestr(SalesEditLines, SalesParmTable)).cursor();

        salesTable = SalesTable::find(salesParmTable.SalesId);

        docStatus = salesParmTable.documentStatus();

        if(docStatus != DocumentStatus::PackingSlip)

        {

            return;

        }

//Som source codes in here

    }

-- Event Closing

[FormEventHandler(formStr(SalesEditLines), FormEventType::Closing)]

    public static void SalesEditLines_OnClosing(xFormRun sender, FormEventArgs e)

    {

        FormDataSource datasource_ds = sender.dataSource(formDataSourceStr(SalesEditLines, SalesLines));

        SalesLine buffTable = datasource_ds.cursor();

    }

***

[FormEventHandler(formStr(TSExtensionInstanceTest), FormEventType::Initialized)]

    public static void TSExtensionInstanceTest_OnInitialized(xFormRun sender, FormEventArgs e)

    {

        FormRun formRun = sender as FormRun;

        TSExtensionInstanceTestHandler extensionInstance = formRun.getExtensionInstance(classStr(TSExtensionInstanceTestHandler));

        extensionInstance.init();

    }

On method Modified of field in datasource TrvRequisitionLine on form

[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);

    }


On method Clicked of button on form

[FormControlEventHandler(formControlStr(TSExtensionInstanceTest, Counter), FormControlEventType::Clicked)]

    public static void Toggle_OnClicked(FormControl sender, FormControlEventArgs e)

    {

        FormRun                    element       = sender.formRun() as FormRun;
        FormControl              myNewButton   = element.design(0).controlName("MyNewButton");

        myNewButton.enabled(false);

    }


D365 F&O: Event handler- Retrieve some values from data source in form

 

D365 Finance and Operations: How to get the value datasource of form in an Event handler in Dynamics 365 Finance & Operations X++ method SalesEditLines_Post_closeOk

In this example I'll be showing you how to get a data-source in the event-handler for form method 'closeok'. In this example I'm using post event.

You can change the code for other form methods in the same way.


class SalesEditLines_EventHandler

{

    [PostHandlerFor(formStr(SalesEditLines), formMethodStr(SalesEditLines, closeOk))]

    public static void SalesEditLines_Post_closeOk(XppPrePostArgs args)

    {

        FormRun sender = args.getThis();

        SalesParmUpdate salesParmUpdate;

        salesParmUpdate = sender.dataSource(formdatasourcestr(SalesEditLines, SalesParmUpdate)).cursor();

        info(salesParmUpdate.ParmId);

    }

}

D365 FinOps - How call a runnable class with URL parameters

We are already familiar with calling D365FO URL menu items or forms or Runnable class. To recap, I am giving few samples below To call  Disp...