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 :
Supply chain | Supply Chain Management, Commerce
Answered

Custom service validateWrite validation skipped

(0) ShareShare
ReportReport
Posted on by 150

I'm having a custom service created and published, it is to create a customer from a third-party application using the given web API. The problem is when the customer is created and given for example an invalid TaxGroup or Currency which doesnt exist in main table (for example to give USDDD rather than the value in main table USD) it doesnt return false in super() - validateWrite i went through this by using the debugger when calling the service.

But for sure when you manually create the customer and assign wrong value it will give an error message that the value doesnt exist. not sure if there is some kind of setup for this service or service group. any idea guys?

I have the same question (0)
  • nmaenpaa Profile Picture
    101,162 Moderator on at

    When you manipulate data via code, you need to call validateWrite yourself.

  • Ammar Salah Profile Picture
    150 on at

    This is what i did, but it returns true even when it should return false. Try to create a quick customer create service and call validateWrite as u suggested. Pass or assign an invalid TaxGroup and share with us what u see. thanks Nikola

  • nmaenpaa Profile Picture
    101,162 Moderator on at

    Could you share your code? It's easier than us having to guess how it looks like and trying to write similar one.

    Also does your code work if you run it in a job?

  • Ammar Salah Profile Picture
    150 on at

    Nikola, check this pls

    class INDXIntCustSvc
    {
    public boolean createOrUpdate(INDXIntCustContract _contract)
    {
    boolean ret;
    CustTable custTable;

    ttsbegin;
    //INDXIntCustSvc::validateCreate(_contract); // since validateWrite doesnt work i did my own validation to move forward

    custTable = CustTable::find(_contract.parmCustAccount(),true);
    custTable.TaxGroup = _contract.parmTaxGroup(); // try to pass incorrect value
    if (custTable.validateWrite()) // validate write always return true
    {
    custTable.update();
    ret = true;
    }
    ttscommit;

    return ret;
    }

    public static void validateCreate(INDXIntCustContract _contract)
    {
    if (!_contract.parmCustAccount())
    throw AifFault::faultList("CustAccount must be filled in.", "CustAccountEmpty");

    if (!_contract.parmTaxGroup()) throw AifFault::faultList("TaxGroup must be filled in.", "TaxGroupEmpty");

    if (!TaxGroupHeading::exist(_contract.parmTaxGroup()))
    throw AifFault::faultList("TaxGroup does not exist in main table.", "TaxGroupNotFound");
    }
    }

  • nmaenpaa Profile Picture
    101,162 Moderator on at

    You edited your code a bit after first sharing it. In the first version 

    you had something like this:

    if (custTable.validateWrite())
    return true;
    {
        custTable.update();
    }
    

    And actually that code would update custTable only if validateWrite() returns false.

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

    Anyway, seems you need to use validateField() method to validate the fields.

    This is not related to custom service, it's how record validation works in x++.

    So, if you use validateWrite(fieldNum(CustTable, CustGroup)) after putting the new value in custGroup, you will get the expected result.

    Also in the CustTable form the validation happens when the field is changed, not when the record is saved.

  • Ammar Salah Profile Picture
    150 on at

    Nikola thanks for your time, so it seems now its bit different from AX2012 and D365 how it handles the validation for incorrect values (i had the same code running in AX2012 services and its working as expected but not in D365, this is why i raised this post)

    So you're saying the best solution is to call the validateField for all the fields im using? check a sample code

    custTable.AccountNum = _contract.parmCustAccount();

                   custTable.CustGroup  = _contract.parmCustGroup();

                   custTable.validateField(fieldNum(CustTable,CustGroup));

                   custTable.initFromCustGroup(CustGroup::find(custTable.CustGroup));

                   CustTable::confirmAndSaveCustGroupChange(custTable, false);

                   custTable.Currency = _contract.parmCurrency();

                   custTable.validateField(fieldNum(CustTable,Currency));

                   custTable.PaymTermId = _contract.parmPaymTermId();

                   custTable.validateField(fieldNum(CustTable,PaymTermId));

                   custTable.TaxGroup = _contract.parmTaxGroup();

                   custTable.validateField(fieldNum(CustTable,TaxGroup));

                   custTable.CreditMax = _contract.parmCreditMax();  

                   custTable.IdentificationNumber = _contract.parmIdentificationNumber();

    Isn't there any better way? 

  • Verified answer
    nmaenpaa Profile Picture
    101,162 Moderator on at

    I don't know what code you have in AX2012, but validateWrite() works just the same. You can easily try it with a job.

    And yes, seems you need to validate the fields one by one in this case.

  • Ammar Salah Profile Picture
    150 on at

    Ok thanks alot Nikola, lets see if someone else got any other ideas :)

  • WillWU Profile Picture
    22,361 on at

    Hi Ammar,

    Your codes looks well.

    As Nikolaos said, we don't know what code you have in AX2012 and what kind of verification do you need.

    The validateWrite() method validates that each mandatory field is filled in.This is the place to add validation through code.

    It does not validate that the record already exists.

    Maybe you need to check if some fields are mandatory fields.

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 > Supply chain | Supply Chain Management, Commerce

#1
Laurens vd Tang Profile Picture

Laurens vd Tang 301 Super User 2025 Season 2

#2
André Arnaud de Calavon Profile Picture

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

#3
Siv Sagar Profile Picture

Siv Sagar 105 Super User 2025 Season 2

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans