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

Community site session details

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

Sales Lines automation-Power Automate

(2) ShareShare
ReportReport
Posted on by 4
Hi Community,

I’m working on automating a process in Business Central, and I need some guidance on how to set this up effectively. Here's the scenario:

When a user inserts the "Reference N" in a Sales Order line , I want a workflow to be triggered automatically.

This workflow should go to a Reference Table, find all items that match the same "Reference N", and identify the item with the highest Qty (We have already added this information to reference table) .

Once the item with the highest Qty is identified, it should be automatically added to the Sales Line of the same Sales Order.

The entire process should run automatically without needing any manual trigger.

I’m considering using Power Automate, but I’m not sure if it’s capable of handling this process entirely
I have the same question (0)
  • Suggested answer
    YUN ZHU Profile Picture
    90,561 Super User 2025 Season 2 on at
    Sales Lines automation-Power Automate
    Hi, this should be possible using Business events in Power Automate.
    Dynamics 365 Business Central: OnValidate (Field) Trigger in Power Automate (Trigger a flow when a field is modified) – Business events
     
    Hope this helps.
    Thanks.
    ZHU
  • Suggested answer
    Suresh Kulla Profile Picture
    49,410 Super User 2025 Season 2 on at
    Sales Lines automation-Power Automate
    Understanding the technology and solutions necessary to meet business requirements is crucial. In this case, Power Automate may not be the best option; it can be addressed with minimal customization if you have experience as an AL Developer. Additionally, your requirements seem unclear. If you define a reference number for an item, you cannot use the same reference number if you are using the same Unit of Measure (UOM) or reference type. I'm unsure why an item would have the same reference number in that table.

    It should be straightforward: if you enter the item reference number on the sales order line, it should retrieve the original item and create the lines in standard Business Central (BC).

    Please clarify your requirements.
  • Verified answer
    CU03020606-0 Profile Picture
    27 on at
    Sales Lines automation-Power Automate

    Yes, it can be done easily if you are not considering using Power Automate. You will need to customize the code. Here is a step-by-step process:

    1. Use an Event Subscriber for the OnInsertRecordEvent of the Sales Order Subform page inside a codeunit.
    2. In this Event Subscriber, check the condition for the inserted line (if the line is Reference N).
    3. If the condition is met, write code to look for the item in your reference table.
    4. After finding the record with the highest quantity, insert the item into the sales line.
    5. Delete the current line.

    Note: Use the Sales Order Subform page and not the Sales Line table directly, otherwise, various looping issues may occur.

    I have added reference code if it can help

    codeunit 50100 "Sales Line Insert Handler"
    {
        [EventSubscriber(ObjectType::Page, Page::"Sales Order Subform", 'OnInsertRecordEvent', '', false, false)]
        local procedure OnInsertSalesLine(var Rec: Record "Sales Line"; var xRec: Record "Sales Line"; CurrPageEditMode: Boolean)
        var
            RefTable: Record "Your Reference Table"; // Replace with actual table name
            HighestQtyItem: Code[20];
            HighestQty: Decimal;
            SalesLine: Record "Sales Line";
        begin
            if Rec."Your Condition Field" = 'Reference N' then begin
                HighestQty := 0;
                // Find the item with the highest quantity in the reference table
                if RefTable.FindSet() then
                    repeat
                        if RefTable.Quantity > HighestQty then begin
                            HighestQty := RefTable.Quantity;
                            HighestQtyItem := RefTable."Item No.";
                        end;
                    until RefTable.Next() = 0;
                if HighestQtyItem <> '' then begin
                    // Insert new sales line with the highest quantity item
                    SalesLine.Init();
                    SalesLine."Document Type" := Rec."Document Type";
                    SalesLine."Document No." := Rec."Document No.";
                    SalesLine."Line No." := Rec."Line No." + 10000; // Adjust the Line No. accordingly
                    SalesLine."No." := HighestQtyItem;
                    SalesLine.Quantity := HighestQty;
                    SalesLine.Insert();
                    // Delete the original line
                    Rec.Delete();
                end;
            end;
        end;
    }
     

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Andrés Arias – Community Spotlight

We are honored to recognize Andrés Arias as our Community Spotlight honoree for…

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

#1
Sumit Singh Profile Picture

Sumit Singh 2,727

#2
Sohail Ahmed Profile Picture

Sohail Ahmed 2,681 Super User 2025 Season 2

#3
YUN ZHU Profile Picture

YUN ZHU 2,288 Super User 2025 Season 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans