Automation for Moving Documents to Folders

Questions about and code samples for automation process code snippets within Select.
kendale.wyatt
Posts: 14
Joined: Wed Sep 26, 2018 1:24 pm

Automation for Moving Documents to Folders

Post by kendale.wyatt » Mon Oct 01, 2018 2:14 pm

Is there a way to have documents moved to a specific folder based on the document name? Like if a document is attached with Payoff it is moved to the money folder?

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

Re: Automation for Moving Documents to Folders

Post by BobRichards » Tue Oct 02, 2018 10:53 am

You can interact with the attachments with the IAttachmentFile and IAttachmentFolder interfaces (See the properties and methods for each in the SDK). The general technique is to get the top level attachment folder then use IAttachmentFolder operations to find/create/delete folders or navigate to other folders. Once you find the folder, you can get the contents of a folder with the "Items" property. You can inspect the names in the folder and if you find the criteria you are looking for, you can move/copy it.

Below is an example of moving a file that is used as the action ("run" code snippet) for the automation rule:
  • When a document is attached
  • and document name contains 'Title Search'
  • and a code snippet evaluates to true
  • then do the following:
  • run code snippet

Code: Select all

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

# Context ( IAttachmentItem) is the document that was attached to order!
def MoveDocFolder():
    # Get top level attachment folder at "\{OrderNumber}\Attachments"
    topFolder = IOrder.Attachments.GetValue(Context.Parent.Order)
    
    # Get child destination folder at "\{OrderNumber}\Attachments\Title"  
    #     If folder missing then skip move operation.
    items = [f for f in topFolder.Items if f.Name == 'Title']
    if items:
        # Got a result.  The first (and only) item is the target folder.
        destFolder = items[0]
        
        # If document already in the destination folder then skip move.
        #    Otherwise move document to Title folder.
        if Context.Parent.Path != destFolder.Path:
            destFolder.MoveItem(Context, destFolder)        

MoveDocFolder()
Bob Richards, Software Developer, SoftPro

jturek
Posts: 14
Joined: Fri Jul 20, 2018 6:17 pm

Re: Automation for Moving Documents to Folders

Post by jturek » Thu Dec 20, 2018 11:31 am

Hi Bob,

Our requirement is to move some specific list of Order documents [In Attachment folder] move in Local Folder on C or D drive of system then the same documents will send to our portal.

For this requirement,tried with the below the automation snippet code to move documents on local folder on drive.

Code: Select all

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

# Context ( IAttachmentItem) is the document that was attached to order!
def MoveDocFolder():
    # Get top level attachment folder at "\{OrderNumber}\Attachments"
    topFolder = IOrder.Attachments.GetValue(Context.Parent.Order)
    
    # Get child destination folder at "\{OrderNumber}\Attachments\Title"  
    #     If folder missing then skip move operation.
    items = [f for f in topFolder.Items ]
    if items:
        # Got a result.  The first (and only) item is the target folder.
        destFolder = items[0] # "C:\\OrderDocuments"
        
        # If document already in the destination folder then skip move.
        #    Otherwise move document to Title folder.
        if Context.Parent.Path != destFolder.Path:
            destFolder.MoveItem(Context, destFolder)        

MoveDocFolder()
This gives below error message.

Error: The automation instance failed.
Stack Trace:
at SoftPro.Select.Server.Automation.Runtime.XamlWorkflowInstanceWorker.OnResume(String bookmark, EventArgs args)
at SoftPro.Select.Server.Automation.Runtime.WorkflowController.Resume(WorkflowInstance instance, String bookmark, EventArgs args)
***********************************************************************************
Error: 'Order' object has no attribute 'Parent'
Stack Trace:
at SoftPro.ClientModel.AsyncResult`1.get_Result()
at SoftPro.Select.Server.Automation.Builtin.CodedActionActivity.EndExecute(NativeActivityContext context, IAsyncResult result)
at System.Activities.Runtime.BookmarkWorkItem.Execute(ActivityExecutor executor, BookmarkManager bookmarkManager)


Please help me resolve this .

Is this right approach to move the documents of 'Attachment' folder of Order ?

For the same requirement I have worked on the second approach,that is send the Order Number by automation snippet and get the document list using Attachments.Items. It gives the list of documents path but not able to get the File stream for the same as.

Code: Select all

  IOrder order = os.OpenOrder(search, true);
  List<IAttachmentItem> lstAttachmentItem = order.Attachments.Items.ToList();

  foreach (IAttachmentItem Item in lstAttachmentItem)
   {
      FileStream stream = File.OpenRead(Item.Path);
    }


This also gives error as , not able to find the document path.

Please suggest.

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

Re: Automation for Moving Documents to Folders

Post by BobRichards » Thu Dec 20, 2018 12:05 pm

What is the automation rule setup? If I had to guess for the first snippet, it looks like the Context is an Order and since Order doesn't have a Parent property, the error occurs.
Bob Richards, Software Developer, SoftPro

jturek
Posts: 14
Joined: Fri Jul 20, 2018 6:17 pm

Re: Automation for Moving Documents to Folders

Post by jturek » Fri Dec 21, 2018 1:40 pm

Hello Bob,
This is Joanna, now we are getting this error

Error: The automation instance failed.
Stack Trace:
at SoftPro.Select.Server.Automation.Runtime.XamlWorkflowInstanceWorker.OnStart(EventArgs args)
at SoftPro.Select.Server.Automation.Runtime.WorkflowController.Start(WorkflowInstance instance, EventArgs args)
***********************************************************************************
Error: 'AttachmentFile' object has no attribute 'MoveItem'
Stack Trace:
at SoftPro.ClientModel.AsyncResult`1.get_Result()
at SoftPro.Select.Server.Automation.Builtin.CodedActionActivity.EndExecute(NativeActivityContext context, IAsyncResult result)
at System.Activities.Runtime.BookmarkWorkItem.Execute(ActivityExecutor executor, BookmarkManager bookmarkManager).


Here is the snippet our team wrote:

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

# Context ( IAttachmentItem) is the document that was attached to order!
def MoveDocFolder():
# Get top level attachment folder at "\{OrderNumber}\Attachments"
topFolder = IOrder.Attachments.GetValue(Context)
# Get child destination folder at "\{OrderNumber}\Attachments\Title"
# If folder missing then skip move operation.
items = [f for f in topFolder.Items]

for p in items: print p

if items:
# Got a result. The first (and only) item is the target folder.
destFolder = items
# If document already in the destination folder then skip move.
# Otherwise move document to Title folder.
#if Context.Parent.Path != destFolder.Path:
for i in destFolder: i.MoveItem(Context, destFolder)

MoveDocFolder()


Can you tell what's wrong with it?

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

Re: Automation for Moving Documents to Folders

Post by BobRichards » Thu Jan 31, 2019 12:37 pm

Yes. "MoveItem" is not a valid method name.
Bob Richards, Software Developer, SoftPro

Kevin Olinski
Posts: 3
Joined: Wed Feb 26, 2020 10:46 am

Re: Automation for Moving Documents to Folders

Post by Kevin Olinski » Wed Feb 26, 2020 12:12 pm

Hello,

We have been using this automation for a bit and it works great unless a document with the same name already exists. The automation will fail and we get "An item already exists by that name" error. ProForm will auto rename duplicate documents in attachments but it appears that this automation attempts to run before the renaming happens. Is there any way to avoid this? Possibly code it to not run if it sees a document of the same name and then once it is renamed it would run?

Thank you

Kevin Olinski
Posts: 3
Joined: Wed Feb 26, 2020 10:46 am

Re: Automation for Moving Documents to Folders

Post by Kevin Olinski » Wed Feb 26, 2020 12:12 pm

Hello,

We have been using this automation for a bit and it works great unless a document with the same name already exists. The automation will fail and we get "An item already exists by that name" error. ProForm will auto rename duplicate documents in attachments but it appears that this automation attempts to run before the renaming happens. Is there any way to avoid this? Possibly code it to not run if it sees a document of the same name and then once it is renamed it would run?

Thank you

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

Re: Automation for Moving Documents to Folders

Post by BobRichards » Wed Feb 26, 2020 12:37 pm

You can do what the Select Client is doing under the covers - go to the attachment folder and get the file names. If any match the file you want to add, then change your new file name until it is unique. You can add this logic into the code snippet.
Bob Richards, Software Developer, SoftPro

Kevin Olinski
Posts: 3
Joined: Wed Feb 26, 2020 10:46 am

Re: Automation for Moving Documents to Folders

Post by Kevin Olinski » Mon Mar 02, 2020 10:17 am

Hi Bob,

In working on this I noticed the above snippet contains this code.

Code: Select all

# If document already in the destination folder then skip move.
        #    Otherwise move document to Title folder.
        if Context.Parent.Path != destFolder.Path:
            destFolder.MoveItem(Context, destFolder)        
Shouldn't this cancel the automation if the document is a duplicate and avoid the error we are receiving?

Thanks

Post Reply