web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
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 151

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
    IB-29041624-0 Profile Picture
    1,191 Moderator on at

    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.

  • Verified answer
    YUN ZHU Profile Picture
    103,227 Super User 2026 Season 2 on at

    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

  • Salba Z.K Profile Picture
    151 on at

     [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;

  • Salba Z.K Profile Picture
    151 on at

    [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

  • Suggested answer
    Nitin Verma Profile Picture
    21,812 Moderator on at

    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)]

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

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the August Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Small and medium business | Business Central, NAV, RMS

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 720 Super User 2026 Season 2

#2
YUN ZHU Profile Picture

YUN ZHU 436 Super User 2026 Season 2

#3
AndrewThomas81 Profile Picture

AndrewThomas81 409 Super User 2026 Season 2

Last 30 days Overall leaderboard

Featured topics

Microsoft Training Manuals

Product updates

Dynamics 365 release plans