Blocking Order without Advance Deposit

Hello Guys my company needed to block order processing happening if customer does not have deposit in advance and my company doe not signed ATS with the System Vendor yet and i am more of the support and functional guy yet i know intermediate NAV Development and my way around it.So i tried putting the following code on the Post action on the sales order page and it works well and i need the good guys of this forum to take a look at this code and suggest enhancement or if there are any pitfalls i haven't expected.By the way i am excited to deliver my first development challenge.


Customer.GET("Sell-to Customer No.");
Customer.CALCFIELDS(Balance);
SalesLine.SETFILTER(SalesLine."Document No.","No.");
SalesLine.CALCSUMS(SalesLine.Amount);
SalesLineIncVAT:=SalesLine.Amount * 1.15;//To calculate line amount including VAT

IF Customer."Gen. Bus. Posting Group"='CREDIT' THEN //Continue if customer is credit customer
   Post(CODEUNIT::"Sales-Post (Yes/No)")
ELSE
    IF Customer.Balance < 0 THEN BEGIN
      "Cust.Bal.Pos.":=Customer.Balance*-1;//To change the negative customer to positive
       Shortage:=SalesLineIncVAT-"Cust.Bal.Pos."; //To compare Balance and order amount
           IF ("Cust.Bal.Pos." >= SalesLineIncVAT) THEN BEGIN //IF customer balance covers the order amount
               Post(CODEUNIT::"Sales-Post (Yes/No)")
           END
           ELSE
               ERROR(Text003,Customer.Balance,SalesLineIncVAT,Shortage) //IF customer balance does not cover the order amount
    END ELSE BEGIN
           ERROR(Text004);//If no deposit at all

 

//Thanks in advance!!

  • Hi Alemayehu,
    Which version of NAV is this about? If NAV 2016+ then just use a workflow.
  • And to comment on your code. The first real "project" is always exciting. Let me start by showing you how I would have done, then then later tell you why.

    Customer.GET("Sell-to Customer No.");
    IF Customer."Signed ATS Contract" THEN 
       Post(CODEUNIT::"Sales-Post (Yes/No)")
    ELSE BEGIN
      Customer.CALCFIELDS(Balance);
      SalesLine.SETRANGE("Document Type","Document Type");
      SalesLine.SETFILTER("Document No.","No.");
      SalesLine.CALCSUMS("Amount Including VAT");
    
      IF Customer.Balance < 0 THEN BEGIN
        Shortage := SalesLine."Amount Including VAT" - ABS(Customer.Balance);
        IF (ABS(Customer.Balance) >= SalesLine."Amount Including VAT") THEN 
          Post(CODEUNIT::"Sales-Post (Yes/No)")
        ELSE
          ERROR(BalanceBelowOrderAmountErr,ABS(Customer.Balance),SalesLine."Amount Including VAT",Shortage) 
      END ELSE 
        ERROR(CustomerHasNoBalanceErr);
    END;

    Firstly then you should ALWAYS prevent doing ANY hard-coding. In your example you have hard-coded both the 'CREDIT' posting group and the VAT percentage. Instead I would create a new field in the customer table to filter on instead. Even if this is the simple way you need now, then not hard code.

    Same with the VAT %, just use the "Amount including VAT" field instead.

    Instead of * -1 just use the ABS() function.

    Finally to follow current NAV development best practices, then you should name your new text constants Text003 and Text004 to something more saying. By making your code simpler and easier to read, then you also don't need the many inline (//) comments.

    (PS: development questions should always be posted in the Developer forum, not the User forum).

  • In reply to Erik P. Ernst:

    Thank you Erik this is a kind of advice i needed, from the pros. like you.It means a lot, i will structure this code based on your advice and also From now on i will start dev. questions on the Dev. forum.
  • In reply to Erik P. Ernst:

    I am using 2015 Version
Related