web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Suggested Answer

How to extend a form to add validation to a OnClicked of button and if it fails to throw exception and not open buttons form

(0) ShareShare
ReportReport
Posted on by 235

Hi

I have a new field ReasonCode which is added to the SalesLine.  If the salesQty (CreditNote) is less than ) this field is mandatory.

I have added the code to the SalesTable form Salesline_ds on the validate_write, but this will then only execute if the salesline record is modified.  I can ot add it to the Tables validate write, because the salesline gets created from different places and I do not want to have to add this field on all the table from where a salesline is created.

So I was thinking of adding the validation on the onClicked of the ConfirmButton.  Hoping that if it fails then I can throw an exception and not allow the confirmation form to open.

However it first opens the Form and then runs through my code.  How can I prevent this form from opening by forcing that the validation I added on the OnClicked method be done before opening the form.

Oh and I have the same validation on the Return order

Any advice will be appreciated.  See my code

    [FormControlEventHandler(formControlStr(SalesTable, buttonUpdateConfirmation), FormControlEventType::Clicked)]
    public static void buttonUpdateConfirmation_OnClicked(FormControl sender, FormControlEventArgs e)
    {
        SalesTable      salesTable;
        SalesLine       salesLine;
        FormRun         formRun = sender.formRun() as FormRun;
        FormDataSource  salesTable_ds;

        salesTable_ds = sender.formRun().dataSource(1);
        salesTable = salesTable_ds.cursor();

        select SalesId, ItemId, RecId from salesLine
            where salesline.salesid == salesTable.salesId
            && salesLine.SalesQty < 0
            && salesline.PBFReturnReasonCodeId == '';
        if(salesLine.RecId)
        {
            throw error('Return reason code is mandatory for line with item :'   salesLine.ItemId);
        }

    }

I have the same question (0)
  • Suggested answer
    nmaenpaa Profile Picture
    101,160 Moderator on at

    onClicked event is executed after the clicked method.

    I recommend enabling/disabling the confirmation button based on your validation. This validation logic should be triggered in active method of SalesTable data source.

  • Suggested answer
    Vishal Dhavgaye Profile Picture
    405 on at

    Did you explore the possibility of having this validation in 'salesFormletter_conform' before opening form.

  • Sophia.r Profile Picture
    235 on at

    Hi Vishal, thank you for your reply

    I am struggling with this bit, how to add the code and be able to stop the process if all the lines do not have the new field populated.

    Do you have example of code that I can look at?

  • Anton Venter Profile Picture
    20,345 Super User 2025 Season 2 on at

    Hi Sophia,

    Why don't you validate this on the validateWrite of the SalesLine table? If you add your validation to the validateWrite of the SalesLine, the sales line records will never be saved if the SalesQty is less than 0 and if the reason code is empty.

    And you can even add some code to the form to indicate that the reason code is mandatory if the SalesQty is < 0 so users can see that up front.

    If you extend the validateWrite, you will enforce the business logic from the user interface and also on database level. So if an integration adds sales lines, this business logic will also be triggered. That's why it is best practice to enforce such logic on table level.

  • Suggested answer
    Sergei Minozhenko Profile Picture
    23,093 on at

    Hi Sophia.r,

    There are a couple of standard methods in class SalesTableType: mayConfirmationBeUpdate and canConfirmationBeUpdated which are used in many places to handle validation logic for confirmations. I suggest you create CoC method for one of these methods and add needed logic, instead of using clicked method.

  • Sophia.r Profile Picture
    235 on at

    Hi Anton.  We started by adding the code to te validate Write.  Unfortunately when you create a Credit note, it creates te SalesLine but from a different form where we do not have access to the new field.  Then the saleslines are created in the background. So you can not write due to the validation on the validateWrite, but you cant add the field.

  • Sophia.r Profile Picture
    235 on at

    Hi Sergei.  Thank  you so much.   I was able to solve my problem by adding a validation method to Salestable  to verify if the saleslines passed.  And then I extended Coc the MayConfirmationBeUpdated.  It is not the ideal solution as every time they change record these validations are called which can slow the system down.  But it works.  Thank you.

  • Suggested answer
    nmaenpaa Profile Picture
    101,160 Moderator on at

    Actually it makes sense to run some validations whenever the record is changed, in order to enable/disable buttons on the form. This is much more user friendly than throwing errors after the button was clicked.

    And since this pattern is already used in the system by Microsoft, it makes sense that you hook your code in this same check.

  • Sergei Minozhenko Profile Picture
    23,093 on at

    Hi Sophia.r,

    In order to minimize performance impact use "firstonly" keyword and remove unused fields from select statement

    select firstonly RecId from salesLine
        where salesline.salesid == salesTable.salesId
        && salesLine.SalesQty < 0
        && salesline.PBFReturnReasonCodeId == '';

  • Sophia.r Profile Picture
    235 on at

    Thank you for your response.  This same requirement is needed for a Return Order.  Can you propose a place to add the code as the only place that the field has been added is on the Return Order form (Return Table).  I was thinking if I can prevent the Generate > Post Packing slip from becoming available.  Please advise.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Martin Dráb Profile Picture

Martin Dráb 664 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 522 Super User 2025 Season 2

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 303 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans