Project Name Contains Specific Words

Questions about and code samples for automation process code snippets within Select.
Post Reply
zhinze
Posts: 4
Joined: Sat Aug 25, 2018 2:31 pm

Project Name Contains Specific Words

Post by zhinze » Mon Nov 11, 2019 2:18 pm

I'm looking for a way to look for specific words in the Project field within an order. For example, I want to use an automation to look at project name and if it contains X, Y or Z to return true and if not to return false. The automation I'm needing to build is based off of a task being completed and I don't know if my context is what it failing out or something else. Also, I'm not familiar with writing a contains function in IronPython, so I may have used the wrong expression.

Any guidance would be appreciated.

Code: Select all

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

def TitleProcessing():
   if Order.Project contains ("X", "Y", "Z")
   return True

 return False

TitleProcessing()

BobRichards
Posts: 813
Joined: Wed Jan 15, 2014 3:50 pm
Location: Raleigh, NC
Contact:

Re: Project Name Contains Specific Words

Post by BobRichards » Mon Nov 11, 2019 3:39 pm

For the most part, if you need help with Python, just Google for "python" and your subject. It usually doesn't matter if Select uses IronPython since it is based on Python. These examples should help. All we are doing is breaking the list into separate items and comparing it against the project text.

Code: Select all

project = Order.Project

# Items to look for in Project text.
target_list = ['X', 'Y', 'Z']

for item in target_list:
	if item in project:
		return True

# Not found.
return False
Since Python comparisons are always case sensitive, the matching will fail if the user enters lower case letters (i.e. 'x'). If you want to make it case insensitive, do comparisons after all the strings are made lower case. Of course, you can convert all to upper case for comparison since it doesn't matter what you convert to as long as it is the same.

Code: Select all

# Get the project and convert it to lower case for comparison.
project = Order.Project
project = project.lower()

# Items to look for in Project text.  Enter words in lower case!
target_list = ['hello', goodbye', 'lawyer']

for item in target_list:
	if item in project:
		return True

# Not found.
return False
Bob Richards, Software Developer, SoftPro

zhinze
Posts: 4
Joined: Sat Aug 25, 2018 2:31 pm

Re: Project Name Contains Specific Words

Post by zhinze » Mon Nov 11, 2019 4:54 pm

That makes sense. I think I'm still having an issue with the context. The automation is based off of when a task is updated or added. Do I need to add something to change context.

BobRichards
Posts: 813
Joined: Wed Jan 15, 2014 3:50 pm
Location: Raleigh, NC
Contact:

Re: Project Name Contains Specific Words

Post by BobRichards » Mon Nov 11, 2019 6:18 pm

Since your automation snippet is based on tasks, then for each task that is changed, you will have a SEPARATE call to your code. (This is a very important point that many uses don't get.)

You can get any order property. In this case, I used the Root property that is available on many objects to get the top level of the order (the Order object in the Field Code Browser). Now I can get the Project text easily.

Code: Select all

from ...

def TitleProcessing():
	# The context is a task.  Get the top level order object.
	#   If user changed (or added) multiple tasks, then Select will call this snippet once for each task.
	order = Context.Root
	
	# Store the project name in a working variable.
	project = order.Project
	
	... add your processing code here...

TitleProcessing()
Bob Richards, Software Developer, SoftPro

zhinze
Posts: 4
Joined: Sat Aug 25, 2018 2:31 pm

Re: Project Name Contains Specific Words

Post by zhinze » Tue Nov 12, 2019 12:17 pm

So when I combine the code to correct the context with my processing code, I am still unable to get the automation to fire off. This one really has me stumped, as I now should have context correct, I have the project field converting to lower case and have the target list in all lowercase to match the project field.

Code: Select all

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

def TitleProcessing():

# The context is a task.  Get the top level order object.
	#	If user changed (or added) multiple tasks, then Select will call this snippet once for each task.
	order = Context.Root
	
	#	Store the project name in a working variable and convert to lowercase.
	project = order.Project
	project = project.lower()

#	Items to look for in Project text.  Enter words in lower case!
target_list = ['hello', 'goodbye', 'lawyer']

for item in target_list:
	if item in project:
		return True

# Not found.
return False

TitleProcessing()



BobRichards
Posts: 813
Joined: Wed Jan 15, 2014 3:50 pm
Location: Raleigh, NC
Contact:

Re: Project Name Contains Specific Words

Post by BobRichards » Tue Nov 12, 2019 2:16 pm

What is the exact string you have in the Project field?
Bob Richards, Software Developer, SoftPro

zhinze
Posts: 4
Joined: Sat Aug 25, 2018 2:31 pm

Re: Project Name Contains Specific Words

Post by zhinze » Tue Nov 12, 2019 2:44 pm

I tested with just hello in the project field so I could first verify that it would work with one of the exact words in the exact case from the code snippet.

BobRichards
Posts: 813
Joined: Wed Jan 15, 2014 3:50 pm
Location: Raleigh, NC
Contact:

Re: Project Name Contains Specific Words

Post by BobRichards » Tue Nov 12, 2019 3:51 pm

You need to have all the code that is part of the TitleProcessing() function indented in the same way. This is how Python knows what lines are part of what method, class, function, etc. You mentioned that you are new to Python. Perhaps you might want to get a book or do one of the free online tutorials. It will make life a little easier. There are many free (and short) courses from Udemy.com that could help. (Udemy is my personal choice and this is not an endorsement by SoftPro.)

Your code should look more like this:

Code: Select all

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

def TitleProcessing():

	# The context is a task.  Get the top level order object.
	#	If user changed (or added) multiple tasks, then Select will call this snippet once for each task.
	order = Context.Root
	
	#	Store the project name in a working variable and convert to lowercase.
	project = order.Project
	project = project.lower()

	#	Items to look for in Project text.  Enter words in lower case!
	target_list = ['hello', 'goodbye', 'lawyer']

	for item in target_list:
		if item in project:
			return True

	# Not found.
	return False

TitleProcessing()

Bob Richards, Software Developer, SoftPro

Post Reply