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 :
Finance | Project Operations, Human Resources, ...
Suggested Answer

validations on insert and update methods of table OR on validate write method of table

(0) ShareShare
ReportReport
Posted on by 1,552

Hi,

I have a table with three NoYes fields.

for each record, only one of those fields can be yes.

Where is the best place to put this validation... Do i need to put this condition on both Insert and update of this table? or validate write.. what is the best way?

        if(this.Field1 == NoYes::Yes
            && (this.Field2 == NoYes::Yes
                || this.Field3 == NoYes::Yes)
            ||
            this.Field2 == NoYes::Yes
            && (this.Field1 == NoYes::Yes
                || this.Filed3 == NoYes::Yes)
            ||
            this.Field3 == NoYes::Yes
            && (this.Field1 == NoYes::Yes
                || this.Field2 == NoYes::Yes)
          )
          {
          throw error ("only one field can be true");
          }



  • Suggested answer
    GirishS Profile Picture
    27,845 Moderator on at

    Hi,

    I think validateWrite method at table level is good solution, instead of insert and update.

    Thanks,

    Girish S.

  • Suggested answer
    Mohit Rampal Profile Picture
    12,573 Moderator on at

    Hi, ValidateWrite will be correct method to use here.

    Check this out for more understanding on how (Not) to use this method

    community.dynamics.com/.../how-not-to-use-validatewrite-and-validatedelete

  • Martin Dráb Profile Picture
    241,637 Most Valuable Professional on at

    By the way, your design looks wrong to me. You should use an enum instead of three checkboxes. Then a user can always select just one value and you don't need code ensuring that and you don't need extra fields in database.

    If you really must use checkboxes, I would use code like this:

     

     

    boolean exactlyOneChecked = this.Field1 ^ this.Field2 ^ this.Field3;
    
    if(!exactlyOneChecked)
    {
        throw error ("only one field can be true");
    }

     

  • junior AX Profile Picture
    1,552 on at

    Hi Martin,

    What does ^ mean?

    Also would u put this logic in validate write? Or insert and update method?

  • Martin Dráb Profile Picture
    241,637 Most Valuable Professional on at

    It's an operator, therefore the place where you should look for the answer is X++ operators page of F&O documentation.

    This is an "exclusive OR" operator, often called "XOR".

    The fact that the method intended for validations is called validateWrite() method was already discussed above. Also note that what you should do always depends on what exactly you want to achieve. There may be a situation when you want to have an extra check on writing, in case that some code fail to call validateWrite().

  • junior AX Profile Picture
    1,552 on at

    Hi Martin,

    What would case validateWrite not to be called?

    what i want is that i don't want the record to be inserted or updated in case the more than one checkbox is ticked to true. In this case should i use validate write or writing?  (or would handling it on insert and update be more accurate)?

  • Martin Dráb Profile Picture
    241,637 Most Valuable Professional on at

    Okay, let me put in a simpler way: Use validateWrite().

    validateWrite() is called automatically by forms and data entities, but it needs to be called explicitly (and its return value taken into account) when data is manipulated by code.

  • junior AX Profile Picture
    1,552 on at

     Hi Martin,

    XOR Is not working because currently it allows the three fields to be set as true.

    Only one field can be true from the three fields.

  • Martin Dráb Profile Picture
    241,637 Most Valuable Professional on at

    XOR is exclusive OR. It returns 0 if both operands are 1. It's unlikely that you've found a bug in a basic X++ operator, therefore the problem must be in something else than that "XOR Is not working". But OK, let's not waste time with it; do it as you like. Otherwise the solution would be debugging code, instead of just saying that it doesn't work.

  • junior AX Profile Picture
    1,552 on at

    HI Martin,

    there is nothing extra in the code, it allows the three fields to be true (it 2 are true, then it doesn't allow me as expected but if three are ticked it allows me)

        public boolean validateWrite()
        {
            boolean ret;
    
            ret =  next validateWrite();
    
            if(ret)
            {
                NoYesId exactlyOneChecked = this.Field1 ^ this.Field2 ^ this.Field3;
                if(!exactlyOneChecked)
                {
                    ret = checkFailed("only one can  be ticked");
                }
            }
    
            return ret;
        }

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

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

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

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 274 Super User 2026 Season 2

#2
Aayush Tiwari Profile Picture

Aayush Tiwari 213

#3
Subra Profile Picture

Subra 202 Super User 2026 Season 2

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans