extension-methodsmicrosoft-dynamicsaxaptax++

SalesLine.update function has been called incorrectly


My goal is to change the calculation of line Amount on Sales Line by multiplying a custom field 'NoOfMonths' to the value of line amount. I have created an extension of SalesLine table and used the update() method to fulfill the requirement. Following is my code:

[Extensionof(tableStr(SalesLine))]
final class PRMSC_SalesLine_Extension

public void update(boolean  _dropInvent,
                   Common   _childBuffer,
                   boolean  _updateOrderLineOfDeliverySchedule ,
                   boolean  _mcrAutoallocate ,
                   boolean  _cameFromCreate ,
                   boolean  _promptSuppItem)
{
         next update();
         SalesLine saleLine;
         if (this.ItemId == saleLine.ItemId)
         {
            saleLine.LineAmount = saleLine.LineAmount * saleLine.PRMSC_NoOfMonths;
         }
} 

Build and DB sync complete successfully. On debugging and changing any value on the front end of Sales Order, this error generates SalesLine.update function has been called incorrectly On debugging, this error comes on the next update() line.


Solution

  • The above code will never work. It compares an empty SalesLine record with the current this sales line record, will always fail.

    [Extensionof(tableStr(SalesLine))]
    final class PRMSC_SalesLine_Extension
    
    public void update(boolean  _dropInvent,
                       Common   _childBuffer,
                       boolean  _updateOrderLineOfDeliverySchedule ,
                       boolean  _mcrAutoallocate ,
                       boolean  _cameFromCreate ,
                       boolean  _promptSuppItem)
    {
             if (this.PRMSC_NoOfMonths)
             {
                this.LineAmount = this.LineAmount * this.PRMSC_NoOfMonths;
             }
             next update();
    } 
    

    super is called after the multiplication, so it is saved along with the other changed fields.

    This will work better, though I do not recommend it. It does not handle the insert case, also the LineAmount is updated every time update is called, which will make the line amount go up!

    Also it the multiplication happens to late, the users first sees the correct value after the record is saved.

    You will have to rethink this solution, it does not work.