Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

Validating input value of form

(0) ShareShare
ReportReport
Posted on by 493

Senores, I need some help in validating a field in AX 2012 R2...

Here is what i have:

I override the method on the DS field control

public boolean validate()

{

   AgreementLineQuantityCommitment   agreementLineQuantityCommitment;

  boolean ret;

   ret = super();

   if(agreementLineQuantityCommitment.PricePerUnit<100)

       ret= checkFailed('Any message that you may want to display.');

   return ret;

}

I want to make sure the field value is greater than the calculated/recommended cost of an item.

Please any help suggestions would be greatly appreciated.

*This post is locked for comments

  • Lionel07 Profile Picture
    493 on at
    RE: Validating input value of form

    Hi Rustem,

    My understanding for using the select statement was to hold the Itemid on the line and compare it with the inventTable itemID. This obviously was the wrong thinking.

    Thanks much for your points, going forward I will definitely use these to better my understanding.

    Your code worked fine except that I had to change the method getValue() to realValue().

    Thanks much again for you help I am truly appreciative.

  • Verified answer
    Rustem Galiamov Profile Picture
    8,072 on at
    RE: Validating input value of form

    Hi Lionel07!

    1. What you want to achieve by using while select statement? You should select only one record from InventTable which depends on your ItemId from agreement line. Use ALine_AgreementLineQuantityCommitment as current record.

    2. If you use validate() method on field from data source table you don't need AgreementLine_AgreementLineQuantityCommitment_PricePerUnit control. Just use 'this'.

    3. You should return value of 'ret' variable only one time.

    Your code should looks like:

    public boolean validate()
    {
        boolean         ret;
       
        ret = super();
    
        if(this.getValue() <= InventTable::find(ALine_AgreementLineQuantityCommitment.ItemId).costPcsPrice())
        {
            ret = checkFailed('Item Price is lower than cost price');
        }
    
        return ret;
    }


  • Lionel07 Profile Picture
    493 on at
    RE: Validating input value of form

    Hi Rustem, thanks for your willingness to help. So I have done some more research and realized that its best I use costPcsPrice() to get the average cost of an item.

    Given that all I want to do is to validate the input amount for the field "unitprice" an overriding the validate method under the control. I have also changed my code to the following :

    public boolean validate()

    {    

      InventTable it;

      AgreementLine ag;

       boolean ret;

     ret = super();

    while select ItemId from it

      join ag

      where ag.ItemId == it.ItemId

     if(AgreementLine_AgreementLineQuantityCommitment_PricePerUnit.realValue()<=it.costPcsPrice() )

          {

              ret= checkFailed('Item Price is lower than cost price');

                return false;

           }

       return true;

    }

    Its not working yet but would appreciate if you could guide me.

  • Rustem Galiamov Profile Picture
    8,072 on at
    RE: Validating input value of form

    Hi Lionel07!

    Sorry for late response.

    Where did you override the modified() method?

  • Lionel07 Profile Picture
    493 on at
    RE: Validating input value of form

    Hi again Rustem,

    I have done some more work on the code here is the final draft.

    public boolean modified()

    {    

       InventItemPrice iip;

       AgreementLine ag;

        boolean ret;

      ret = super();

    if(this.realValue())

    {

       while select ItemId from iip

       join ag

       where ag.ItemId == iip.ItemId

      if(AgreementLine_AgreementLineQuantityCommitment_PricePerUnit.realValue()==iip.Price())

           ret= checkFailed('Any message that you may want to display.');

       return false;

    }

       return true;

    }

    So i choose to use the table InventItemPrice because from the manage cost tab on an item this info is taken from this table.

    All i need to do is to make sure a user does not enter a price lower than the cost price of an item when doing sales agreement. Grateful if you could help

  • Lionel07 Profile Picture
    493 on at
    RE: Validating input value of form

    Hi Rustem,

    Trust you are doing great.  From my last post, i have learnt that its best to validate fields from control level. ie. override the validate method on the design field control.

    I am now back on this issue you helped me with in the past.

    What I really want to do is ensure that whatever value the user enter for an item, that value is higher than the cost of the item. so with this am using two table to do the comparison...  These are: InventItemPrice and AgreementLineQuantityCommitment.

    AgreementLine_AgreementLineQuantityCommitment_PricePerUnit the name of the control.....autoDeclaration is set to YES

    This is my code, It doesn't show any error but its not validating.

    public boolean modified()

    {    

       InventItemPrice iip;

       AgreementLineQuantityCommitment abc;

        boolean ret;

      ret = super();

    if(this.realValue())

    {

       while select iip where iip.ItemId==abc.ItemId

       {

          if(AgreementLine_AgreementLineQuantityCommitment_PricePerUnit.realValue()<=iip.Price)

           ret= checkFailed('Any message that you may want to display.');

       }

    }

       return true;

    }

    Can you please guide me as to what am doing wrong?

  • Verified answer
    Rustem Galiamov Profile Picture
    8,072 on at
    RE: Validating input value of form

    So, try to use InventCostPriceCache class in validateField() method on AgreementLineQuantityCommitment table.

    Using this class allows you to get average unit cost price of the item.

    Declare the variable first, and then use this variable in PricePerUnit field validate:

    /// <summary>
    /// Validates a field in the <c>AgreementLineQuantityCommitment</c> table.
    /// </summary>
    /// <param name="_fieldIdToCheck">
    /// The field ID of the field to validate.
    /// </param>
    /// <returns>
    /// true if the value in the field is valid; otherwise, false.
    /// </returns>
    public boolean validateField(fieldId _fieldIdToCheck)
    {
        boolean ret;
        AgreementLineQuantityCommitment lineOrig;
        real    qtyDelta;
        // garu -->
        InventCostPriceCache inventCostPriceCache;
        // garu <--
    
        ret = super(_fieldIdToCheck);
    
        if (ret)
        {
            switch (_fieldIdToCheck)
            {
                case fieldNum(AgreementLineQuantityCommitment, CommitedQuantity) :
                    //
                    // There is no point to validate quantity changes if MAX is not enforced
                    // or nothing has been released from the line...
                    if (this.IsMaxEnforced == NoYes::Yes && this.isReleased())
                    {
                        qtyDelta = this.orig().CommitedQuantity - this.CommitedQuantity;
                        if ( (qtyDelta > 0)  && (this.remainingQueried() < qtyDelta))
                        {
                            // Qty has been decreased by more than remaining quantity...
                            ret = checkFailed("@SYS302115");
                        }
                    }
                    break;
    
                case fieldNum(AgreementLineQuantityCommitment, ProductUnitOfMeasure):
                    if (this.ProductUnitOfMeasure && !UnitOfMeasure::findBySymbol(this.ProductUnitOfMeasure).RecId)
                    {
                        ret = checkFailed(strFmt("@SYP4986038",fieldStr(AgreementLineQuantityCommitment, ProductUnitOfMeasure),
                            this.ProductUnitOfMeasure,
                            tableStr(UnitOfMeasure)));
                    }
    
                    if (ret && this.isReleased())
                    {
                        ret = checkFailed("@SYS137220");
                    }
                    break;
    
                case fieldNum(AgreementLineQuantityCommitment, PriceUnit):
                case fieldNum(AgreementLineQuantityCommitment, PricePerUnit):
                    // garu -->
                    inventCostPriceCache = InventCostPriceCache::construct();
                    info(strFmt("%1", inventCostPriceCache.costPricePcs(InventSum::find(this.ItemId, this.InventDimId), InventDim::find(this.InventDimId))));
                    break;
                    // garu <--
                case fieldNum(AgreementLineQuantityCommitment, LineDiscountAmount):
                    if (this.IsPriceInformationMandatory == NoYes::Yes && this.isReleased())
                    {
                        ret = checkFailed("@SYS137221");
                    }
                    break;
            }
            if (!ret)
            {
                lineOrig = this.orig();
                this.(_fieldIdToCheck) = lineOrig.(_fieldIdToCheck);
            }
        }
    
        return ret;
    }
  • Lionel07 Profile Picture
    493 on at
    RE: Validating input value of form

    Sir Rustem, am still fighting with this one. Please note am new to AX so am learning little by little. Also am doing a 1 man show for the company

  • Rustem Galiamov Profile Picture
    8,072 on at
    RE: Validating input value of form

    Did you solve the issue?

  • Suggested answer
    Rustem Galiamov Profile Picture
    8,072 on at
    RE: Validating input value of form

    Did you get the inventOnHand.costPricePcs() value?

    And you must write your logic only if user modified PricePerUnit field because of this method will fire on every table's field.

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

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 293,110 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,886 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156 Moderator

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans