Skip to main content

Notifications

Announcements

No record found.

Dynamics 365 Community / Forums / Finance forum / Modifying extended for...
Finance forum
Suggested answer

Modifying extended form datasource method

Posted on by 1,400

Hi guys, I've encountered the following situation: 

I have to modify a datasource's validateWrite method on an extended form, to which I have to add some logic before and after it executes the parent validation that modifies the return value on the validateWrite method.

How can I approach that using event handlers (if that's possible)? More specifically, how can I modify its return value and add code before and after the super is called?

I'm currently developing on Update 7.

Thanks!

  • Suggested answer
    RE: Modifying extended form datasource method - D365FO

    See this link : http://dax365.blogspot.com/2017/06/ax-7-validating-events-on-form-data.html

    You can cancel the return event :

    [FormDataSourceEventHandler(formDataSourceStr(MyForm, abTable), FormDataSourceEventType::ValidatingWrite)]
    public static void abTable_OnValidatingWrite(FormDataSource sender, FormDataSourceEventArgs e)
    {
        var datasource = sender as FormDataSource;
        var args = e as FormDataSourceCancelEventArgs;
        if (args != null && datasource != null)
        {
            var record = datasource.cursor() as abTable;
            if (record.recId == 0)
            {
                if (record.FieldInt1 == 1)
                {
                    boolean doCancel = !checkFailed("Value 1 is not allowed");
                    args.cancel(doCancel);
                }
            }
        }
    }
  • swetha desai Profile Picture
    swetha desai 152 on at
    RE: Modifying extended form datasource method - D365FO

    I have the same question, any answer for this yet?

    Thanks,
    SWetha K Desai

  • greengrimms Profile Picture
    greengrimms 1,400 on at
    RE: Modifying extended form datasource method - D365FO

    Ok, but how do I tell the "parent" validateWrite that the validation on the event handler was successful or not without passing it the returned value?

  • Sukrut Parab Profile Picture
    Sukrut Parab 71,643 Moderator on at
    RE: Modifying extended form datasource method - D365FO

    By looking at your requirement , I don't feel you have to return any value , you can just perform your validation in Pre or post event handler and if validation is ok use that value further.

  • greengrimms Profile Picture
    greengrimms 1,400 on at
    RE: Modifying extended form datasource method - D365FO

    Well, for example, I have extended the SalesEditLines form and added a datasource to it. On this new datasource I have to override the validateWrite, because I need to use a variable on the form level (documentStatus) to check if it's an invoice or a packing slip and perform a different validation based on that value... so it's crucial that the validateWrite is on the datasource and not on the table in this particular case.

    That's one of the many cases I'm facing, each of them different.

    Do you think it is achievable to modify the validateWrite on the datasource in the same way as the example you provided?

  • Sukrut Parab Profile Picture
    Sukrut Parab 71,643 Moderator on at
    RE: Modifying extended form datasource method - D365FO

    I am not sure about your requirement, Is there any reason you are not writing this on table EventHandler and change value there and return it. May be you can specify your form name and what you want to achieve so that we get clear Requirement.

  • greengrimms Profile Picture
    greengrimms 1,400 on at
    RE: Modifying extended form datasource method - D365FO

    Thank you for the help Sukrut. Sorry for the delay, I didn't read the notification of your message until today.

    The example you provided is for the validateWrite on the table, am I right? How can I make it work for the validateWrite on the datasource of a form? Because the event handler has different parameters, and none of those objects has the parmValidateResult method to modify the original return value:

    /// <summary>

    /// Event handler for datasource validateWrite

    /// </summary>

    /// <param name="sender"></param>

    /// <param name="e"></param>

    [FormDataSourceEventHandler(formDataSourceStr(LedgerJournalTable, LedgerJournalTable), FormDataSourceEventType::ValidatedWrite)]

    public static void LedgerJournalTable_OnValidatedWrite(FormDataSource sender, FormDataSourceEventArgs e)

    {      

    }

    Again, thank you.

  • Suggested answer
    Sukrut Parab Profile Picture
    Sukrut Parab 71,643 Moderator on at
    RE: Modifying extended form datasource method - D365FO

    You just have to copy those events and implement in your eventHandler class. Here is example you can return value through DateEventArgs variable

    [DataEventHandler(tableStr(AnyTable), DataEventType::ValidatedWrite)]

    public static void InventLocation_onValidatedWrite(Common sender, DataEventArgs e)

    {

       // convert Common to AnyTable

       AnyTable anyTable = sender;

       // the DataEventArgs actually are ValidateEventArgs and can be converted

       ValidateEventArgs validateEventArgs = e;

       // the ValidateEventArgs carry the validation result (so far)

       boolean ret = validateEventArgs.parmValidateResult();

       // the table has some additional validation logic and gives back the result

       ret = anyTable.doSomeAdditionalCustomValidation(ret);

       // provide the args with the validation result

       validateEventArgs.parmValidateResult(ret);

    }

  • greengrimms Profile Picture
    greengrimms 1,400 on at
    RE: Modifying extended form datasource method - D365FO

    Hi Sukrut, thank you for answering. How could I modify the returned value in that case? I've been using the approach on the link you provided, but only for fields on the datasource... and that brings me to the next question; how could I override a datasource method by code?

  • Suggested answer
    Sukrut Parab Profile Picture
    Sukrut Parab 71,643 Moderator on at
    RE: Modifying extended form datasource method - D365FO

    To  execute logic before super you can write your logic on OnValidatingWrite and after super you can write on OnValidatedWrite event for the form datasource. You can see examples in this link

Helpful resources

Quick Links

Replay now available! Dynamics 365 Community Call (CRM Edition)

Catch up on the first D365 Community Call held on 7/10

Community Spotlight of the Month

Kudos to Saurav Dhyani!

Congratulations to the June Top 10 community leaders!

These stars go above and beyond . . .

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 287,914 Super User

#2
Martin Dráb Profile Picture

Martin Dráb 225,549 Super User

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans