Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics AX forum
Answered

Validation on the fields

Posted on by 543

Hi Guys,

I have a form & table called CommissionTrans  where I have the dropdown field  settlementId. I need to select the SettlementId for each transactions manually on this form . I have to add a validation that I should be able to select one settlementId for one transaction only. If I select the same ID for next transaction it should show an error that its already used for another transaction. how can I do it ? which validation method to be added and how the code should be. 

  • Verified answer
    Martin Dráb Profile Picture
    Martin Dráb 228,112 Most Valuable Professional on at
    RE: Validation on the fields

    Don't use a regular button with clicked(). Use a command button with Command = OK and override closeOk(). The validation can be done in canClose(). Nevertheless removing all the code and using a lookup form would likely be a better approach. Then you would use validateWrite() and or validateField() as discussed before.

    I don't know what's the purpose the while select + delete; it looks suspicious. And the fact there is no condition must be a bug.

    If you really need some deletion, remember never put any code like this on client. It meaninglessly fetches all CommissionTransSettlement records from database to client just to send delete commands (one by one) back to database. It's a performance killer. It's made even worse by the long-living transaction created on client. *If* you need this logic, put it in a server-side method and replace the while select with delete_from.

  • D365  beginner Profile Picture
    D365 beginner 543 on at
    RE: Validation on the fields

    Hi Martin,

    Thanks for the reply.

    I Just made some changes in the requirement as I created a new child form (CommissionTransSettlement) and I would like to select the settlement Id from the child form. In this case I am trying to modify the clicked method already there on the button from where I can select the SettlementId.  how can I call the function validatefield() created on the table commissiontrans on this form.

    void clicked()

    {

     commissionTrans CommissionTrans;

      super();

     ttsbegin;

     while select forUpdate CommissionTransSettlement

     {

     CommissionTransSettlement.Delete();

     }

     ttscommit;

     ttsbegin;

     select forUpdate CommissionTrans

                   where CommissionTrans.InvoiceId == CommissionTransSettlement.InvoiceId

                      &&  CommissionTrans.EmplId == CommissionTransSettlement.EmplId  ;

     CommissionTrans.SettlementId = CommissionTransSettlement.SettlementId;

     CommissionTrans.update();

      ttscommit;

     element.close();

    }

  • Suggested answer
    Martin Dráb Profile Picture
    Martin Dráb 228,112 Most Valuable Professional on at
    RE: Validation on the fields

    Which method you should use depends on when you want to run the validation. For example, if you want to do it on save, override validateWrite() method on the table.

    Simply a run a query to check whether there already is another transaction with the same settlement ID and return false if it is. You'll likely want to create an index on SettlementId, to make this query efficient.

Helpful resources

Quick Links

Dynamics 365 Community Update – Sep 9th

Welcome to the next edition of the Community Platform Update. This is a weekly…

Announcing Our 2024 Season 2 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,263 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 228,112 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans