cancel
Showing results for 
Search instead for 
Did you mean: 

Script C# for workflow action

Jose_Contreras4
Champ on-the-rise
Champ on-the-rise

Hello All,

I am very new at scripting.  I am looking to run a workflow script that will take all the documents in a workflow queue and send them all to a batch as a single batch.  However, finding Hyland examples of scripts is not easy.

Note*  I know there is a workflow action to send the docs 1 batch/1 doc.  however, if I have 100 docs then I would need 1 batch/100 docs.


For Hyland: Is there a forum where people can post their examples of scripts?

Here is my script: 

using System;
using System.Text;
using System.Collections.Generic;
using Hyland.Unity;
using Hyland.Unity.Workflow;
using Hyland.Unity.CodeAnalysis;

//namespace PNCaddtotoPNCCPDscanqueue
//{
public class PNCaddtotoPNCCPDscanqueue : IWorkflowScript
{
public void OnWorkflowScriptExecute(Application app, WorkflowEventArgs args)
{
// Retrieve documents from the specified workflow queue
List<Document> documentsFromWorkflow = GetDocumentsFromWorkflow(app, 336);

// Create a new batch
BatchManagement batchManager = app.Core.BatchManagement;
ScanQueueList scanQueues = batchManager.ScanQueues;
ScanQueue scanQueue = scanQueues.Find("HB/PB PNC BANK CPD");
ScanBatchProperties scanBatchProperties = batchManager.CreateNewScanBatchProperties(scanQueue);
ScanBatch scanBatch = batchManager.CreateScanBatch(scanBatchProperties);

// Add documents to the batch
foreach (Document doc in documentsFromWorkflow)
{
// Add the document to the batch
scanBatch.AddDocument(doc);
}

// Save the batch
scanBatch.Save();
}

// Retrieve documents from the specified workflow queue
private List<Document> GetDocumentsFromWorkflow(Application app, int queueId)
{
// Initialize an empty list to hold the retrieved documents
List<Document> documents = new List<Document>();

// Get the workflow queue with the specified ID
WorkflowQueue queue = app.Core.Workflow.GetWorkflowQueue(queueId);

// Retrieve documents from the workflow queue
foreach (WorkflowItem item in queue.Items)
{
// Assuming GetDocument method retrieves the document associated with the workflow item
Document doc = app.Core.Workflow.GetDocument(item.DocumentId);
documents.Add(doc);
}

return documents;
}
//}
}

1 REPLY 1

Sherrie_Butler2
Confirmed Champ
Confirmed Champ

Here is an example from a script I am currently working on to add documents to scan queue batches.  I'm getting my documents using a combination of Document Query and SQL, but once you have the documents, but other than that, seems like we trying to do the same thing.

This has been my first adventure with writing a script for this, and one thing I struggled with was having to use ScanBatchQuery and Transition.  Depending on what will need to happen once your documents are in the batch, you may or may not need these lines of code.  But I'm still learning, so take that with a grain of salt.

The other struggle I had was getting my settings in Configuration correct so that the batches functioned the way we needed once the documents were in the scan queue.  So, if you get your batches loaded with documents, but it's still not quite right, check your configuration.  The configuration settings impact the status the batch will be in once it's created and transitioned.  

 public class AddDocumentstoScanQueue : Hyland.Unity.IWorkflowScript    {                #region IWorkflowScript        /// <summary>        /// Implementation of <see cref="IWorkflowScript.OnWorkflowScriptExecute" />.        /// <seealso cref="IWorkflowScript" />        /// </summary>        /// <param name="app"></param>        /// <param name="args"></param>        public void OnWorkflowScriptExecute(Hyland.Unity.Application app, Hyland.Unity.WorkflowEventArgs args)        {			string batchName = "BATCH_NAME";				string scanQueueName = "SCAN_QUEUE_NAME"						//Identify the Scan Queue			BatchManagement batchManager = app.Core.BatchManagement;			ScanQueueList scanQueues = batchManager.ScanQueues;			ScanQueue scanQueue = scanQueues.Find(scanQueueName);						//Create the Scan Batch			ScanBatchProperties scanBatchProperties = batchManager.CreateNewScanBatchProperties(batchName, scanQueue);						ScanBatch scanBatchNew = batchManager.CreateScanBatch(scanBatchProperties);							//query for the new batch to find and transition			ScanBatchQuery batchQuery = batchManager.CreateScanBatchQuery();			batchQuery.AddScanQueue(scanQueue);																				ScanBatchList scanBatchList = batchQuery.Execute();			foreach (ScanBatch scanBatch in scanBatchList)			{				if(scanBatch.Name == batchName)				{									scanBatch.Transition();					}							}						List<Document> docsForScanQueue = new List<Document>();			//code to add documents to the list//						foreach(Document doc in docsForScanQueue)			{								scanBatchNew.AddDocuments(new List<Document>{doc});				}			}