Corrupted Code Snippets for Task Automation

Questions about and code samples for automation process code snippets within Select.
Post Reply
hollyburgan
Posts: 2
Joined: Sun Feb 04, 2024 2:49 pm

Corrupted Code Snippets for Task Automation

Post by hollyburgan »

Hi there!
I am fairly new to softpro & and code snippets (1 year in), but am doing everything I can to learn so hopefully you can bear with me ;)
We have Automated code snippets in place to update our tasks, but it seems that 2 of our code snippets are corrupted as when we try to save a file (or publish documents) we will frequently (although sporadically) receive error messages which refer us back to the 2 Automation code snippets (either 145 Code, or 150 Code). When these error messages occur, Softpro won't let us save/publish the first time around, but will eventually allow us to save (or publish) on the second go around.

Our intent is to still utilize the function of the SSN/TaxID to update our tasks, but just need uncorrupted snippets that won't cause errors. Thank you so very much for your help and assistance!

Code: Select all

 "CODE SNIPPET 145"
#Evaluate if Buyer SSN is populated
#Context = Buyer
def SSNPopulated():
	contact = Context
	organization = int(contact.BuyerSellerType.Organization)
	if int(contact.BuyerSellerType) & organization > 0:
		# Organization
		if contact.Name:
			if contact.TaxID:
				return True
	else:
		# Individual: Joint, Male or Female
		for people in contact.People:
			if people.Name:
				if people.SSN:
					return True

SSNPopulated()
Image

Code: Select all

 "CODE SNIPPET 150"
#Evaluate if Seller SSN is populated
#Context = Seller
def SSNPopulated():
	contact = Context
	organization = int(contact.BuyerSellerType.Organization)
	if int(contact.BuyerSellerType) & organization > 0:
		# Organization
		if contact.Name:
			if contact.TaxID:
				return True
	else:
		# Individual: Joint, Male or Female
		for people in contact.People:
			if people.Name:
				if people.SSN:
					return True

SSNPopulated()
Image
Attachments
150 auto and error.jpg
150 auto and error.jpg (84.6 KiB) Viewed 103 times
145 Automation.jpg.png
145 Automation.jpg.png (94.17 KiB) Viewed 103 times
145.jpg
145.jpg (26.36 KiB) Viewed 103 times
BobRichards
Posts: 1377
Joined: Wed Jan 15, 2014 3:50 pm
Location: Raleigh, NC
Contact:

Re: Corrupted Code Snippets for Task Automation

Post by BobRichards »

I do not see anything wrong with the code you wrote. Looks good. You need to track down additional information about when an error occurs. Can you look (or have an admin look) at the Event Log for the server? You should find an event for the error. Also look at any events within a second or so of the event for others that might provide additional information.

In my case, I was playing with different code and got an error "Error: An unexpected error occurred while evaluating the conditions for process '145' (ID=908a9c71-d4a1-489a-a488-c6af9769a521)." The event log error immediately before that one was "An error occurred on the service boundary. Error: An unexpected error occurred while evaluating the conditions for process '145' (ID=908a9c71-d4a1-489a-a488-c6af9769a521)." and halfway through the event log was the actual cause of the error - ... Error: unindent does not match any outer indentation level...
Bottom line - One Select error might generate multiple Event Log error messages but you may need to tell the Admin to look for them.
I would like to offer two pieces of advice for the code.
  • Your understanding of bitwise operators is good. I would use the actual enum when you need to get these types of values: BuyerSellerType.Organization. This required importing the correct namespace (SoftPro.OrderTracking.Client.Orders). It makes it clearer that you don't actually care about the contact's value at this point (organization = int(contact.BuyerSellerType.Organization)).
  • Explicitly use a return value in the SSNPopulated() method. It makes the code more obvious to maintain that the method ALWAYS returns a bool. I don't care if you always return at the end of the method or short circuit where it makes sense, but devs that come after you make not fully understand they might be returning a False value, too. Also, please give retValue a better name. :)

Code: Select all

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

# Required namespace for Trace class.
import System.Diagnostics as SD

#Evaluate if Buyer SSN is populated
#Context = Buyer
def SSNPopulated():
	# Set default return value for test.
	retValue = False					# Point (2)

	contact = Context
	organization = int(BuyerSellerType.Organization)	# Point (1)
	if int(contact.BuyerSellerType) & organization > 0:
		# Organization
		if contact.Name:
			if contact.TaxID:
				retValue = True			# Point (2)
	else:
		# Individual: Joint, Male or Female
		for people in contact.People:
			if people.Name:
				if people.SSN:
					retValue = True		# Point (2)
	
	# Return calculated return value.
	return retValue						# Point (2)

SSNPopulated()
Let me know what you find out!
Bob Richards, Senior Software Developer, SoftPro
amtech
Posts: 9
Joined: Tue May 24, 2022 5:51 pm

Re: Corrupted Code Snippets for Task Automation

Post by amtech »

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)
amtech
Posts: 9
Joined: Tue May 24, 2022 5:51 pm

Re: Corrupted Code Snippets for Task Automation

Post by amtech »

There was an indentation error in the code I posted. See below for the correct code.

Code: Select all

def SSNPopulated():
    # 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
                return retValue
    else:
        # Individual: Joint, Male or Female
        for people in contact.People:
            if people.Name:
                if people.SSN:
                    retValue = True
                    return retValue
    
    # Return calculated return value.
    return retValue
hollyburgan
Posts: 2
Joined: Sun Feb 04, 2024 2:49 pm

Re: Corrupted Code Snippets for Task Automation

Post by hollyburgan »

@amtech - Thank you SO much - You completely fixed the issue! We no longer have the error codes popping up. Thank you, thank you, thank you! :D :D :D
Post Reply