It uses a SQL script to find candidate sales orders, writes to a history custom field. Saves the sales order & moves to the next sales order.
On the save event, I am getting an error "New transaction is not allowed because there are other threads running in the session.".
Version 7.2.1 SR8
I've tried various little code tweaks to sort it out but I can't can't solve it so I suspect it's a bit more esoteric. My code
- Code: Select all
public void Execute(JiwaFinancials.Jiwa.JiwaApplication.Plugin.Plugin Plugin, JiwaFinancials.Jiwa.JiwaApplication.Schedule.Schedule Schedule)
{
lock (JiwaFinancials.Jiwa.JiwaApplication.Manager.CriticalSectionFlag)
{
// place processing code in here
//Find candidate sales orders
LogToEventLog(String.Format("Executing schedule '{0}'", Schedule.Name), System.Diagnostics.EventLogEntryType.Information, Plugin);
SqlDataReader SQLReader = null;
try {
JiwaFinancials.Jiwa.JiwaSales.SalesOrder.SalesOrder salesOrder = Plugin.Manager.BusinessLogicFactory.CreateBusinessLogic<JiwaFinancials.Jiwa.JiwaSales.SalesOrder.SalesOrder>(null);
string SQL = "SELECT TOP (100) PERCENT SO_Main.InvoiceID, SO_Main.InvoiceInitDate, SO_HistoryCustomSettingValues.Contents,";
SQL += " SO_HistoryCustomSettingValues.SettingValueID, SO_HistoryCustomSettingValues.SettingID ";
SQL += " FROM SO_Main INNER JOIN SO_History ON SO_Main.InvoiceID = SO_History.InvoiceID AND SO_Main.CurrentHistoryNo = SO_History.HistoryNo";
SQL += " LEFT OUTER JOIN SO_HistoryCustomSettingValues ON SO_History.InvoiceHistoryID = SO_HistoryCustomSettingValues.HistoryID ";
SQL += " WHERE (SO_Main.DebtorID = '0FC4070CEBCC491CA15A') AND (SO_HistoryCustomSettingValues.Contents <> 'True' OR ";
SQL += " SO_HistoryCustomSettingValues.Contents IS NULL) AND (SO_Main.InvoiceInitDate > CONVERT(DATETIME, '2022-02-27 00:00:00', 102))";
using (SqlCommand SQLCmd = new SqlCommand(SQL, Plugin.Manager.Database.SQLConnection, Plugin.Manager.Database.SQLTransaction)) {
SQLReader = SQLCmd.ExecuteReader();
while (SQLReader.Read())
{
LogToEventLog(String.Format("Exporting invoice '{0}'", (string)Plugin.Manager.Database.Sanitise(SQLReader, "InvoiceID")), System.Diagnostics.EventLogEntryType.Information, Plugin);
salesOrder.Read((string)Plugin.Manager.Database.Sanitise(SQLReader, "InvoiceID"));
JiwaFinancials.Jiwa.JiwaSales.SalesOrder.SalesOrderHistory salesHist = salesOrder.SalesOrderHistorys[salesOrder.CurrentHistoryNo];
salesHist.CustomSettingValues.get_ItemFromSettingName("ScanOrders").Contents = "True";
salesOrder.Save();
LogToEventLog(String.Format("Invoice Saved: '{0}'", salesOrder.InvoiceNo), System.Diagnostics.EventLogEntryType.Information, Plugin);
}
}
}
catch(System.Exception e)
{
LogToEventLog(String.Format("Error '{0}'", e.Message), System.Diagnostics.EventLogEntryType.Information, Plugin);
}
finally {
if (SQLReader != null) {
SQLReader.Close();
}
}
}
}