Get Value for Attachment Description

Questions about and code samples for automation process code snippets within Select.
Post Reply
BobRichards
Posts: 1116
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, Senior Software Developer, SoftPro

BobRichards
Posts: 1116
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, Senior Software Developer, SoftPro

tmeisinger
Posts: 74
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: 1116
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, Senior Software Developer, SoftPro

timothymeyer16
Posts: 11
Joined: Mon Jun 14, 2021 9:47 am

Re: Get Value for Attachment Description

Post by timothymeyer16 » Wed Jul 07, 2021 9:57 am

Hi Bob,

First time poster here, please let me know if I make any Faux Pas. Also, thank you for all you're posts, they're extremely helpful.

The CORs example you provided worked well in CORs. I was able to iterate and analyze the files (and file size) as needed.
However, I was unable to implement this code into an automation. I received errors at several points.


Automation Condition Errors:
- First comes from topFolder = IOrder.Attachments.GetValue(Context)
- If .GetValue() is added, the file will not save
- If I remove .GetValue(); "for item in folder.Items:" will prevent the automation from running.


If the conditions are removed, I get the following errors in Automation execution:
First, "topFolder = IOrder.Attachments #.GetValue(Context)" => "Expected IOrder got AttachmentFile"
Second, "for item in folder.Items:" # Causes Error => "Get Set Descriptor has no attribute Items"


They seem to be the same error. Can you shed a bit of light on the situation?

Additional Information:
- File attachment is "Test.txt" 1KB in size



Automation Conditions:
-------------------------
from System import *
from SoftPro.ClientModel import *
from SoftPro.OrderTracking.Client import *
from SoftPro.OrderTracking.Client.Orders import *
from System.Collections import Queue


def FileGreaterThan24MB():

return True

topFolder = IOrder.Attachments #.GetValue(Context) => Causes Error in automation; wont run

searchQueue = Queue()
searchQueue.Enqueue(topFolder)

while searchQueue.Count > 0:

folder = searchQueue.Dequeue()

for item in folder.Items: # causes error in automation; wont run

if 'File' in str(type(item)) and '.' in item.Name:

fileSizeBits = item.Length

# Size in mb
fileSizeMegaBites = fileSizeBits // 1#000000

if fileSizeMegaBites > 24:
return True

return False



FileGreaterThan24MB()

Automation Execution:
-------------------------
from System import *
from SoftPro.ClientModel import *
from SoftPro.Select.Client import *
from SoftPro.OrderTracking.Client import *
from SoftPro.OrderTracking.Client.Orders import *
from System.Collections import Queue

from datetime import datetime

# Iterate over the Attachments
# and Add tasks for any greater
# Than 24 MB
def FilterAttachment():

topFolder = IOrder.Attachments #.GetValue(Context) => "Expected IOrder got AttachmentFile"
searchQueue = Queue()
searchQueue.Enqueue(topFolder)

while searchQueue.Count > 0:

folder = searchQueue.Dequeue()

for item in folder.Items: # Causes Error => "Get Set Descriptor has no attribute Items"

if 'File' in str(type(item)) and '.' in item.Name:

fileSizeBits = item.Length

# Size in mb
fileSizeMegaBites = fileSizeBits // 1#000000

if fileSizeMegaBites > 24:
#CreateNewTask(item)

FilterAttachment()

Post Reply