I believe the problems you were having were having relates to combining Python libraries and .NET ones. I prefer to stay with the .NET ones since I am more familiar with them - but both are fine if you stay consistent. You were very close to working code.
Code: Select all
# Exit if there are no tasks in order.
checklistTasks = args.Context.Root.ChecklistTasks
if not checklistTasks:
args.Value = str('No checklist tasks')
return over14Days
# Exit if ANY task has the Code of 'pmtovr14'.
if any((task.Code is not None and task.Code.lower()) == 'pmtovr14' for task in checklistTasks):
args.Value = str('Found task code: pmtovr14')
return str(over14Days)
The first section says if there are no checklist tasks, then exit now. This makes the COR faster and the UI more responsive. The second section makes sure the task.Code is specified before trying to convert it to lower case. If Code is None (empty), you can't call "tolower()" on it. We need to do that any time a string might not be set.
The entire snippet is below:
Code: Select all
from System import *
from SoftPro.ClientModel import *
from SoftPro.OrderTracking.Client import *
from SoftPro.OrderTracking.Client.Orders import *
def Order_RelatedOrders_Value(args):
over14Days = False
# Exit if there are no tasks in order.
checklistTasks = args.Context.Root.ChecklistTasks
if not checklistTasks:
args.Value = str('No checklist tasks')
return over14Days
# Exit if ANY task has the Code of 'pmtovr14'.
if any((task.Code is not None and task.Code.lower()) == 'pmtovr14' for task in checklistTasks):
args.Value = str('Found task code: pmtovr14')
return str(over14Days)
# Found task(s) that need work.
for task in checklistTasks:
if (task.Code is not None
and task.Code.lower() == 'revdlvrpol'
and task.Status == TaskStatusType.Completed):
today = DateTime.Now
completedDate = task.CompletedDate
duration = today - completedDate # Add/Subtraction of dates creates a .NET TimeSpan object.
if duration.Days > 14:
args.Value = 'Day: ' + str(duration.Days)
over14Days = True
args.Value = 'Exit: ' + str(over14Days)
return str(over14Days)
Notice that I removed the Python date library and am using the .NET version
System.DateTime that is imported in the first line of the snippet. When two DateTime objects are added or subtracted, a
System.TimeSpan is created to hold the new value. The classes have lots of properties and methods (functions) that are really useful.
As additional debugging help, I often return a string to the COR value so I can see how far in the code I got with unique strings to verify logic. Test drive the code and see if it helps.