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

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

ValidateWrite/CanClose method on a form

(1) ShareShare
ReportReport
Posted on by

Hi, 

Pertains to the Item Details form - General Tab - Default Configuration field. 

I want to throw an error when the Default Configuration field value is not populated correctly. 
I tried to used ValidateWrite on the Form/Field level - but still it only "hits" the breakpoint when the lookup is called - which makes sense. 
And when I used CanClose() method at the form level - it works fine to an extent - it throws the error when I try to save the form as expected - which is good. But the problem is that it clears all the other auto-populated fields when I close the error(info) - which confuses the users - as why would an error on a particular field clears up all other fields? Funny thing is that the other field values come back when the Item Details form is opened again. But still not a user - friendly option. 

What can I do to throw an error when the field is populated incorrectly (and the user tries to save the form)  - without clearing/messing up other fields? 

Please help. 

*This post is locked for comments

I have the same question (0)
  • Martin Dráb Profile Picture
    239,082 Most Valuable Professional on at

    If you want to validate a single field as soon as it's set, override validateField() on table or validate() on form data source field. If you want to run validation before saving a record, override validateWrite() on table or datasource.

  • Community Member Profile Picture
    on at

    What if the field value if pre-populated(probably as soon as the new record is created) ? And is not actually "set" ? I don't believe that validate etc. would be hit - if the field is not touched...

    Any ideas?

  • Suggested answer
    Martin Dráb Profile Picture
    239,082 Most Valuable Professional on at

    Validation is used for input from users, not for what the application itself filled in. If you're putting invalid values to the form, fix the code.

  • Suggested answer
    Niels Kloster Profile Picture
    35 on at

    I had a similar issue where I needed to stop the form from closing if a validation failed. I placed a return false before the super in CanClose-method to keep the form as it is in case of errors.

  • gsbsrk b Profile Picture
    692 on at

    In canclose() method writer table_ds.research(true) below ret = false line

    Thanks,

    Krishna

  • TestBot Profile Picture
    958 on at

    Khosla,

    i had simillar requirement like you but my form(smmopportunitytable) doesn't have canclose method. i had written in form onclosing eventhandler. but the logic is hitting outside of details page i.e. in listpage.

    second i had tried wriitin in formdatasource validatingwrite eventhandler using formdatasourceeventargs e "return error msg via boolean using checkfailed"., this is triggering while saving the record . but after saving, the records are cleared when i reopen the form its showing. how to resolve the clearing up other fields?

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

    If you return false in validateWrite, the record is not written to the table.

    ValidateField is the correct place for field validation for user inputs. But if that fails, the record is not saved, either.

    The purpose of these validations is to prevent users from inserting bad data into the system.

  • TestBot Profile Picture
    958 on at


    Nikolaos,

    Validatefield is correct but i need to check whether a record exists or not in that particular grid while closing/.saving. if not i should throw the error.

  • nmaenpaa Profile Picture
    101,166 Moderator on at

    What record are you talking about? The same record that you are validating?

    Could you describe the requirement in more detail please.

  • TestBot Profile Picture
    958 on at

    In opportunity form,  whenever user creates new opportunity, he should enter  business address and contact info of phone type mandatorily . if not while closing/saving the opportunity . i should throw errror like " user should enter business address" . so my approach is while closing whether a record of type business address and phone contact exists or not . If not user should not close the form

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the March Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
Joris dG Profile Picture

Joris dG 5

#2
Andrew Jones a1x Profile Picture

Andrew Jones a1x 2

#3
Basit Profile Picture

Basit 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans