Using the Select Automation with Python Code Snippets

Select allows you to supplement the “canned” conditions and activities for Automation with Python code. The object passed to the code (context) depends on how the automation task is coded. The context can come from at least five sources.

Python Hooks in Automation

Automation can host Python snippets in both the condition and activity areas. The way automation works is that when the automation process is triggered by meeting the “Start the process when” criteria, the conditions are tested to determine if the activity (automation task job) should be queued. If so, the job will run when it is allowed (such as when the order is closed for “when order is saved”). In all cases, the Context is the same for the condition and the activity.

As a helpful service, when you select “code snippet”, Select will provide example snippets as a guide. Currently the example code assumes you are using the “When order is saved”. But they are useful for all criteria showing proper case spelling of the Context object. Examples are below with the commenting removed.

# Example Automation Condition snippet
from System import *
from SoftPro.ClientModel import *
from SoftPro.Select.Client import *

def MyFunc():
	return Context.Project == 'SoftPro Rocks!'

MyFunc()
# Example Automation Task snippet
from System import *
from SoftPro.ClientModel import *
from SoftPro.Select.Client import *

Context.Project = 'SoftPro Rocks!'

Trigger 1: Order is Saved – Context: IOrder

This is the most straightforward automation context source as the context is the IOrder of the order that was saved.

# Condition code
from System import *
from SoftPro.ClientModel import *
from SoftPro.Select.Client import *
from SoftPro.OrderTracking.Client.Orders import *
import clr
import System.Diagnostics as SD

def MyFunc():
	s = 'Context: %s' % str(Context)
	s += '\nContext type: %s' % type(Context).__name__	
	s += '\nIs IOrder? %s' % str(isinstance(Context, IOrder))

	SD.Trace.TraceWarning(s)
	return True

MyFunc()

Results:

Context: Order XAT20000008
Context type: Order
Is IOrder? True

Below are the available Context properties:

Property : (Type)Description
Classification : Guidtypeof(IOrder).GUID
Correlation : Guidorder.Guid
Context : objectIOrder object
Moniker : string$”Orders/{order.Guid.ToString()}”
Name : stringorder[“Number”].ToString()

Trigger 2: Document is Attached – Context: IAttachmentFile

In this example, I attached a document (ie.css) to the attachment folder “Attachments\One\Two”. Below is the code condition snippet and the results. The Context will be the file that was attached. If two files are attached then the order is saved, these code snippets will be called twice. Once for each separate attached file – you will never handle two files at once.

from System import *
from SoftPro.ClientModel import *
from SoftPro.Select.Client import *
import clr
import System.Diagnostics as SD

def MyFunc():
	s = 'Context: %s' % str(Context)
	s += '\nContext type: %s' % type(Context).__name__
	s += '\nContext.Name: %s' % Context.Name
	s += '\nContext.Parent.Name: %s' % Context.Parent.Name
	s += '\nContext.Parent.Parent.Name: %s' % Context.Parent.Parent.Name
	s += '\nContext.Parent.Parent.Parent.Name: %s' % Context.Parent.Parent.Parent.Name
	s += '\nDescription: %s' % Context.Description
	s += '\nExtension: %s' % Context.Extension
	s += '\nID: %s' % Context.ID
	s += '\nPath: %s' % Context.Path
	s += '\nTags: %s' % len(Context.Tags)
	s += '\nOrder.Number: %s' % Context.Order.Number

	SD.Trace.TraceWarning(s)
	return True

MyFunc()

Results:

Context:              Attachments\One\Two\ie
Context type:         AttachmentFile
Context.Name:         ie
Context.Parent.Name:  Two
Context.Parent.Parent.Name:         One
Context.Parent.Parent.Parent.Name:  Attachments
Context.Description:  None
Context.Extension:    .css
Context.ID:           {order Guid}
Context.Path:         \XAT20000008\Attachments\One\Two\ie.css
len(Context.Tags):    0
Context.Order.Number: XAT20000008

Below are the available Context properties:

Property : (Type)Description
Classification : Guidtypeof(IAttachmentFile).GUID
Correlation : Guidtask.Order.Guid
Context : objectIAttachmentFile object
Moniker : string$”Orders/{file.Order.Guid}/Attachments/{file.ID}”
Name : string$”({task.Order[“Number”]}) {task[“Description”]}”

Trigger 3: Specific Day and Time Occurs – Context: IScheduledTrigger

There is not much useful information to be gleaned from this Context. Here is what you can access.

Property : (Type)Description
Classification : Guidtypeof(IScheduledTrigger).GUID
Correlation : Guid{00000000-0000-0000-0000-000000000000}
Context : objectIScheduledTrigger object
Moniker : string$”Scheduled/{scheduledTrigger.ID}”
Name : string“Scheduled”

Trigger 4: When Task is Added or Updated – Context: ITask (ChecklistTask or RequestedTask)

The Context of these snippets depend on which type of task is added/updated. The most efficient type of test is to look for a unique property. Don’t do a string comparison of the task type and your desired type name since string comparisons are much slower.

from System import *
from SoftPro.ClientModel import *
from SoftPro.Select.Client import *
from SoftPro.OrderTracking.Client.Orders import *
import clr
import System.Diagnostics as SD

def MyFunc():
    s = 'Context: %s' % str(Context)
    s += '\nContext type: %s' % type(Context).__name__
    s += '\nIs RequestedTask? %s' % str(IOrderItem.HasProperty(Context, 'RequestDueDate'))

    SD.Trace.TraceWarning(s)
    return True
    
MyFunc()

Results from Checklist Task:

Context:           Flood Insurance Policy (task name)
Context type:      ChecklistTask
Is RequestedTask?  False

Results from Requested Task:

Context:           Title Exam (task name)
Context type:      RequestedTask
Is RequestedTask?  True

Below are the available Context properties:

Property : (Type)Description
Classification : Guidtypeof(ITask).GUID
Correlation : Guidtask.Order.Guid
Context : objectITask object
Moniker : string$”Orders/{task.Order.Guid}/Tasks/{task.Guid}”
Name : string$”({task.Order[“Number”]}) {task[“Description”]}”

Trigger 5: 360 Transaction is Updated – Context: IProductTransaction

I never deal with SoftPro 360. However this might be helpful if you do. Below are the available Context properties:

Property : (Type)Description
Classification : Guidtypeof(IProductTransaction).GUID
Correlation : Guid?
Context : objectIProductTransaction object
Moniker : string$”ProductTransactions/{IProductTransaction.TransactionNumber}”
Name : string$”({IProductTransaction.LinkedOrder}) {IProductTransaction.TransactionNumber}”