Get Value for Attachment Description

Questions about and code samples for automation process code snippets within Select.
Post Reply
BobRichards
Posts: 959
Joined: Wed Jan 15, 2014 3:50 pm
Location: Raleigh, NC
Contact:

Get Value for Attachment Description

Post by BobRichards » Tue Feb 05, 2019 3:33 pm

(Question from user) Is it possible to evaluate via Code Snippet for Automation if there is an Attachment in the Attachments folder with a description that contains a particular word such as Policy?
Bob Richards, Software Developer, SoftPro

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

Re: Get Value for Attachment Description

Post by BobRichards » Tue Feb 05, 2019 3:40 pm

Here is a solution for looking through all attachment files until you find a match. I have coded it as a COR so you can play with it. The COR passes the top level attachment folder to the input of the search routine (SearchAttachmentFiles()). If it finds a match, it will return the file. Otherwise it will return None. The function "FoundMatch()" is passed files until it either returns True or we run out of attachment files. You will modify this function to add the logic of what you call a match. There should be no reason to modify "SearchAttachmentFiles()".

Code: Select all

from System import *
from SoftPro.ClientModel import *
from SoftPro.OrderTracking.Client import *
from SoftPro.OrderTracking.Client.Orders import *
from System.Collections import Queue

def Order_RelatedOrders_Validate(args):
    fake = args.Context.RelatedOrders
    topFolder = IOrder.Attachments.GetValue(args.Context.Root)
    file = SearchAttachmentFiles(topFolder)
    
# Search all folders starting with pass one for attachment file meeting search criteria.
#   Input:  folder
#   Output: AttachmentFile or None if match not found.
def SearchAttachmentFiles(topFolder):
    searchQueue =  Queue()
    searchQueue.Enqueue(topFolder)
    
    # Loop until we run out of folders.
    while searchQueue.Count > 0:
        folder = searchQueue.Dequeue()
        for item in folder.Items:
            if isinstance(item, IAttachmentFolder):
                # Save folders we find to list of folders to possibly search later.
                searchQueue.Enqueue(item)
            elif FoundMatch(item):
                # Found a file.  Check for match.
                return item
    
    # Didn't find a match.
    return None
    
#  Return True if attachment file meets your search criteria.
def FoundMatch(file):
    # If description is filled out and contains our target phrase, return True.
    return file.Description and 'Hello' in file.Description 
There are many options available with this code. For instance...
  • If you want to search starting with a folder below the top level folder, pass the folder to "SearchAttachmentFiles()" and it will search that folder and its children.
  • If your match logic in "FoundMatch()" requires the file's folder, it will be "file.Parent".
Bob Richards, Software Developer, SoftPro

tmeisinger
Posts: 70
Joined: Fri Apr 24, 2015 10:33 am

Re: Get Value for Attachment Description

Post by tmeisinger » Tue Jul 28, 2020 6:45 pm

Any chance you could do the same in C# for me?

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

Re: Get Value for Attachment Description

Post by BobRichards » Tue Jul 28, 2020 9:24 pm

I'm afraid I don't have any time right now. :)

Why don't you give it a shot and I'll try to help if needed.

The Select interfaces IAttachmentFile, IAttachmentFolder, and IAttachmentItem are in the SDK Help file. A Python Queue is a FIFO. An example is the .NET System.Collections.Queue class. In addition to translating the code to C#, you have to create the search method.

Code: Select all

bool FoundMatch(IAttachmentFile file)
{
    ...
    return isMatch;
}
Let me know how it goes.
Bob Richards, Software Developer, SoftPro

Post Reply