This page is designed to provide examples of using .NET features in IronPython when creating Softpro Select content. The comprehensive example is here but I’ll provide examples in a form you are more familiar with.
In all the code below, “order” is always the top level order object. For instance, in a custom order rule (COR), it is the same as:
order = args.Context.Root
Get/Set Properties
It is easy to get or set property values from order model objects by appending a dot then the property name.
# Get OrderIdentifier object from order oi = order.Identifier # Set Project field for order. order.Project = 'hello'
Methods
It is similarly easy to use methods (functions) that are exposed by order objects.
# Get all the Note objects for the order. notes = order.GetAllNotes()
Working with .NET Interfaces
Interfaces are typically used as a hint to the programmer that she should not make assumptions about how the defined methods work and treat them as a black-box. The interface is a contract to the programmer that the signature and function of the method/property will not change but the algorithm inside the interface may change.
Get/Set Interface Properties
Accessing properties on interfaces may require extra typing – you can’t directly access them like non-interface properties above. First you have to get the interface object then you append GetValue or SetValue to the desired interface property so you can pass the interface object.
The top level order object (order – the object we have been using all along on this page), supports many interfaces. One of them is IOrder. If we look up IOrder in the SoftPro Select SDK documentation, we will see that one of the properties on the interface is Attachments. This object provides the top level folder holding containing attachments and folders.
# IOrder is the interface to use and append GetValue with the object # that supports IOrder. topFolder = IOrder.Attachments.GetValue(order) # We can get any interface property. oi = IOrder.Identifier.GetValue(order)
We can also get Custom Fields using the IOrderItem interface. This interface is supported by many items in the order model such as policies, invoices, contacts, etc. The order object also supports this interface. If you look up the property in the SDK, you will see “GetProperty(string property)”. As before, we have to insert the object that implement IOrderItem as THE FIRST PARAMETER in the list of arguments. The SDK will not show this fact since it was not created for IronPython – you just have to know to do it.
Call Interface Methods
# Get custom field created with Order as context. signorStatus = IOrderItem.GetProperty(order, 'SignorStatus##')
How about we give an example of having to get the interface from Select. In this example of getting the active profile, we have to get the profile manager from Select. The COR will write the path to the Related Orders file.
Lines 1-4 are standard COR boilerplate. Lines 6 is added to allow IronPython to find definitions for IProfileManager (from SDK documentation). We get the IProfileManager interface object in line 9. Line 10 shows the same pattern as before for getting a value from an interface. Finally line 11 sets the order field to the profile path.
from System import * from SoftPro.ClientModel import * from SoftPro.OrderTracking.Client import * from SoftPro.OrderTracking.Client.Orders import * from SoftPro.Select.Client.Profiles import * def Order_RelatedOrders_Value(args): profileMgr = args.GetService(IProfileManager) profile = IProfileManager.ActiveProfile.GetValue(profileMgr) args.Value = profile.Path
In case you are curious, if you had entered…
args.Value = profileMgr.ActiveProfile
…on line 10, you would have gotten an error in the order Errors and Warning panel saying: Error encountered during rule execution. ‘ProfileManager’ object has no attribute ‘ActiveProfile’. Be aware that Python refers to properties as “attributes”.
And finally an example of how to set interface properties. Set value actions follow the same pattern as get values actions – get the interface object if you need to then set the value.
# Set custom field created with Order as context. signorStatus = IOrderItem.SetProperty(order, 'SignorStatus##', 'newvalue')
LINQ
We can even use .NET LINQ commands.
# Add LINQ support. import clr import System clr.AddReference('System.Core') clr.ImportExtensions(System.Linq) acctMgr = args.Context.GetService(IAccountsManager) ta = (IAccountsManager.TrustAccounts.GetValue(acctMgr) .Where(lambda t: t.Code == args.Context.EmailSubjectLine and t.Enabled == True) .FirstOrDefault())