Code syntax issue on Sales Line - any help?

gadzilla1gadzilla1 Member Posts: 316
Newbie here,

I have a boolean field added to the Sales Line table called "Allow Volume Disc.". I want some code that applies a volume discount to execute EVERY time the boolean field is true/checked on the Sales Order, and skip every record that this field is FALSE/unchecked.

Currently everything fires great, except when there is a record in the pack that is unchecked..it applies the volume discount to the unchecked record anyway.

Here's the code I have now:

IF SalesLine2."Allow Volume Disc." THEN REPEAT
CalcVolumeDiscount(Rec);
UNTIL SalesLine2.NEXT = 0;

What needs to be changed to check for unchecked records?

Thanks for any assistance in this matter. gad1

Answers

  • ara3nara3n Member Posts: 9,255
    Your code should look something like this on the validate


    SalesLine2.setrange("documen type","Document type");
    SalesLine2.setrange("documen No.","Document No.");
    SalesLine2.setfilter("line no.",'<>%1',"Line No.");
    SalesLine2.setrange("Allow Volume Disc.",true);
    IF SalesLine2.find('-') THEN REPEAT
    CalcVolumeDiscount(SalesLine2);
    UNTIL SalesLine2.NEXT = 0;

    If "Allow Volume Disc." then
    CalcVolumeDiscount(Rec);
    Ahmed Rashed Amini
    Independent Consultant/Developer


    blog: https://dynamicsuser.net/nav/b/ara3n
  • gadzilla1gadzilla1 Member Posts: 316
    Rashed,

    Thanks for the tip...you say I should call your code from the on-validate...what if I'm calling the code from codeunit 414?

    Will the code change?

    Thanks again - gad1
  • diptish.naskardiptish.naskar Member Posts: 360
    I don't think the setfilter on the line no. is necessary..the rest seems fine..
    Diptish Naskar
    For any queries you can also visit my blog site: http://msnavarena.blogspot.com/
  • ara3nara3n Member Posts: 9,255
    if you are doing it from release sales document. Then this how you would do it.
    Rec is SalesHeader

    SalesLine2.setrange("documen type","Document type");
    SalesLine2.setrange("documen No.","No.");
    SalesLine2.setrange("Allow Volume Disc.",true);
    IF SalesLine2.find('-') THEN REPEAT
    CalcVolumeDiscount(SalesLine2);
    UNTIL SalesLine2.NEXT = 0;
    Ahmed Rashed Amini
    Independent Consultant/Developer


    blog: https://dynamicsuser.net/nav/b/ara3n
  • DenSterDenSter Member Posts: 8,304
    IF SalesLine2.FINDSET(FALSE,FALSE) THEN REPEAT
    
  • ara3nara3n Member Posts: 9,255
    I don't know what version he is on. So always stay on safe side.
    Ahmed Rashed Amini
    Independent Consultant/Developer


    blog: https://dynamicsuser.net/nav/b/ara3n
  • gadzilla1gadzilla1 Member Posts: 316
    Thanks everyone for all of your help. DenSter, Rashed, I am on version 4.01 just FYI.

    I put the code in verbatim to the following. I also saw the "Rec is SalesHeader" from Rashed and understand why all of my records were calculated for a volume discount as a result...not just the 'checked' items.

    I am getting the error 'The Record variable must belong to 36 and not to 37' when trying to save the changes to the Codeunit...where do I remedy this? I placed the following code in the OnRun area of Codeunit 414.

    SalesLine2.setrange("document type","Document type");
    SalesLine2.setrange("document No.","No.");
    SalesLine2.setrange("Allow Volume Disc.",true);
    IF SalesLine2.find('-') THEN REPEAT
    CalcVolumeDiscount(SalesLine2);
    UNTIL SalesLine2.NEXT = 0;

    Once again, newbie... :roll: I'm only posting after I try to figure things out on my own and through reading the ADG.

    Thanks - gad1
  • ara3nara3n Member Posts: 9,255
    Your function CalcVolumeDiscount( is expecting a sales Header.

    It does does the calculation. Can you paste that code in here?

    So we can see what it's doing.
    Ahmed Rashed Amini
    Independent Consultant/Developer


    blog: https://dynamicsuser.net/nav/b/ara3n
  • gadzilla1gadzilla1 Member Posts: 316
    Rashed - I sent a PM with the code. Thanks - gad1
  • ara3nara3n Member Posts: 9,255
    ok got your code.
    Basically you want to only calculate volume discount on lines that have the boolean checked.
    To do this we have to modify the function.

    So my question is do you want to include the lines in the calculation of volume discount but do not update the unit price on those sales lines?
    Or just exlude them totally from the calculate?


    So if you have two lines Item A qty 100, Item A qty 100. And you've setup volume discount of 150. Should the order get any volume discount if only 1st line is checked?
    Ahmed Rashed Amini
    Independent Consultant/Developer


    blog: https://dynamicsuser.net/nav/b/ara3n
  • gadzilla1gadzilla1 Member Posts: 316
    Rashed - Thanks a ton for your help in this matter. Your questions are numbered and answered below.

    1. "calculate volume discount on lines that have the boolean checked."

    Yes. BUT I just talked to a customer service person and I had a major spec incorrect. The function should SUM up all quantities for all checked items and apply the discount to only the SUM of all checked items.

    So, if the boolean is checked for Item A qty 100 and checked for Item B qty 100 and NOT checked for Item C qty 100...if a volume discount is set to 150, then the discount will only be applied to Item A and B.

    2. "do you want to include the lines in the calculation of volume discount"

    I'd like to include the lines in the calculation and NOT change the unit price...but if this is too difficult then please just leave it. The issue with having the unit price change on release of the Sales Order comes later on the Sales Invoice report where we'd like to see the change between Net and Unit price and report a discount effectively at the lines level. Right now I can't do that.

    3. "So if you have two lines Item A qty 100, Item A qty 100. And you've setup volume discount of 150. Should the order get any volume discount if only 1st line is checked?"

    Since the functionality should sum up only the checked item qty (see 1.) I don't think this matters anymore. There are cases where this is now valid.

    My apologies for writing a dictionary. :) Thanks - gad1
  • ara3nara3n Member Posts: 9,255
    Ok Here is a change in code.

    SalesLine2.setrange("document type","Document type");
    SalesLine2.setrange("document No.","No.");
    SalesLine2.setrange("Allow Volume Disc.",true);
    IF SalesLine2.find('-') THEN REPEAT
    CalcVolumeDiscount(Rec);
    UNTIL SalesLine2.NEXT = 0;

    In the function. CalcVolumeDiscount add the following line.

    /update Unit Price on the lines if the Volume Discount produces a lower price.
    IF TotalVolumeDiscAmount >= TotalInvoiceDiscAmount THEN BEGIN
    //Mod Start
    SalesLine.setrange("Allow Volume Disc.",true);
    //Mod End
    IF SalesLine.FINDSET(TRUE, FALSE) THEN REPEAT
    Ahmed Rashed Amini
    Independent Consultant/Developer


    blog: https://dynamicsuser.net/nav/b/ara3n
  • gadzilla1gadzilla1 Member Posts: 316
    Rashed,

    This works awesome, thank you!!! I understand the code as well.

    I will put 'solved' in the title.

    If you can EASILY answer the following, ok, if not please disregard.

    How do I get the original unit price BACK when I reopen the order and delete some of the sales lines and the volume discount is either less or should not be applied.

    Right now if the unit price is 10.00, and the discounted price is 9.00, the price stays the same when I reopen and change quantities or delete lines. When I reopen the order I want the original unit price back (10.00) and then run the function again.

    Thanks - gad1
  • ara3nara3n Member Posts: 9,255
    Code something like this will do the trick. PriceCalcMgt is CU Sales Price Calc. Mgt.


    SalesLine2.reset;
    SalesLine2.setrange("documen type","Document type");
    SalesLine2.setrange("documen No.","No.");
    IF SalesLine2.find('-') THEN REPEAT
    PriceCalcMgt.FindSalesLinePrice(Rec,SalesLine2,6);
    SalesLine2.VALIDATE("Unit Price");
    SalesLine2.modify;
    UNTIL SalesLine2.NEXT = 0;
    SalesLine2.reset;

    Put that before

    SalesLine2.setrange("document type","Document type");
    SalesLine2.setrange("document No.","No.");
    SalesLine2.setrange("Allow Volume Disc.",true);
    IF SalesLine2.find('-') THEN REPEAT
    CalcVolumeDiscount(Rec);
    UNTIL SalesLine2.NEXT = 0;

    Haven't tested it. So test it beforehand.
    Ahmed Rashed Amini
    Independent Consultant/Developer


    blog: https://dynamicsuser.net/nav/b/ara3n
  • gadzilla1gadzilla1 Member Posts: 316
    Unreal...it works very well.

    =D>

    Thanks - Chris
  • ara3nara3n Member Posts: 9,255
    You are welcome. Hope you learned enough to do your next modification.
    Ahmed Rashed Amini
    Independent Consultant/Developer


    blog: https://dynamicsuser.net/nav/b/ara3n
  • gadzilla1gadzilla1 Member Posts: 316
    I learned a great deal. Take care - gad1
Sign In or Register to comment.