Hello
I have created a C# console application by which I can able to sync data from CRM to Business Central (online). I am using web services to sync data from CRM to BC.
While trying to update the sales order line it was throwing an exception "A transaction must be started before changes can be made to the database.".
Initially, for the first time I can able to update the sales order line but after that, it continuously throws the exception. Even when read the sales order information through web services.
Any suggestions from the community much appreciated.
Now the issue is happening even I am trying to read the sales order using this line
var order = salesOrderService.Read("Sales order No");//read sales order no
Even I can't able to read any sales order using sales order web service.
Again thanks for your feedback.
I can see some problems here. First, this line is not necessary if you don’t update the header:
salesOrderService.Update(ref order);
Then, when you read the Sales Order using the WS you should have also the lines in an array (no need to read them again).
Hi Steffano
Thank you very much for your great support.
After dig down, I find out that my problem started from when
1. Read Sales order
2. Only update the sales order line(no change in sales order)-- Sales order line
Here is my code block
using (var salesOrderService = GenerateBCProxyClass<Sales_Order_Service>(credential))
{
var order = salesOrderService.Read("Sales order No");//read sales order no
var itemId = GetItemId(item, credential);// Read Nav ItemId
var lines = order.SalesLines.ToList(); // Read all the existing sales lines from
lines.Add(new Sales_Order_Line());
order.SalesLines = lines.ToArray();
salesOrderService.Update(ref order); //Update the sales lines to sales order
using (var soLineService = GenerateBCProxyClass<salesDocumentLines_Service>(credential))// sales order line web service
{
var soFilter = new List<salesDocumentLines_Filter> { new salesDocumentLines_Filter { Field = salesDocumentLines_Fields.documentNumber, Criteria = order.No } };
var soLines = soLineService.ReadMultiple(soFilter.ToArray(), "", 100);// read the existing sales order line
if (soLines.Length > 0)
{
var lineToUpdate = soLines[soLines.Length - 1];
lineToUpdate.quantity = quantity;
lineToUpdate.quantitySpecified = true;
lineToUpdate.number = itemId;
lineToUpdate.unitPrice = unitPrice;
lineToUpdate.unitPriceSpecified = true;
soLineService.Update(ref lineToUpdate);//update the sales order line
}
}
}
This kind of error happens if you run report or page without calling COMMIT right before this action. Find the place where you manipulate with the data (insert or modify, probably delete). Consider calling COMMIT after this and before running report or page. I hope you understand that an incorrect call of COMMIT can cause serious proble with your data since changes won't be rollback if your page/report would fail. So be very attentive :)
Good luck!
Can you please post a piece of your C# code that you're using to create the Sales Order (header and lines)?
This is a piece of C# code that works (full source on my GitHub page here github.com/.../NAV-AzureServiceBus):
SalesOrder_Service ws = new SalesOrder_Service();
ws.Url = YourURL;
ws.UseDefaultCredentials = true;
//Create the Sales Header
SalesOrder order = new SalesOrder();
ws.Create(ref order);
//Here the Sales Order is created and we have the order no.
//Update the Sales Header with details
order.Sell_to_Customer_No = YourCustomerNo;
order.Order_Date = YourOrderDate;
int _rows = YourNumberOfRowsToCreate;
if (_rows > 0)
{
//Create the Sales Lines array and initialize the lines
order.SalesLines = new Sales_Order_Line[_rows];
for (int i = 0; i < _rows; i++)
{
order.SalesLines[i] = new Sales_Order_Line();
}
}
ws.Update(ref order);
//Loads the data into the Lines
if (_rows > 0)
{
int rowindex = 0;
for (int rowindex = 0; rowindex < _rows; rowindex++)
{
Sales_Order_Line line = order.SalesLines[rowindex];
line.Type = NAVSalesOrderWS.Type.Item;
line.No = YourItemNo;
line.Quantity = YourQuantity;
rowindex++;
}
//Update the order lines with all the informations
ws.Update(ref order);
}
No custom code (event subscribers) to the Sales header or Sales line table.
Do you have some custom code (event subscribers) to the Sales Header or Sales Line table?
Hi Stefano
Thanks for your comments.
I am using web services SOAP. Yes exposing the Sales Order page.
1. Intially I am able to create Sales Order with sales order line using soap service and c# console app.
2. While trying to update the sales line it was failed.
3. Trying with different solution at last using the sales order line web service(soap) to update the sales order line.(only order line update, sales header as it is)
4. I can able to update sales order line and it was updated successfully.
5. I was creating a d365 crm plugin to sync crm to BC sales order sync. Registered it to the crm.
6. After that, it throws exception "A transaction must be started before changes can be made to the database."
7. Unregister the crm and again trying with the console application now this exception happens even while trying to read sales order via web service.
Quite difficult without seeing the code. Are you using web services (SOAP) or REST APIs? If SOAP ws, are you exposing the Sales Order page?
Stay up to date on forum activity by subscribing. You can also customize your in-app and email Notification settings across all subscriptions.
André Arnaud de Cal... 291,240 Super User 2024 Season 2
Martin Dráb 230,149 Most Valuable Professional
nmaenpaa 101,156