Creating an order
Posted: Wed Jan 17, 2024 6:25 pm
I'm trying to create the most basic of orders as a starter. I've taken a few examples from the forum here but am running into a null exception error. Any thoughts?
Free support for custom code within SoftPro's products, monitored by SoftPro employees with a 24-48 hr expected turn time on business days.
https://devforum.softprocorp.com/
Code: Select all
using SoftPro.Accounting.Client;
using SoftPro.ClientModel;
using SoftPro.OrderTracking.Client;
using SoftPro.OrderTracking.Client.Orders;
using SoftPro.Select.Client;
using SoftPro.Select.Service;
using SoftPro.ServerModel;
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Runtime.InteropServices;
using SoftPro.Select.Service.Runtime;
using SoftPro.Select.Client.Scheduler;
using SoftPro.Select.Client.Runtime;
using System.Linq;
using System.Net;
namespace XXXXXX
{
[Guid("3c691408-fd01-45a2-9945-6831b0358c96")]
partial class XXXXXX: Package
{
// This method is called when the package is loaded by the server.
protected override void OnInitialize()
{
// Listen to the environment started event.
ISelectServerEnvironment environment = GetService<ISelectServerEnvironment>();
environment.Started += SelectServerEnvironment_Started;
}
// This method is called when the server is finished loading packages and services.
private void SelectServerEnvironment_Started(object sender, System.EventArgs e)
{
// Get services here because they are guaranteed to be loaded.
// Listen to the order saving event.
IOrderStore os = GetService<IOrderStore>();
if (os == null)
{
System.Diagnostics.Debug.WriteLine("os is null");
} else
{
//This is what I'm getting - os is not null
System.Diagnostics.Debug.WriteLine("os is not null");
}
OrderCreationSpec spec = new OrderCreationSpec();
spec.SettlementType = SettlementType.CDF;
spec.BaseNumber = "Test-007";
//Code breaks here
IOrder order = os.NewOrder(spec);
// Cast to dynamic to make order handling easier.
dynamic o = (dynamic)order;
// Set a property.
o.Project = "New Deal";
// Save the order in the database.
os.ApplyChanges(order);
// if I uncomment the following line the OrderStore_OrderSaving method works
//os.OrderSaving += OrderStore_OrderSaving;
// Unhook this event as it is no longer needed.
ISelectServerEnvironment environment = GetService<ISelectServerEnvironment>();
environment.Started -= SelectServerEnvironment_Started;
}
// This method is called when an order is being saved.
private void OrderStore_OrderSaving(object sender, OrderSavingEventArgs e)
{
// Get the order as a dynamic object to make programming easier.
dynamic order = e.Order;
string salesPrice = Convert.ToString(order.SalesContract.SalesPrice);
System.Diagnostics.Debug.WriteLine("an order is saving");
System.Diagnostics.Debug.WriteLine(salesPrice);
}
}
}