My guess as to the cause of the problem is that sometimes the code snippet is hitting contacts other than buyers and sellers, despite how you have the logic properly setup to prevent this. I develop and debug my code snippets in CORs so that I can get real-time feedback on it's functionality so I tested yours the same way. I only receive errors if the code hits a Contact which is not a Buyer/Borrower or Seller. If you are comfortable with Custom Order Rules, I'd suggest testing on one of the orders that threw the error to verify the issue. If you are not, then below is the code I'd recommend using instead to add in a short circuit if the contact isn't a buyer or seller. I'd also recommend the logging Bob proposed above so that if the below code doesn't fix your issue, you can trace the actual cause.
Code: Select all
def SSNPopulated():
# Set default return value for test.
retValue = False
contact = Context
# Short circuit and return false if contact is not a Buyer or Seller
if ( str( contact.ContactType ) == "Buyer" ) or ( str( contact.ContactType ) == "Seller" ):
organization = int(BuyerSellerType.Organization))
else:
return retValue
if int(contact.BuyerSellerType) & organization > 0:
# Organization
if contact.Name:
if contact.TaxID:
retValue = True
else:
# Individual: Joint, Male or Female
for people in contact.People:
if people.Name:
if people.SSN:
retValue = True
# Return calculated return value.
return retValue
If you are familiar with Custom Order Rules, they are useful for testing Automation code snippets in a test file. Do not put test CORs in your MASTER ORDER RULES file. Below is the code I started with for testing:
Code: Select all
def SSNPopulated(args):
try:
# Set default return value for test.
retValue = False
contact = args.Context
organization = int(BuyerSellerType.Organization))
if int(contact.BuyerSellerType) & organization > 0:
# Organization
if contact.Name:
if contact.TaxID:
retValue = True
else:
# Individual: Joint, Male or Female
for people in contact.People:
if people.Name:
if people.SSN:
retValue = True
# Return calculated return value.
return retValue
except Exception as exceptionMsg:
args.RaiseWarning(exceptionMsg)
def Buyer_Name_Validate(args):
SSNPopulated()
args.RunDefaultRule()
def Seller_Name_Validate(args):
SSNPopulated()
args.RunDefaultRule()
[/code]
No exceptions. It handles not having a SSN or a name just fine so next I expanded it to all contacts, replacing the Buyer_Name_Validate and Seller_Name_Validate functions with Contact_Name_Validate.
Code: Select all
def Contact_Name_Validate(args):
SSNPopulated()
args.RunDefaultRule()
Now I'm immediately getting a missing member exception. Intuitively I know it's from the contact.BuyerSellerType field but RaiseWarnings is one way to find out. I start at the top of the code and work my way down.
Code: Select all
def SSNPopulated(args):
try:
args.RaiseWarning( str(args.Context) )
# Set default return value for test.
retValue = False
contact = Context
organization = int(BuyerSellerType.Organization))
if int(contact.BuyerSellerType) & organization > 0:
# Organization
if contact.Name:
if contact.TaxID:
retValue = True
else:
# Individual: Joint, Male or Female
for people in contact.People:
if people.Name:
if people.SSN:
retValue = True
# Return calculated return value.
return retValue
except Exception as exceptionMsg:
args.RaiseWarning(exceptionMsg)[/code]
Now I can see that I'm only accessing Buyers and Sellers, every other contact is immediately throwing the missing member exception. If I needed to debug more, I would work my way through the code adding warnings layer by layer and commenting out previous warnings as I cleared the section or layer of code. Since I started at the top and was working my way through, I found the issue immediately and I don't need to dig in more. Now I need to identify the piece of code that only relates to Buyers and Sellers and bake in a check for it, so I wrap the organization variable in an if-else statement and short circuit the function if the contact type is not one of those two. This resolved my errors on my test file. Please note that the correct code for the code snippet is at the top of this post, this takes in args for a COR.
Code: Select all
def SSNPopulated(args):
try:
args.RaiseWarning( str(args.Context) )
# Set default return value for test.
retValue = False
contact = args.Context
# Short circuit and return false if contact is not a Buyer or Seller
if ( str( contact.ContactType ) == "Buyer" ) or ( str( contact.ContactType ) == "Seller" ):
organization = int(BuyerSellerType.Organization))
else:
return retValue
if int(contact.BuyerSellerType) & organization > 0:
# Organization
if contact.Name:
if contact.TaxID:
retValue = True
else:
# Individual: Joint, Male or Female
for people in contact.People:
if people.Name:
if people.SSN:
retValue = True
# Return calculated return value.
return retValue
except Exception as exceptionMsg:
args.RaiseWarning(exceptionMsg)