Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics NAV (Archived)

Invoice Discount error when creating Sales Order through web services

Posted on by 2,983

So I'm creating a a SalesOrder with some Lines using the SOAP Page type web service. I get "'You cannot make any changes to the database until a Transaction has been started'."

This is my code in C#

public async Task<Create_Result> Post([FromBody] SalesHeader data)
        {
            SalesOrder_PortClient order = new SalesOrder_PortClient();
            SalesOrder SalesOrder = new SalesOrder();
            
            SalesOrder.Sell_to_Customer_No = data.CustNo;

            int i = 0;

            SalesOrder.SalesLines = new Sales_Order_Line[data.SalesLines.Length]; 

            foreach (SalesLine l in data.SalesLines)
            {
                SalesOrder.SalesLines[i] = new Sales_Order_Line();           
                SalesOrder.SalesLines[i].Type = NavSalesOrderService.Type.Item;
                SalesOrder.SalesLines[i].TypeSpecified = true;
                SalesOrder.SalesLines[i].No = l.itemno;
                SalesOrder.SalesLines[i].Quantity = l.qty;
                SalesOrder.SalesLines[i].QuantitySpecified = true;
                SalesOrder.SalesLines[i].Location_Code = l.location;
                i++;
            }

            Create c = new Create();
            c.SalesOrder = SalesOrder;
            
            Create_Result res = new Create_Result();
            res = await order.CreateAsync(c);

            return res;
           
        }


So I create header and lines in  single call. The problem I have is with the Invoice Discount. 

In Sales Order Subform we have 

OnAfterGetCurrRecord()
CalculateTotals;

This takes us to 

[External] CalcInvDisc()
SalesCalcDiscount.CalculateInvoiceDiscountOnLine(Rec);

And finally to:

LOCAL CalculateInvoiceDiscount(VAR SalesHeader : Record "Sales Header";VAR SalesLine2 : Record "Sales Line")
SalesSetup.GET;
IF UpdateHeader THEN
  SalesHeader.FIND; // To ensure we have the latest - otherwise update fails.
OnBeforeCalcSalesDiscount(SalesHeader);

WITH SalesLine DO BEGIN
  LOCKTABLE; //THIS LINE THROWS TRANSACTION ERROR <<<<<------------------------------------------------!!!!!
  SalesHeader.TESTFIELD("Customer Posting Group");
  CustPostingGr.GET(SalesHeader."Customer Posting Group");

  SalesLine2.RESET;
  SalesLine2.SETRANGE("Document Type","Document Type");
  SalesLine2.SETRANGE("Document No.","Document No.");
  SalesLine2.SETRANGE("System-Created Entry",TRUE);
  SalesLine2.SETRANGE(Type,SalesLine2.Type::"G/L Account");
  SalesLine2.SETRANGE("No.",CustPostingGr."Service Charge Acc.");
  IF SalesLine2.FINDSET(TRUE,FALSE) THEN
    REPEAT

Any one got this error before. Any solutions?

'You cannot make any changes to the database until a Transaction has been started'.

*This post is locked for comments

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!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans