How to Write to IOrderItem Properties
Posted: Thu Oct 11, 2018 9:43 pm
The Select Order Model uses IOrderItems as the building block for a vast number of object from Policies to Settlement Agents. To the developer, it provides a simple pattern for interaction despite the differences between the complex objects (Policies have Billcodes and Settlement Agents have Logos). In this topic we will discuss writing to IOrderItem properties. Due to length constraints, I have to assume you know a little about IOrderItems. If not, create a new post. Be aware that this topic is the sister to How to Read from IOrderItem Properties.
For this example, I am going to use an invoice that has been previously set up and is ready to send. It currently has a Status of Pending. We'll get the invoice number then change it. Last step is to save the order with its changes.
Typically this is all there is to know. But there are times that order properties are not writable. By setting the invoice status to Sent and saving the order, Select core rules will set the "Locked" aspect on the property to prevent any changes at this time. This is subtly different from the "ReadOnly" aspect that other core rules may set but the net effect is the same - the user (and its proxy the API) may not write to the property.
If you attempt to set the invoice number now that the status is Sent, the code throws an exception: Error - Cannot access member. You can test for properties being Locked/ReadOnly because of the current order state as follows:
Best practice: If in doubt, always test the properties in production code and don't rely on try/catch blocks.
For this example, I am going to use an invoice that has been previously set up and is ready to send. It currently has a Status of Pending. We'll get the invoice number then change it. Last step is to save the order with its changes.
Code: Select all
using (IOrder order = os.OpenOrder(oi, OrderEditMode.ReadWrite))
{
IList invoices = (IList)order.GetProperty("Invoices");
IOrderItem firstInv = (IOrderItem)invoices[0];
// Read current invoice number value.
string initialValue = (string)firstInv.GetProperty("Number");
// Now change it to new value.
firstInv.SetProperty("Number", "22222");
// Save the modified order.
os.ApplyChanges(order);
os.CloseOrder(order);
}
If you attempt to set the invoice number now that the status is Sent, the code throws an exception: Error - Cannot access member. You can test for properties being Locked/ReadOnly because of the current order state as follows:
Code: Select all
if (!(firstInv.GetIsReadOnly("Number") || firstInv.GetIsLocked("Number")))
{
firstInv.SetProperty("Number", "22222");
}