Open Forum

Like what you see? Discover the benefits of the NAVUG Community. Learn More

1.  Release Sales Documents

Posted 8 days ago
I am trying to add something like this to the Codeunit 414 - Release Sales Documents.  The Amount doesn't want to calculate after I change the sales lines.

The idea is to have a minimum order from the customer table for an sales order/quote/invoice.  I will add that later, just want to get this part working.  The calcfields doesn't seem to add up all of the lines after I edit the sales lines in NAV.  Any ideas where I went wrong?  Thanks

OnRun(VAR Rec : Record "Sales Header")
IF Status = Status::Released THEN  //Standard nav
EXIT;   //Standard nav

//Check for Customer Minimum Order Status
CALCFIELDS(Amount);
MODIFY(TRUE);
Customer.SETFILTER("No.", "Sell-to Customer No.");
IF Customer.FIND('-') THEN
IF (Amount < Customer."Minimum Order") THEN
ERROR('The total Amount must be more than %1. It is currently %2 on %3', Customer."Minimum Order", Amount, "No.");

.....
We are on NAV 2015.

------------------------------
Jonathan Delfraisse
Computer Specialist
MANCON
Virginia Beach VA
------------------------------


2.  RE: Release Sales Documents

Posted 8 days ago
It should work, but try to skip the MODIFY(TRUE) (it is not needed in your case, and it also runs code in the background that can interfere with your code).
So it would look like this:

CALCFIELDS(Amount);
Customer.GET("Sell-to Customer No.");
IF (Amount < Customer."Minimum Order") THEN
  ERROR('The total Amount must be more than %1. It is currently %2 on %3', Customer."Minimum Order", Amount, "No.");


And the error message would be nice looking in the global variable text constants, instead being in the code line :)


------------------------------
Kováts, Attila
------------------------------



3.  RE: Release Sales Documents

Posted 8 days ago
I agree with Attila - it is likely the Modify that is reverting the Calcfields value of the Amount field.  Changing from Find to Get will also give an error message if the Customer record is not retrieved which will narrow down the issue.  You could also put in a temporary Message line to show what values you have for both Amount and Minimum Order.

One general comment: if you want your code to be "upgrade friendly," use a one line readable hook instead of putting all the lines of your custom function into a standard codeunit object.  By "readable" I mean something like "CheckForCustomerMinimumOrderStatus;"  Then you create a function in one of your own custom codeunit objects of the same name.  That way when you merge objects during the next upgrade, your delta between the standard CU414 and your customized CU414 is only one line (plus the declaration for your custom CU object).  And someone reading your code will have a general idea of the purpose of that function without needing to explain it with escaped comments.

This will pave the way for you to use Event trigger subscriptions in the newer versions which start with I believe the 2016 version.  Eventually there will be event subscriptions for all triggers so that all of our customizations can run without any modifications to base code objects.  I have the feeling MS wants to push everyone this direction (events and/or extensions) and I think it is a good idea.  ISVs are converting their solutions into Extensions for these reasons.  Allowing us to have all our customizations without changes to any base code - eventually everyone will very easily be able to stay on the most recent version.

Sorry to get on a soapbox there.  Hope this helps.

------------------------------
Paul Turner
Liberty Mountain
Sandy UT
------------------------------



4.  RE: Release Sales Documents

Posted 8 days ago
Agree with Attila's cleaning up of the code.  Either debug right after the calcfields and tell us the amount or but this code in there instead and tell us the amount:

CALCFIELDS(Amount);
MESSAGE('Amount is %1',Amount);
EXIT;

It would be odd if the calcfields was not working there.

------------------------------
Jason Wilder
Senior Application Developer
Stonewall Kitchen
York ME
------------------------------



Best Answer
5.  RE: Release Sales Documents

Posted 8 days ago
Hi Jonathan,

I think the issue you are having is the Amount field on the sales header calculates to 0 if the order is in an "open" status. So if you are trying to check the amount of the order before release you should sum the values from the sales line table.


------------------------------
Joe Shields
Advanced Media Technologies
------------------------------



6.  RE: Release Sales Documents

Posted 8 days ago
Oh that's right.  Joe nailed it.

------------------------------
Paul Turner
Liberty Mountain
Sandy UT
------------------------------



7.  RE: Release Sales Documents

Posted 8 days ago
Hey Paul,
     Good idea.  No worries about getting on the soapbox.  It's always good to learn about best practices with a shared development environment like NAV.  I am still learning my way around.

Joe,
  Based on your recommendation, I came up with this code.  For some reason, the "Amount" field on the Sales Line was still zero, so I used Quantity * Unit Price.

CheckForCustomerMinimumOrderStatus(VAR SalesHeader : Record "Sales Header")
WITH SalesHeader DO BEGIN
   IF "Document Type" IN ["Document Type"::Quote, "Document Type"::Order,"Document Type"::Invoice] THEN BEGIN
      SalesLine.SETRANGE("Document Type","Document Type");
      SalesLine.SETRANGE("Document No.","No.");

      Amt := 0;
      IF SalesLine.FINDSET THEN
         REPEAT
            Amt := Amt + (SalesLine."Quantity" * SalesLine."Unit Price");
         UNTIL SalesLine.NEXT = 0;

      Customer.SETFILTER("No.", "Sell-to Customer No.");
      IF Customer.FIND('-') THEN
         IF (Amt < Customer."Minimum Order") THEN
            ERROR(Text001, Customer."Minimum Order", Amt, "No.");
   END;
END;

Thanks everyone for their quick response.

------------------------------
Jonathan Delfraisse
Computer Specialist
MANCON
Virginia Beach VA
------------------------------



8.  RE: Release Sales Documents

Posted 4 days ago
Jonathan,

You are right good catch, "Amount" on the Sales Line behaves like "Amount" on the header.

------------------------------
Joe Shields
Advanced Media Technologies
------------------------------



9.  RE: Release Sales Documents

Posted 3 days ago
​I noticed that you are passing the Sales Header record and have the Var checked. I only use this when I am modifying the parameter that I am passing. There is less overhead when passing a parameter by value instead of reference.

------------------------------
Joseph Flynn
Gold Star Foods INC
Ontario CA
------------------------------



10.  RE: Release Sales Documents

Posted 2 days ago
There is code in different versions of NAV in the same codeunit that 0's the amount field when you Re-open the Sales Order.  My version of NAV 2016 had this code removed (but NAV 2009 had it).  So when we reopen a Sales Document it does not 0 the sales lines.  If you look at the Reopen function of codeunit 414 you will probably see that there is a loop through the sales lines where it 0's the amount fields.  This is why your Amount is 0 but once you upgrade it will probably not be 0.

Looks like you already have a solution but don't forget about Line and Inv. Discounts in your calculation to add up sales.

Another option would be to lower your code to the end of the OnRun function where the Amount field will be filled in and you can do your original chunk of code that you were trying to do.

------------------------------
Jason Wilder
Senior Application Developer
Stonewall Kitchen
York ME
------------------------------