Page 1 of 2

Add Checklist task based on Requested Task Name

Posted: Wed Jan 31, 2018 4:37 pm
by DanChapin
I am trying to loop through the requested tasks until I find a task with "Exam - UP" in the name. Then I am reading the last two characters from that task which will be the task number ie "Exam - UP01" result will return "01". Now I create the new checklist name "'Exam - UP' + PLTaskName + ': Searched'" Then create the checklist task. I get the following error:

"Collection was modified; enumeration operation may not execute."

Help Please!
Thanks
Dan

from System import *
from SoftPro.ClientModel import *
from SoftPro.Select.Client import *
from SoftPro.OrderTracking.Client.Orders import *
from SoftPro.Select.Client.Security import *

###################################################################
# Snippet type: Action
# Description: Create Exam update Task
####################################################################

for task in Context.RequestedTasks:
PLTaskName = task.Description
if PLTaskName.lower().startswith('exam - up'):
#continue
PLTaskName = task.Description[-2:]
PLCTaskName = 'Exam - UP' + PLTaskName + ': Searched'
#raise ValueError(PLCTaskName)
newtask = IOrder.CreateNew(Context, 'ChecklistTask')
newtask.Description = PLCTaskName
newtask.Status = TaskStatusType.Required
newtask.AssignedTo = Context.Escrow.PreCloserEscrowAssistant
newtask.DueDate = newtask.CreatedDate.AddDays(4)
#newtask.Contact = payofflender
Context.Tasks.Add(newtask)

Re: Add Checklist task based on Requested Task Name

Posted: Fri Feb 02, 2018 11:21 am
by BobRichards
I can't guarantee this is the answer since you didn't enter your code in the code display formatter (button above labelled "</>") and all white space disappeared. However, this is the classic error of attempting to modify a collection that you are looping on.

Code: Select all

for task in Context.RequestedTasks:
    ...
    Context.Tasks.Add(newtask)
Your "for" loop gets its tasks from the RequestedTasks collection - and Context.Tasks is the data source for this collection. You are not allowed to modify the collection members (add or remove) while you are looping on them.

Re: Add Checklist task based on Requested Task Name

Posted: Fri Feb 02, 2018 11:29 am
by DanChapin
So I am looping through the requested tasks to set a name variable that will be used to create a Checklist Task. How would I do that?

Here is the formatted code

Code: Select all

rom System import *
from SoftPro.ClientModel import *
from SoftPro.Select.Client import *
from SoftPro.OrderTracking.Client.Orders import *
from SoftPro.Select.Client.Security import *

###################################################################
# Snippet type: Action
# Description: Create Exam update Task
####################################################################

for task in Context.RequestedTasks:
	PLTaskName = task.Description
	if PLTaskName.lower().startswith('exam - up'):
		#continue
		PLTaskName = task.Description[-2:]
		PLCTaskName = 'Exam - UP' + PLTaskName + ': Searched' # Creates the task name to be created
		#raise ValueError(PLCTaskName)
		newtask = IOrder.CreateNew(Context.ChecklistTasks, 'ChecklistTask')
		newtask.Description = PLCTaskName
		newtask.Status = TaskStatusType.Required
		newtask.AssignedTo = Context.Escrow.PreCloserEscrowAssistant
		newtask.DueDate = newtask.CreatedDate.AddDays(4)
		#newtask.Contact = payofflender
		Context.Tasks.Add(newtask)


Re: Add Checklist task based on Requested Task Name

Posted: Fri Feb 02, 2018 11:38 am
by DanChapin
Can we use and return a function within a code snipped to separate the loop from the create task code? I am not sure what that syntax would look like in an automation snippet?

Re: Add Checklist task based on Requested Task Name

Posted: Fri Feb 02, 2018 11:44 am
by BobRichards
Among the many ways this can be done, you could store the list of items that you create then add the items after the loop. (Code below is untested but should get the point across.)

Code: Select all

holdingList = []
for task in Context.RequestedTasks:
    ...
    // Create new item and add it to holding list.
    newtask = IOrder.CreateNew(Context.ChecklistTasks, 'ChecklistTask')
    ...
    holdingList.append(newTask)
    
// All RequestedTasks processed.  Now add any new tasks that were created.
foreach task in holdingList:
    Context.Tasks.Add(newtask)

Re: Add Checklist task based on Requested Task Name

Posted: Fri Feb 02, 2018 1:08 pm
by DanChapin

Code: Select all

holdingList = []
for task in Context.RequestedTasks:
                PLTaskName = task.Description
                if PLTaskName.lower().startswith('exam - up'):
                                #continue
                                PLTaskName = task.Description[-2:]
                                PLCTaskName = 'Exam - UP' + PLTaskName + ': Searched'
                                #raise ValueError(getUpdateNo())
                newtask = IOrder.CreateNew(Context.ChecklistTasks, 'ChecklistTask') # Create new item and add it to holding list
                newtask.Description = PLCTaskName
                newtask.Status = TaskStatusType.Required
                newtask.AssignedTo = Context.Escrow.PreCloserEscrowAssistant
                newtask.DueDate = newtask.CreatedDate.AddDays(4)
                #newtask.Contact = payofflender
                holdingList.append(newTask) 
# All RequestedTasks processed.  Now add any new tasks that were created.
foreach task in holdingList:
                Context.Tasks.Add(newtask)
This new snippet returns the following error:
Unexpected token ‘task’

Re: Add Checklist task based on Requested Task Name

Posted: Fri Feb 02, 2018 1:57 pm
by BobRichards
Oops. Try "for item in holdingList:" instead of "foreach". This page is handy for Python list questions: http://effbot.org/zone/python-list.htm

Re: Add Checklist task based on Requested Task Name

Posted: Fri Feb 02, 2018 3:47 pm
by DanChapin
Thanks Bob, I had already changed and tried "for" rather than "foreach", after making that change and running from the code below, I get the following error:

expected IOrder, got Set[ChecklistTask]


My Code:

Code: Select all

from System import *
from SoftPro.ClientModel import *
from SoftPro.Select.Client import *
from SoftPro.OrderTracking.Client.Orders import *
from SoftPro.Select.Client.Security import *

###################################################################
# Snippet type: Action
# Description: Create Exam update Task
####################################################################
holdingList = []
for task in Context.RequestedTasks:
	PLTaskName = task.Description
	if PLTaskName.lower().startswith('exam - up'):
		#continue
		PLTaskName = task.Description[-2:]
		PLCTaskName = 'Exam - UP' + PLTaskName + ': Searched'
		#raise ValueError(getUpdateNo())
		newtask = IOrder.CreateNew(Context.ChecklistTasks, 'ChecklistTask') # Create new item and add it to holding list
		newtask.Description = PLCTaskName
		newtask.Status = TaskStatusType.Required
		newtask.AssignedTo = Context.Escrow.PreCloserEscrowAssistant
		newtask.DueDate = newtask.CreatedDate.AddDays(4)
		#newtask.Contact = payofflender
		holdingList.append(newTask) 
# All RequestedTasks processed.  Now add any new tasks that were created.
for task in holdingList:
	Context.Tasks.Add(newtask)

Re: Add Checklist task based on Requested Task Name

Posted: Fri Feb 02, 2018 4:32 pm
by BobRichards
You should be passing the Order to the object creator:

Code: Select all

    ...
    newtask = IOrder.CreateNew(Context.Root, 'ChecklistTask')
    ...

Re: Add Checklist task based on Requested Task Name

Posted: Tue Feb 06, 2018 3:25 pm
by DanChapin
Still getting error: This time "An Item with the same key has already been added" code below

Code: Select all

from System import *
from SoftPro.ClientModel import *
from SoftPro.Select.Client import *
from SoftPro.OrderTracking.Client.Orders import *
from SoftPro.Select.Client.Security import *

###################################################################
# Snippet type: Action
# Description: Create Exam update Task
####################################################################
holdingList = []
for task in Context.RequestedTasks:
	UPDTaskName = task.Description
	if UPDTaskName.lower().startswith('exam - up'):
		#continue
		UPDTaskName = task.Description[-2:]
		UPDTaskName = 'Exam - UP' + UPDTaskName + ': Searched'
		#raise ValueError(getUpdateNo())
	# Create new item and add it to holding list
	#newtask = IOrder.CreateNew(Context.ChecklistTasks, 'ChecklistTask') #expected IOrder, got Set[ChecklistTask]
	#newtask = IOrder.CreateNew(Context.Root.ChecklistTasks, 'ChecklistTask') #expected IOrder, got Set[ChecklistTask]
	newtask = IOrder.CreateNew(Context.Root, 'ChecklistTask') 
	newtask.Description = UPDTaskName
	newtask.Status = TaskStatusType.Required
	newtask.AssignedTo = Context.Escrow.PreCloserEscrowAssistant
	newtask.DueDate = newtask.CreatedDate.AddDays(4)
	#holdingList.append(newTask) #newTask is not defined
	holdingList.append(newtask) 
# All RequestedTasks processed.  Now add any new tasks that were created.
for task in holdingList:
	Context.Tasks.Add(newtask)