Skip to main content

Notifications

Community site session details

Community site session details

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

Post the sales order as shipped using power automate

(0) ShareShare
ReportReport
Posted on by 558

Hello guys,

     Hope you all are doing good

I have some issues in posting the sales order as shipped(not invoiced) using power automate. I have used the codeunit  Ship-Post(Yes/No). 

I have called the action from Power automate and it runs fine but the order doesn't get posted in Business Central.

The below is my code to post the order

procedure postAsShip(var actionContext: WebServiceActionContext)
begin
    Codeunit.Run(Codeunit::"Ship-Post (Yes/No)");
    actionContext.SetObjectId(Page::"Sales Order");
    actionContext.SetObjectType(ObjectType::Page);
    actionContext.AddEntityKey(Rec.FieldNo(SystemId), Rec.SystemId);
    actionContext.SetResultCode(WebServiceActionResultCode::Updated);
end;

Is there any other way to post the sales order as shipped using codeunit. 

Any help or suggestions on this would be much appreciated.

  • Suggested answer
    Tamilarasu Arunachalam Profile Picture
    558 on at
    RE: Post the sales order as shipped using power automate

     Gianfranco C. ,

    Thanks again! and much appreciation for your suggestion

    It worked as expected

  • Verified answer
    Gianfranco C. Profile Picture
    370 on at
    RE: Post the sales order as shipped using power automate

    hello , I have tried to expose this API page and test it, and it works correctly.

    page 84043 "SalesOrdersCust"
    {
        APIVersion = 'v2.0';
        EntityCaption = 'SalesOrderCust';
        EntitySetCaption = 'SalesOrdersCust';
        ChangeTrackingAllowed = true;
        DelayedInsert = true;
        EntityName = 'salesOrderCust';
        EntitySetName = 'salesOrdersCust';
        ODataKeyFields = Id;
        APIPublisher = 'CR';
        APIGroup = 'CR';
        PageType = API;
        SourceTable = "Sales Order Entity Buffer";
        Extensible = false;
    
        layout
        {
            area(content)
            {
                repeater(Group)
                {
                    field(id; Rec.Id)
                    {
                        Caption = 'Id';
                        Editable = false;
    
                        trigger OnValidate()
                        begin
                            RegisterFieldSet(Rec.FieldNo(Id));
                        end;
                    }
                    field(number; Rec."No.")
                    {
                        Caption = 'No.';
                        Editable = false;
    
                        trigger OnValidate()
                        begin
                            RegisterFieldSet(Rec.FieldNo("No."));
                        end;
                    }
    
                }
            }
        }
    
        actions
        {
        }
    
        trigger OnAfterGetRecord()
        begin
        end;
    
        trigger OnDeleteRecord(): Boolean
        begin
            exit(false);
        end;
    
        trigger OnInsertRecord(BelowxRec: Boolean): Boolean
        begin
    
            exit(false);
        end;
    
        trigger OnModifyRecord(): Boolean
        begin
            if xRec.Id <> Rec.Id then
                Error(CannotChangeIDErr);
    
            GraphMgtSalesOrderBuffer.PropagateOnModify(Rec, TempFieldBuffer);
    
            exit(false);
        end;
    
        trigger OnNewRecord(BelowxRec: Boolean)
        begin
        end;
    
        trigger OnOpenPage()
        begin
        end;
    
        var
            TempFieldBuffer: Record "Field Buffer" temporary;
            GraphMgtSalesOrderBuffer: Codeunit "Graph Mgt - Sales Order Buffer";
            CannotChangeIDErr: Label 'The "id" cannot be changed.', Comment = 'id is a field name and should not be translated.';
            CannotFindOrderErr: Label 'The order cannot be found.';
    
    
        local procedure RegisterFieldSet(FieldNo: Integer)
        var
            LastOrderNo: Integer;
        begin
            LastOrderNo := 1;
            if TempFieldBuffer.FindLast() then
                LastOrderNo := TempFieldBuffer.Order   1;
    
            Clear(TempFieldBuffer);
            TempFieldBuffer.Order := LastOrderNo;
            TempFieldBuffer."Table ID" := Database::"Sales Invoice Entity Aggregate";
            TempFieldBuffer."Field ID" := FieldNo;
            TempFieldBuffer.Insert();
        end;
    
    
        local procedure GetOrder(var SalesHeader: Record "Sales Header")
        begin
            if not SalesHeader.GetBySystemId(Rec.Id) then
                Error(CannotFindOrderErr);
        end;
    
        local procedure PostWithShip(var SalesHeader: Record "Sales Header")
        begin
            SalesHeader.Ship := true;
            SalesHeader.Invoice := false;
            SalesHeader.SendToPosting(Codeunit::"Sales-Post");
        end;
    
        local procedure SetActionResponse(var ActionContext: WebServiceActionContext; PageId: Integer; DocumentId: Guid)
        var
        begin
            ActionContext.SetObjectType(ObjectType::Page);
            ActionContext.SetObjectId(PageId);
            ActionContext.AddEntityKey(Rec.FieldNo(Id), DocumentId);
            ActionContext.SetResultCode(WebServiceActionResultCode::Updated);
        end;
    
        [ServiceEnabled]
        [Scope('Cloud')]
        procedure Ship(var ActionContext: WebServiceActionContext)
        var
            SalesHeader: Record "Sales Header";
        begin
            GetOrder(SalesHeader);
            PostWithShip(SalesHeader);
            SetActionResponse(ActionContext, Page::"Sales Order", Rec.SystemId);
        end;
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    


    A registered sales shipment is generated.
    I suggest you do a registration preview on the order you are testing, to check if there are any error messages.

    Your error would seem to be triggered in codeunit 21 in the RunCheck.


    The flow set up is very simple:

    pastedimage1680884030417v1.png

    let me know if you solve it

  • Suggested answer
    Tamilarasu Arunachalam Profile Picture
    558 on at
    RE: Post the sales order as shipped using power automate

    Hi Gianfranco C. ,

    Thanks for your answer

    I have tried your code, but i've got the below error

    pastedimage1680879157182v1.png

    Is there anything i want to change in code or in configuration?

  • Suggested answer
    Gianfranco C. Profile Picture
    370 on at
    RE: Post the sales order as shipped using power automate

    hi,  try this code :

     procedure Ship(var ActionContext: WebServiceActionContext)
        var
            SalesHeader: Record "Sales Header";
        begin
            GetOrder(SalesHeader);
            PostWithShip(SalesHeader);
            SetActionResponse(ActionContext, Page::"Sales Order", Rec.SystemId);
        end;
    
        local procedure GetOrder(var SalesHeader: Record "Sales Header")
        var
            CannotFindOrderErr: Label 'The order cannot be found.';
        begin
            if not SalesHeader.GetBySystemId(Rec.SystemId) then
                Error(CannotFindOrderErr);
        end;
    
        local procedure PostWithShip(var SalesHeader: Record "Sales Header")
        var
            LinesInstructionMgt: Codeunit "Lines Instruction Mgt.";
            OrderNo: Code[20];
            OrderNoSeries: Code[20];
        begin
            LinesInstructionMgt.SalesCheckAllLinesHaveQuantityAssigned(SalesHeader);
            OrderNo := SalesHeader."No.";
            OrderNoSeries := SalesHeader."No. Series";
            SalesHeader.Ship := true;
            SalesHeader.Invoice := false;  //(check if is optional)
            SalesHeader.SendToPosting(Codeunit::"Sales-Post");
        end;
    
        local procedure SetActionResponse(var ActionContext: WebServiceActionContext; PageId: Integer; DocumentId: Guid)
        begin
            ActionContext.SetObjectType(ObjectType::Page);
            ActionContext.SetObjectId(Page::"Sales Order");
            ActionContext.AddEntityKey(Rec.FieldNo(SystemId), Rec.SystemId);
            ActionContext.SetResultCode(WebServiceActionResultCode::Updated);
        end;

    Don't forget to help the community by Verifying the answer and Like it if your question has been answered. It will let others know that the topic has verified answer.

    Thanks & Regards, Gian

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

Jainam Kothari – Community Spotlight

We are honored to recognize Jainam Kothari as our June 2025 Community…

Congratulations to the May Top 10 Community Leaders!

These are the community rock stars!

Announcing the Engage with the Community forum!

This forum is your space to connect, share, and grow!

Leaderboard >

Featured topics

Product updates

Dynamics 365 release plans