Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Small and medium business | Business Central, N...
Answered

check condition for each line of sales line

(0) ShareShare
ReportReport
Posted on by 127

i have added one field on price list line and sales line and one Boolean field on sales header.

If unit price<min price then header field is true i have written code as below and it is working  but problem is it is not checking all line of sales line.If there is any single line with unit price less than min price then Boolean field must be true always. 

[EventSubscriber(ObjectType::Table, Database::"Sales Line", 'OnAfterGetSalesHeader', '', true, true)]
    local procedure MyProcedure(var SalesLine: Record "Sales Line"; var SalesHeader: Record "Sales Header"; var Currency: Record Currency)
    var
        PriceList: Record "Price List Line";
    begin
        PriceList.Reset();
        PriceList.SetRange("Asset No.", SalesLine."No.");
        if PriceList.FindSet(true, true) then
            repeat
                if not salesHeader.get(SalesLine."Document Type", SalesLine."Document No.") then
                    exit;
                if (PriceList."Min. Price Change Allw" > SalesLine."Unit Price") then

                    salesHeader."Min. Price Change Hold" := true
                    else
                          salesHeader."Min. Price Change Hold" := false;


           
            until
               SalesLine.Next() = 0;
        SalesHeader.Modify()



       

    end;

  • Suggested answer
    Nitin Verma Profile Picture
    21,400 Moderator on at
    RE: check condition for each line of sales line

    I think you should refer the Sales line - No. and quantity fields events

    [EventSubscriber(ObjectType::Codeunit, Codeunit::"Sales Line", 'OnAfterValidate', 'No.', false, false)]

    [EventSubscriber(ObjectType::Codeunit, Codeunit::"Sales Line", 'OnAfterValidate', 'Quantity', false, false)]

  • Salba Z.K Profile Picture
    127 on at
    RE: check condition for each line of sales line

    [EventSubscriber(ObjectType::Codeunit, Codeunit::"Sales Line - Price", 'OnAfterValidatePrice', '', false, false)]

       local procedure OnAfterSetPrice(var SalesLine: Record "Sales Line"; AmountType: Enum "Price Amount Type"; CurrPriceType: Enum "Price Type")

       var

           salesHeader: record "Sales Header";

           PriceListLine: Record "Price List Line";

       begin

           PriceListLine.Reset();

           PriceListLine.SetRange("Asset No.", SalesLine."No.");

           if PriceListLine.FindLast() then begin

               if not salesHeader.get(SalesLine."Document Type", SalesLine."Document No.") then

                   exit;

               if PriceListLine."Min. Price Change Allw" > SalesLine."Unit Price" then

                   salesHeader."Min. Price Change Hold" := true;

               if PriceListLine."Min. Price Change Allw" < SalesLine."Unit Price" then

                   salesHeader."Min. Price Change Hold" := false;

               salesHeader.Modify();

           end;

       end;

    I have tried it is not working . Nothing is happening with this

  • Salba Z.K Profile Picture
    127 on at
    RE: check condition for each line of sales line

     [EventSubscriber(ObjectType::Table, Database::"Sales Line", 'OnAfterValidateEvent', 'Unit Price', true, true)]
        local procedure OnAfterValidateEventUnitPrice(var Rec: Record "Sales Line"; var xRec: Record "Sales Line"; CurrFieldNo: Integer)
        var
            salesHeader: Record "Sales Header";
            PriceList: Record "Price List Line";
        begin
            PriceList.Reset();
            PriceList.SetRange("Asset No.", Rec."No.");
            if PriceList.FindLast() then begin
                if not salesHeader.get(Rec."Document Type", Rec."Document No.") then
                    exit;
                if PriceList."Min. Price Change Allw" < Rec."Unit Price" then
                    salesHeader."Min. Price Change Hold" := false
                else
                    salesHeader."Min. Price Change Hold" := true;
                SalesHeader.Modify()
            end;
        end;

    I have also tried but it is not checking each line it is working for only which line on which change unit price. As you see in Image I have Changes Unit price of both item below min price changes allowed i.e 300 and 1900 but when i changed 300 to 500 boolean field get false But it must be true because second line unit price  is still below min price.

    ss2.png

    According to condition if any line have unit price less than a min price then Boolean field must be true on sales header.

    ss3.png

    I have also tried bel ow code but it does not changing anything hence I have used code that mention before. Due you have any other idea how can we solve that that satisfy condition.

    EventSubscriber(ObjectType::Codeunit, Codeunit::"Sales Line - Price", 'OnAfterSetPrice', '', false, false)]
       local procedure OnAfterSetPrice(var SalesLine: Record "Sales Line"; PriceListLine: Record "Price List Line"; AmountType: Enum "Price Amount Type")
       var
           salesHeader: record "Sales Header";
       begin
           if not salesHeader.get(SalesLine."Document Type", SalesLine."Document No.") then
               exit;
           if PriceList."Min. Price Change Allw" > Rec."Unit Price" then
               salesHeader."Min. Price Change Hold" := true;
           if PriceList."Min. Price Change Allw" < Rec."Unit Price" then
               salesHeader."Min. Price Change Hold" := false;
           salesHeader.Modify();
           end;

  • Verified answer
    YUN ZHU Profile Picture
    78,879 Super User 2025 Season 1 on at
    RE: check condition for each line of sales line

    Hi, Depending on your needs, I think adding the control code to the OnAfterValidate Trigger of Unit Price field in Sales Lines would be fine, so that it would trigger every time when the line is validated.

    pastedimage1661299564312v1.png

    Hope this helps as well.

    Thanks.

    ZHU

  • Suggested answer
    Inge M. Bruvik Profile Picture
    999 Moderator on at
    RE: check condition for each line of sales line

    If you want it to respond to changes in your unit price. Would it not be better to do it in a event connected to the unit price field? I don't think every price change will call on the sales header.

    Look in the code unit "Sales Line - price" and the the OnAfterValidatePrice event f.ex.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,933 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,801 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156 Moderator

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans