Skip to main content

Notifications

Small and medium business | Business Central, N...
Answered

Add button that validates each line No. in Sales Order Subform

Posted on by 221
Hello!
I am currently working with a case where we import Sales Lines from a PDF. Everything works great, but the description on the line is in a different language, so we want to change it to the default language.
 
Every line No. is of course connected to a product in Business Central.
 
The tedious way of doing it, is to copy the line No. and insert it again for each line. When the No. is inserted again, it adds all the other information automatically, including description in the correct language.
 
Therefor, I want to add a button. Every time that button is pressed, I want to go through each of the Sales Lines in that order, and I want to insert the No. again. I suppose it is the Validating part that is crucial to auto-insert every other piece of information.
 
I have tried this, but it does not seem to do anything, other than displaying the message at the end:
 
pageextension 50108 SalesOrderSubformExt extends /Sales Order Subform/
{
    actions
    {
        addlast(/O&rder/)
        {
            action(ValidateNo)
            {
                ApplicationArea = All;
                Caption = 'Validate No.';
                trigger OnAction()
                var
                    SalesOrderLine: Record /Sales Line/;
                begin
                    SalesOrderLine.SetRange(/Document Type/, Rec./Document Type/);
                    SalesOrderLine.SetRange(/Document No./, Rec./No./);
                    if SalesOrderLine.FindSet() then
                        repeat
                            SalesOrderLine.Validate(/No./);
                        until SalesOrderLine.Next() = 0;
                    Message('All lines have been validated.');
                end;
            }
        }
    }
}

 
 
How can I modify my code to achieve the result I want?
Categories:
  • Suggested answer
    Hagru Profile Picture
    Hagru 221 on at
    Add button that validates each line No. in Sales Order Subform
    Hello!
    I agree that it would be a better solution to do it automatically, but the customer wanted a button to do it manually. I will suggest the automatic option as well.
    I am still fairly new to AL programming, but I unlocked some understanding by working on this case. Once I figured out the SetRange() method and how the FindSet() method worked, the rest was fairly easy, which I will admit, this case is beginner level.
     
    This is how I solved it:
     
     
    Thought it was best to just validate the No. as all the other information is stored on the Item No. anyways. Validating it inserts all the other values correctly, which is important if the PDF import fails to get some information.
     
    Thank you for your suggestions and help!
  • Suggested answer
    YUN ZHU Profile Picture
    YUN ZHU 73,472 Super User 2024 Season 2 on at
    Add button that validates each line No. in Sales Order Subform
    Hi, if you just want to update the Description in the lines, try the following approach.
     
    pageextension 50108 SalesOrderSubformExt extends "Sales Order Subform"
    {
        actions
        {
            addlast("O&rder")
            {
                action(ValidateNo)
                {
                    ApplicationArea = All;
                    Caption = 'Validate No.';
                    trigger OnAction()
                    var
                        SalesOrderLine: Record "Sales Line";
                        Item: Record Item;
                    begin
                        SalesOrderLine.SetRange("Document Type", Rec."Document Type");
                        SalesOrderLine.SetRange("Document No.", Rec."No.");
                        if SalesOrderLine.FindSet() then
                            repeat
                                if Item.Get(SalesOrderLine."No.") then begin
                                    SalesOrderLine.Validate(Description, Item.Description);
                                    SalesOrderLine.Modify(true);
                                end;
                            until SalesOrderLine.Next() = 0;
                        Message('All lines have been validated.');
                    end;
                }
            }
        }
    }
    Thanks..
    ZHU
  • Suggested answer
    Dustin Wolf Profile Picture
    Dustin Wolf 30 on at
    Add button that validates each line No. in Sales Order Subform
    Hey ChristianPIT,

    I personally wouldnt add a button.  I would instead add a step to the OnInsert function.  Here is a snippet:
     
        [EventSubscriber(ObjectType::Table, Database::"Sales Line", 'OnBeforeInsertEvent', '', false, false)]
        local procedure SalesLineOnBeforeInsertUpdateDescription(var Rec: Record "Sales Line"; RunTrigger: Boolean)
        var
            Item: Record Item;
        begin
            Case Rec.Type of
                Rec.Type::Item:
                    begin
                        if Item.Get(Rec."No.") then begin
                            Rec.Description := Item.Description;
                        end;
                    end;
                    /* additional cases */
            end;
        end;
     
    This way it should happen automatically when each sales line is created.
     
     
    - Dustin Wolf
     
  • Suggested answer
    gdrenteria Profile Picture
    gdrenteria 12,167 Most Valuable Professional on at
    Add button that validates each line No. in Sales Order Subform
    Hi
    I would tell you that you should identify which validation the update of the description field is in, to execute it again.
    Within the repeat you should execute said code, and then modify it.
    Best Regards
    Gerardo

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,240 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,149 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans