Automatically Adding a Contact

Questions about and code samples for automation process code snippets within Select.
Post Reply
bthomas
Posts: 2
Joined: Fri Apr 16, 2021 1:28 pm

Automatically Adding a Contact

Post by bthomas »

Hello!

I am trying to automatically add a contact with the code 'CHI' and the contact type = government when a file is in a specific city. The automation runs in the file I test it with, but I am receiving the error 'object has no attribute' and I am not sure if I need to be more specific with the following:

Context.Order.Contacts[0]

Below is the code that I have so far (I have not specified the contact type yet):

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

#Request: When the transaction has the City = Chicago, add a contact
#Government (contact ‘type’) = CHI (the code) for “City of Chicago”.
#It has to be the lookup code of “CHI”

def NeedsCHI():
for contact in Context.Order.Contacts[0]:
if not contact.Code:
return
elif contact.Code.lower() == 'CHI':
raise ValueError('Contact Code ' + code + ' already exists on the Order.')
return True
NeedsCHI()

code = 'CHI'
if NeedsCHI():
CHI = IOrderContact.CreateNew(Context, 'Contact')
CHI.Code = code
Contacts = Context.Order.Contacts[0]
Contacts.Add(CHI)

I would appreciate any help/guidance at all! Thank you so much!
bthomas
Posts: 2
Joined: Fri Apr 16, 2021 1:28 pm

Re: Automatically Adding a Contact

Post by bthomas »

Code: Select all

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

#Request: When the transaction has the City = Chicago, add a contact
#Government (contact ‘type’) = CHI (the code) for “City of Chicago”.
#It has to be the lookup code of “CHI”

def NeedsCHI():
for contact in Context.Order.Contacts[0]:
if not contact.Code:
return
elif contact.Code.lower() == 'CHI':
raise ValueError('Contact Code ' + code + ' already exists on the Order.')
return True
NeedsCHI()

code = 'CHI'
if NeedsCHI():
CHI = IOrderContact.CreateNew(Context, 'Contact')
CHI.Code = code
Contacts = Context.Order.Contacts[0]
Contacts.Add(CHI)
BobRichards
Posts: 1376
Joined: Wed Jan 15, 2014 3:50 pm
Location: Raleigh, NC
Contact:

Re: Automatically Adding a Contact

Post by BobRichards »

I reworked your code to make it more general and fix several issues.
  • In your original code, it is very difficult to determine indentations so I had to take educated guesses.
  • for contact in Context.Contacts[0] - In my code below, I used the automation "When" clause of "Order is saved". If your context is different, you will need to modify it. Since the context is an order, you don't write "Context.Order.Contacts" - use "Context.Contacts".
  • for contact in Context.Contacts[0] - "Context.Contacts[0]" specifies the first contact. You want to iterate over the set of all contacts so use "Context.Contacts".
  • contact.Code.lower() == 'CHI' - Oops.
  • raise ValueError('Contact Code ' ... - Don't raise errors in automation snippets. Only an administrator could see them. Is it really an error if the contact exists or would the right course of action be to simply not create the contact (as you seem to be doing in the bottom of the snippet.)
  • CHI = IOrderContact.CreateNew(Context, 'Contact') - You must provide the specific contact type. Look up "ContactType" in the SDK to get all the values.
  • CHI.Code = code - Use LookupCode to set a value from a Lookup Table. Code is a value like "A" or "B" (Settlement Agent or Buyer). It is not writeable.
  • Contacts = Context.Order.Contacts[0] - As above, you want the list of ALL contacts, not just the first contact.
I really appreciate you giving it a try first then sending your code in the post. Many people ask questions and never even try to solve it. Please don't take my comments above negatively - the Select order model is difficult to learn and I wanted to clear up any questions you may have had.

Code: Select all

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

#Request: When the transaction has the City = Chicago, add a contact
#Government (contact ‘type’) = CHI (the code) for “City of Chicago”.
#It has to be the lookup code of “CHI”

# Context is "order".

# Desired contact matching information.
TargetContactType = ContactType.Government
TargetLookupCode = 'CHI' # Must be uppper case for matching!!!

# Return False if order has desired contact.
#	Otherwise return True.
def NeedsCHI():
	# Look through all order contacts to see if it already has our contact.
	for contact in Context.Contacts:
		if contact.ContactType != TargetContactType:
			# Not a government contact type - go to next contact.
			continue
		if not contact.LookupCode:
			# No lookup code - go to next contact.
			continue
		if contact.LookupCode.upper() == TargetLookupCode:
			# Found our contact!
			return False
	
	# Contact not found.
	return True

# If order doesn't have target contact. Create it.
if NeedsCHI():	
	#	Create instance of specific contact type.
	contact = IOrder.CreateNew(Context, 'Government')
	contact.LookupCode = TargetLookupCode
	
	# Add new contact to list of all contacts.
	Contacts = Context.Contacts
	Contacts.Add(contact)
Bob Richards, Senior Software Developer, SoftPro
Post Reply