How to Calculate in Nav ?

RanaRana Member Posts: 123
Dear all,

I have created a Table, say Packing Lines. Primary keys are :- "Document Type","Sales Doc. No.","Sales Doc. Line No.",Item,"Line No.".

There is a "Quantity" field in this Table. I want to sum up this field's value at time of entring value. So I have written a Function Say CheckSaleInvoiceLineQty and call this function in the Quantity - OnValidate()

CheckSaleInvoiceLineQty()
TotQty :=0;
PackingLines.SETCURRENTKEY("Document Type","Sales Doc. No.","Sales Doc. Line No.",Item,"Line No.");
PackingLines.SETRANGE("Document Type","Document Type");
PackingLines.SETRANGE("Sales Doc. No.","Sales Doc. No.");
PackingLines.SETRANGE("Sales Doc. Line No.","Sales Doc. Line No.");
IF PackingLines.FINDSET THEN
    REPEAT
      TotQty+= PackingLines.Quantity ;
    UNTIL PackingLines.NEXT = 0;

When I am entering any value in the quantity field the function is executed and enter into the REPEAT
UNTIL Loop , but finds PackingLines.Quantity =0 value. why ?
:shock: :shock: :shock:

Comments

  • SnoopySnoopy Member Posts: 43
    Try: packinglines.modify;
  • BeliasBelias Member Posts: 2,998
    from which object and in which trigger this function is run?
    CheckSaleInvoiceLineQty()

    you're reading a record variable, it means that if ther record is not committed to the database, you won't see it in an instance of your table.
    -Mirko-
    "Never memorize what you can easily find in a book".....Or Mibuso
    My Blog
  • kinekine Member Posts: 12,562
    If you are calling it from OnValidate of new line, you will not see this line in the loop, because it was not inserted yet. You need to add the actual value from Rec and subtract the old value from xRec to have correct value.
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • navuser1navuser1 Member Posts: 1,329
    kine wrote:
    If you are calling it from OnValidate of new line, you will not see this line in the loop, because it was not inserted yet. You need to add the actual value from Rec and subtract the old value from xRec to have correct value.

    This is not working correctly! :shock: :shock: :shock:

    Try this code ....
    TotQty :=0;
    PackingLines.SETCURRENTKEY("Document Type","Sales Doc. No.","Sales Doc. Line No.",Item,"Line No.");
    PackingLines.SETRANGE("Document Type","Document Type",);
    PackingLines.SETRANGE("Sales Doc. No.","Sales Doc. No.");
    PackingLines.SETRANGE("Sales Doc. Line No.","Sales Doc. Line No.");
    PackingLines.SETFILTER("Line No.",'<>%1',"Line No");
    IF PackingLines.FINDSET THEN BEGIN
      REPEAT
       TotQty+= PackingLines.Quantity;
      UNTIL PackingLines.NEXT =0;
       TotQty += Qty;
    END;
    
    Now or Never
  • David_SingletonDavid_Singleton Member Posts: 5,479
    Use a flow field.
    David Singleton
  • kinekine Member Posts: 12,562
    navuser1 wrote:
    kine wrote:
    If you are calling it from OnValidate of new line, you will not see this line in the loop, because it was not inserted yet. You need to add the actual value from Rec and subtract the old value from xRec to have correct value.

    This is not working correctly! :shock: :shock: :shock:

    Try this code ....
    TotQty :=0;
    PackingLines.SETCURRENTKEY("Document Type","Sales Doc. No.","Sales Doc. Line No.",Item,"Line No.");
    PackingLines.SETRANGE("Document Type","Document Type",);
    PackingLines.SETRANGE("Sales Doc. No.","Sales Doc. No.");
    PackingLines.SETRANGE("Sales Doc. Line No.","Sales Doc. Line No.");
    PackingLines.SETFILTER("Line No.",'<>%1',"Line No");
    IF PackingLines.FINDSET THEN BEGIN
      REPEAT
       TotQty+= PackingLines.Quantity;
      UNTIL PackingLines.NEXT =0;
       TotQty += Qty;
    END;
    

    What is Qty? I assume that you need to add Rec.Quantity and not Qty...
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • navuser1navuser1 Member Posts: 1,329
    kine wrote:

    What is Qty? I assume that you need to add Rec.Quantity and not Qty...

    Qty is a parameter.Which is nothing but Rec.Quantity .
    Now or Never
  • kinekine Member Posts: 12,562
    If you are modifying the record, you need to subtract xRec.Quantity and add Rec.Quantity, if you are inserting record, you need only add the Rec.Quantity.

    Could you describe some example of data an the result?
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • RanaRana Member Posts: 123
    kine wrote:
    If you are modifying the record, you need to subtract xRec.Quantity and add Rec.Quantity, if you are inserting record, you need only add the Rec.Quantity.

    Could you describe some example of data an the result?


    I'm creating the Packing List lines against a Sales Invoice Line. And try to block that The Packing Lines total quantity could not exceed the Sales Line Quantity
  • kinekine Member Posts: 12,562
    I know, I though that you write which data you have in the table, which result you got, what you want to have as result ... example with data and what is wrong ("I have these records.... and when I enter XXX into new line, the sum is AAA but it should be BBB")
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
Sign In or Register to comment.