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 :
Business Central forum
Under review by Community Managers

Under review

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

Page 42 SalesOrder Webservice – ‘A transaction must be started before changes can be made to the database’

Posted on by

We are an ISV developing an extension that needs to post Sales Orders into Business Central.  Research on the web suggests the best method is to use the Page 42 (Hitch Hikers Guide to the Galaxy?) and to mimic the page logic by creating the record and then updating it with details and lines.  However, when I connect to the page I get “- A transaction must be started before changes can be made to the database”.   Most of the stuff on the Web seems to refer to older versions of NAV.

I have tried using the API but I get the same error as I suspect it is using Page 42 under the hood.  The API is not ideal for me as it will not expose my Table and Page Extension which the Page 42 Web Service does.

I have started developing a CodeUnit that takes  Json Text is input and builds the order using the “Sales Header” and “Sales Line” Records. This works but none of the calculated fields populate so I am bypassing all the business logic in the Sales Order Page.  I could work away and replicate the bits that I need but I do not think this is a good route as it will isolate my extension from updates of the core product.

Is there a ‘proper way’ of doing this or is there a bug in page 42?

Below is the code I am using on a Business Central Online Sandbox Version 15.1.37881.38071.  The web service classes were generated from the Visual Studio Connected Service Wizard.  I am connecting to the SOAP service. The error occurs if I set no properties on the order, set eh customer and date or write more code to add lines etc.  Interestingly if I use a customer that is ‘Blocked’ I get a validation error so the problem is deeper in the Page than just calling it.

public void PlaceOrderWS()

        {

            global::BC365.SalesOrderWS.SalesOrderHeader order = new global::BC365.SalesOrderWS.SalesOrderHeader();

            order.Sell_to_Customer_No = "20000";

            order.Order_Date = DateTime.Now;

 

            // Create client with Basic authentication

            global::BC365.SalesOrderWS.SalesOrderHeader_PortClient client = new global::BC365.SalesOrderWS.SalesOrderHeader_PortClient();

 

            //Fix Visual Studio Generation that sets HttpClientCredentialType to None

            BasicHttpBinding binding = client.Endpoint.Binding as BasicHttpBinding;

            binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;

 

            SetBasicCredentials(client.ClientCredentials);

 

            global::BC365.SalesOrderWS.Create request = new global::BC365.SalesOrderWS.Create(order);

            global::BC365.SalesOrderWS.Create_Result result = client.CreateAsync(request).Result;

 

            Console.WriteLine(result.SalesOrderHeader.No);

        }

Here is the CodeUnit that I would rather not develop further

codeunit 50141 "Shopfront Sales Order"
{
    procedure PostSalesOrder(var SalesOrder: Text): Text

    var
        Result: Text[20];

        JOrder: JsonObject;
        JT: JsonToken;
        JLines: JsonArray;
        JLine: JsonObject;

        SHdR: Record "Sales Header";
        SLnR: Record "Sales Line";
        SSetUp: Record "Sales & Receivables Setup";

        LineNo: Integer;
        DocumentType: Text;

    begin
        //Initialise Setup data
        SSetUp.FindFirst();

        //Get the order data
        if not JOrder.ReadFrom(SalesOrder) then
            Error('The order was no valid Json\\ %1\', SalesOrder);

        SHdR.Init();

        DocumentType := GetJsonToken(JOrder, 'DocumentType').AsValue().AsText();
        case DocumentType of
            'Quote':
                begin
                    SHdR."No." := NoManager.GetNextNo(SSetUp."Quote Nos.", Today(), true);
                    SHdR."Document Type" := SHdR."Document Type"::Quote;
                end;
            else begin
                    //Assume Sales Order
                    SHdR."No." := NoManager.GetNextNo(SSetUp."Order Nos.", Today(), true);
                    SHdR."Document Type" := SHdR."Document Type"::Order;
                end;
        end;

        SHdR."Sell-to Customer No." := GetJsonToken(JOrder, 'Account').AsValue().AsText();
        SHdR."Sell-to Customer Name" := GetJsonToken(JOrder, 'AccountName').AsValue().AsText();
        SHdR."Currency Code" := GetJsonToken(JOrder, 'Currency').AsValue().AsText();

        SHdR."Order Date" := Today(); //GetJsonToken(JOrder, 'OrderDate').AsValue().AsDate();
        SHdR."Document Date" := Today();
        SHdR."Quote Valid Until Date" := Today();
        SHdR.Insert();


        JLines := GetJsonToken(JOrder, 'Lines').AsArray();

        LineNo := 0;
        foreach JT in JLines do begin
            JLine := JT.AsObject();
            PostSalesLine(JLine, SHdR."No.", SHdR."Sell-to Customer No.", DocumentType, LineNo);
            LineNo := LineNo + 1;
        end;


        exit(SHdR."No.");

    end;

    local procedure PostSalesLine(JLine: JsonObject; OrderNo: Text; CustomerNo: Text; DocumentType: Text; LineNo: Integer)

    var

        SLine: Record "Sales Line";
    begin
        SLine.Init();

        case DocumentType of
            'Quote':
                begin
                    SLine."Document Type" := SLine."Document Type"::Quote;
                end;
            else begin
                    //Assume Sales Order
                    SLine."Document Type" := SLine."Document Type"::Order;
                end;
        end;

        Sline."Document No." := OrderNo;
        Sline."Line No." := LineNo;
        SLine."Sell-to Customer No." := CustomerNo;
        Sline.Type := Sline.Type::Item;//   GetJsonToken(JLine, 'Type').AsValue().AsOption();
        Sline."No." := GetJsonToken(JLine, 'StockCode').AsValue().AsText();
        Sline.Description := GetJsonToken(JLine, 'Description').AsValue().AsText();
        Sline.Quantity := GetJsonToken(JLine, 'Quantity').AsValue().AsDecimal();
        Sline."Unit Price" := GetJsonToken(JLine, 'UnitPrice').AsValue().AsDecimal();

        Sline."Requested Delivery Date" := ToDay();
        Sline.Insert();

    end;







    local procedure GetJsonToken(JO: JsonObject; TokenKey: Text): JsonToken

    var
        JT: JsonToken;

    begin
        if not JO.Get(TokenKey, JT) then
            Error('Could not find a token with key %1', TokenKey);

        exit(JT);

    end;

    local procedure GetSelectToken(JO: JsonObject; Path: Text): JsonToken

    var
        JT: JsonToken;

    begin
        if not JO.SelectToken(Path, JT) then
            Error('Could not find a token with path %1', Path);

        exit(JT);

    end;

    var
        NoManager: Codeunit NoSeriesManagement;
}

Helpful resources

Quick Links

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Coming on 11/8!

In our never-ending quest to help the Dynamics 365 Community members get answers faster …

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard > Business Central

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans