Skip to main content

Notifications

Announcements

No record found.

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?

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.

  • Suggested answer
    nmaenpaa Profile Picture
    nmaenpaa 101,156 on at
    RE: How to trigger a error message via code when creating a new record based on below given conditions?

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

  • Vaishnav Profile Picture
    Vaishnav 45 on at
    RE: How to trigger a error message via code when creating a new record based on below given conditions?

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

  • Vaishnav Profile Picture
    Vaishnav 45 on at
    RE: How to trigger a error message via code when creating a new record based on below given conditions?

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

  • Suggested answer
    nmaenpaa Profile Picture
    nmaenpaa 101,156 on at
    RE: How to trigger a error message via code when creating a new record based on below given conditions?

    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.

  • Suggested answer
    Martin Dráb Profile Picture
    Martin Dráb 230,214 Most Valuable Professional on at
    RE: How to trigger a error message via code when creating a new record based on below given conditions?

    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().

  • Vaishnav Profile Picture
    Vaishnav 45 on at
    RE: How to trigger a error message via code when creating a new record based on below given conditions?

    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
    Martin Dráb 230,214 Most Valuable Professional on at
    RE: How to trigger a error message via code when creating a new record based on below given conditions?

    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.

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans