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 trigger a error message via code when creating a new record based on below given conditions?

(0) ShareShare
ReportReport
Posted on by 45

Example 1 – User attached legal entity “HKG” to code “0002” of type “HFM”. System should not allow if user tries to attach legal entity “HKG” for same code "0002" of same type “HFM” once again. Then system should trigger error message “Legal entity HKG already attached to HFM code 0002. Please remove and try to attach.”

Example 2 – User attached legal entity “HKG” to code “0002” of type “HFM”. System should allow if user tries to attach legal entity “HKG” for same code "0002" of different type “REVAL”.

Here, 

Legal entity, Code , type are the table fields.

type have two values : HFM, REVAL.

I have the same question (0)
  • Suggested answer
    Martin Dráb Profile Picture
    237,884 Most Valuable Professional on at

    The usual approach is definining a unique index and leaving the validation to the system.

    But if you want to write your own validation (e.g. to provide a nicer error message), put the logic to validateWrite() method on the table.

  • Vaishnav Profile Picture
    45 on at

    Yes, I want to write own validation as mentioned. Can you explain how to trigger that error message using validateWrite() method .

  • Suggested answer
    Martin Dráb Profile Picture
    237,884 Most Valuable Professional on at

    Validating a record is a very common task, therefore you can find thousand examples in the application.

    For example, this is validateWrite() of LogisticsAddressCounty:

    public boolean validateWrite()
    {
        boolean ret = super();
    
        if (ret && !LogisticsAddressState::exist(this.CountryRegionId, this.StateId))
        {
            ret = checkFailed(strFmt("@SYS303352", this.StateId, this.CountryRegionId));
        }
    
        return ret;
    }

    As you see, the method returns true or false. False means that the validation failed and the record can't be saved. The first call is super(), which calls system validations. Then you check your condition and if it fails, you add a warning to infolog and set the return value to false. This is typically combined to a single call: checkFailed().

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

    I recommend defining unique index instead of solving it in validateWrite. Or then please implement both. If someone (today or in the future) creates record via x++ code, they might not remember to call validateWrite() and your validation would not be called.

    But if you added the unique index, then the system will always prevent creating such records. And in general unique indexes are the correct way of solving this requirement.

  • Vaishnav Profile Picture
    45 on at

    Yes, But the requirement is system should trigger a nicer error message.

  • Vaishnav Profile Picture
    45 on at

    How do I write the code in validationWrite() method to trigger own error message rather than system validation error ..?

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

    public boolean validateWrite()
    {
        boolean ret = super();
        
        if (ret)
        {
            if (yourCondition)
            {
                ret = checkFailed("Your error message");
            }
        }
    }

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 565 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

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

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 250 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans