Sample Automation Scripts

This section collects a set of sample automation scripts can copy and adjust to implement your own algorithms.

Creates a new file on the basis of another existing document

# if the user is not available, take it from the event
#if(!$user)
    #set($user = $event.user)
#end

# if the current folder is not available, take it from the event
#if(!$folder)
    #set($folder = $event.folder)    
#end

# get the document referenced by the current event
#set($document = $DocTool.findById($event.docId)) 

#if($user.isMemberOf("admin"))
    $log.info("user is admin");
    
    #set ($ctime = $DateTool.currentTime())
    $log.info("current time is $ctime");
    
    $DateTool.setDateFormatShort('yyyy-MM-dd')
    #set ($fdate = $DateTool.format($ctime,false))
    $log.info("fdate $fdate");
    
    # get the custom attribute 'output' of the folder
    #set ($val1 = $folder.getValue('output'))
    $log.info("val1 is $val1");
    #if( !$val1 )
        #set ($val1 = '')
    #end       
    
    # get the custom attribute 'attr1' of the document and update val1
    #set($val1 = $val1 + $document.getValue('attr1') +" - " + $fdate)

    # update the custom attribute 'output' of the fodler with the new val1
    $folder.setValue('output', $val1);
    
    # instantiate a new event for the folder(class FolderHistory) and prepare it
    #set ( $history = $ClassTool.newInstance("core.folder.FolderHistory") )
    $history.setUser($user);
    
    # retrieve the FolderDAO and use it to save the folder with it's event
    #set($folderDAO = $ContextTool.getBean("FolderDAO"))     
    $folderDAO.store($folder, $history);        
    
    # retrieve the custom attribute 'attr2' of the document
    #set($attr2Value = $document.getValue("attr2"))
    
    # instantiate the 3rd party library class IOUtils 
    #set($IOUtils = $ClassTool.newInstance("org.apache.commons.io.IOUtils"))
    
    #set ($current_time = $DateTool.currentTime())
    #set ($java_year = $current_time.getYear())
    #set ($current_year = $java_year + 1900)    
    #set($docAttrs = $document.getAttributeNames())
    
    # iterate over the custom attribute of the document
    #foreach( $attrn in $docAttrs )
        # if we find the existence of the 'attr3' attribute we have to elaborate it
        #if($attrn.toString().contains("attr3"))
            #set($attr3Value = $document.getValue($attrn.toString()))
            
            # define a new name for the document
            #set($newFileName = $attr2Value + "-" + $attr3Value + "-" +$fdate + ".txt")
            
            # prepare the content of the new document as a string and convert it into an InputStream with IOUtils
            #set ($docContent =  $document.getFileName() + ", " + $attr3Value)        
            #set ($docIS = $IOUtils.toInputStream($docContent))                
            
            # create a new document from scratch
            #set ( $documentVO = $ClassTool.newInstance("core.document.Document") )
            $documentVO.setFileName($newFileName);
            
            # get the 'prot' attribute and if the case initialize it
            #set($protValue = $document.getValue('prot'))
            #if(!$protValue)
                #set($protValue = 'NA');
            #end
            
            # define the path of folder that will receive the new document and create it 
            #set($targetFldPath = "/Received Emails/Sent/" + $current_year + "/" + $protValue)
            #set($docTargetFld = $DocTool.createPath($document, $targetFldPath, $user.username))
            
            $log.info("docTargetFld: $docTargetFld.id");    
            
            # Set the folder and fill the other metadata for the new file
            $documentVO.setFolder($docTargetFld);
            $documentVO.setLanguage('en');
            
            #set($templateDAO = $ContextTool.getBean('TemplateDAO'));
            #set($sendingsTemp = $templateDAO.findByName('sendings',1)); 
            $documentVO.setTemplate($sendingsTemp);
            $documentVO.setValue('prot',$protValue);
            $documentVO.setValue('dest-ind',$attr3Value);
            $documentVO.setValue('fecha-env',$fdate);
                        
            $log.info("content: $docIS"); 
            $log.info("document: $documentVO");             
            
            #set($dmanager = $ContextTool.getBean("DocumentManager") )  
            
            #set ( $dhistory = $ClassTool.newInstance("core.document.DocumentHistory") )
            $dhistory.setUser($user);
            $log.info("history: $dhistory");                        
            
            #set($newDocument = $dmanager.create($docIS, $documentVO, $dhistory) )
            $log.info("created new document with ID: $newDocument.id");            
        #end
    #end
#end

Updates the output attribute of the folder by appending the string attr1 - date (where attr1 is an attribute, and date is the current date e.g. 2020-02-18).
Creates a text file for each attr3 metadata value (attr3 is an attribute with multiple values) of the original document whose content will be a single line string.
The characteristics of the text file are:

  • Filename: attr2 - attr3 - date (where attr2 is the value of the attr2 attribute, attr3 is one of the multiple values ​​of the attr3 attribute)
  • Contents of the text file: filename.ext, attr3 (where filename.ext is the name of the document with its extension (.doc, .docx, etc.), attr3 is one of the multiple values ​​of the attr3 attribute)
  • Target folder: /Received Emails/Sent/prot (prot is the prot attribute)

Updates the metadata of the new text file.
Applies the template sendings and sets some metadata:

  • prot (copying the prot attribute of the original document)
  • dest-ind (copying the attr3 attribute that has multiple values)
  • date-sent

Replicate workflow annotations to document's notes

# iterate over the documents attached to the current workflow
#foreach( $doc in $documents ) 
  # retrieve the last task completion event
  #set($lastEvent = $WorkflowTool.getLastHistory($processId,'task.end'))
  
  # apply the 'rejected' stamp and move the file to the Approved subfolder
  $StampTool.stamp($doc, 'approved', $lastEvent.user.username);
  $DocTool.move($doc, 'Approved', $lastEvent.user.username);
  
  # make a pdf conversion of the approved file an store it in a public area
  #set($docConverted = $DocTool.convert($doc, "pdf", $lastEvent.user.username))
  $DocTool.move($docConverted, "/AreaTEST/TEST MERITOR/PUBLIC/Approved", $lastEvent.user.username);
  
  # get the collection of all annotation done by the participants in the current workflow
  #set($histories = $WorkflowTool.getHistories($processId, 'task.note'))
  $log.info("histories: $histories")
  $log.info("histories.size: $histories.size()")
  
  # iterate over the annotations and replicate them in the document
  #set($noteDAO = $ContextTool.getBean('DocumentNoteDAO'))
  #foreach( $hist in $histories )      
      # create the new note to be saved for the document
      #set($note = $ClassTool.newInstance('com.logicaldoc.core.document.DocumentNote'))

      # prepare all the metadata and save to database
      $note.setDocId($doc.id);
      $note.setUsername($hist.username);
      $note.setMessage($hist.comment);
      $note.setDate($doc.date);
      $noteDAO.store($note, null);
  #end
#end

This procedure is designed to be executed inside a workflow, the current workflow's instance identifier is stored in the variable processId.

For each document attached, to the workflow the approved stamp is applied and a pdf conversion is stored in a public folder.

Once the workflow has been completed, the annotations left by the users stay in the history of the forkflow(inside the workflow dashboard), so in order to have them more easily accessible by the normal users, each annotation is replicated into the documents as notes.

Splits paychecks and sends them by email to employees

# split the big payolls pdf in segments of two pages
#set( $paychecks = $SplitTool.splitSelection($document.id, '1-2,3-4,5-6,7-8', 'admin'))

# iterate over the segments, each segment is the paycheck of a single employee
#foreach( $paycheck in $paychecks )
  # parse the segment body and save it in the paycheckBody variable
  #set( $paycheckBody  =  $ContextTool.getBean('DocumentManager').parseDocument($paycheck, null) )
  
  # inspect the body of the segment to understand what is the employee
  # in order to rename the segment file and define the email to send the paycheck to 
  #if($paycheckBody.toUpperCase().contains("JOHN DOE") )
     #set( $newname = $document.title + "-john.pdf")
     #set( $recipient = "john[at]acme.com")
  #elseif($paycheckBody.toUpperCase().contains("MARIO ROSSI")  )
     #set( $newname = $document.title + "-mario.pdf")
     #set( $recipient = "mario[at]acme.com")
  #elseif( $paycheckBody.toUpperCase().contains("ELENA BORGHI") )
     #set( $newname = $document.title + "-elena.pdf")
     #set( $recipient = "elena[at]acme.com")
  #elseif( $paycheckBody.toUpperCase().contains("ALDO MORO") )
     #set( $newname = $document.title + "-aldo.pdf")
     #set( $recipient = "aldo[at]acme.com")
  #end

 # rename the current segment(the user's paycheck pdf)
 $paycheck.setFileName($newname);
 $DocTool.store($paycheck); 

 # notify the employee sending him his paycheck document
 $MailTool.sendDocument($paycheck, null, $recipient, "Acme Paycheck", "Dear employee, please find your new paycheck in attachment of this email.

Remember to save it in your records

Best Regards
Acme paycheck department"); #end

This script works on the currently selected document referenced by the variable document that is considered a big PDF containing all the paychecks of a months.

The document is splitted in segments of two pages each segment is saved as a single payroll PDF and it's content is inspected to discover what is the employee the payroll belongs to in order to send it to him by email.

Automation Snippets

# iterate over the documents
#foreach( $doc in $documents ) 
... 
#end

--------------------------------------------------------------------------------
# move the documents into a target folder computed dinamically
#foreach( $doc in $documents )
  # get the custom attribue 'InvoiceNo' and store it in the fld variable
  #set( $fld = $doc.getValue('InvoiceNo') )
  
  # move the document to a path that is composed by a fixed prefix and the fld value
  $DocTool.move($doc, "/Default/approved/$fld", '_system');
#end

--------------------------------------------------------------------------------
# apply a stamp in the documents
#foreach( $doc in $documents )
 $StampTool.stamp($doc, 'name_of_stamp', 'username');
#end

--------------------------------------------------------------------------------
# get the value of a procedure's invocation parameter
#set($value = $parameters.get('param_name') ) -------------------------------------------------------------------------------- # print logs (useful for debuging) # the log is printed in the file /repository/logs/automation.log #set($now = $ClassTool.newInstance('java.util.Date')) $log.debug( "debug current date: $DateTool.format($now, true)"); #set($lastEvent = $WorkflowTool.getLastHistory($processId,'task.end')) $log.info("The user that completed the last task was: $lastEvent.user.fullName"); $log.warn( "Just print a row in warn priority"); $log.error( "Just print a row in error priority"); -------------------------------------------------------------------------------- # send documents to a set of recipients #set( $recipients= ["a[at]acme.com", "b[at]acme.com", "c[at]acme.com"] ) $MailTool.sendDocuments($documents, 'sender[at]acme.com', $recipients, 'subject', 'message'); -------------------------------------------------------------------------------- # execute a routine $AutomationTool.execute('Routine', $tenantId, $dictionary, null); -------------------------------------------------------------------------------- # change an extended attribute #foreach( $doc in $documents ) $doc.setValue('attribute_name','attribute_value'); $DocTool.store($doc); #end -------------------------------------------------------------------------------- # adding a note #set($note = $ClassTool.newInstance('core.document.DocumentNote')) #set($noteDao = $ContextTool.getBean('DocumentNoteDAO')) $note.setDocId($document.id); $note.setUsername('admin'); $note.setMessage('test note'); $noteDao.store($note, null); -------------------------------------------------------------------------------- # automatically choose a workflow transition depending on attribute's value #foreach( $doc in $documents ) #set($amount = $doc.getValue("myAttributeName")) #if( $amount > 50000 ) $WorkflowTool.complete($task, "Approve Document") #break #else $WorkflowTool.complete($task, "Reject Document") #break #end #end -------------------------------------------------------------------------------- # reverse foreach #set($max = $opportunityList.size() - 1) #foreach($i in [ $max .. 0 ]) #set($opportunity = $opportunityList[$i]) ... #end -------------------------------------------------------------------------------- # digitally sign a selection of documents #foreach($doc in $documents ) $SignTool.sign($doc, $assignee, "Approved") #end

Not really a complete script but a collection of snippets.

Custom email template

A customization of the email template used to notify users about workflow tasks. This proposal differs from the standard template because it includes the list of annotations left on the workflow by the other users. In this script you can see the usage of the I18N class to print localized labels. The current workflow instance is referenced by the variable processId.

Classifies a document on the basis of custom attributes

#set($templateDAO = $ContextTool.getBean('TemplateDAO'))

# retrieve the template that will be assigned to the document
#set($privateTemp = $templateDAO.findByName('TaxLienServicing'))

# store the document's path into the variable T_Path
#set($T_Path=$DocTool.getPath($document))

# get a couple of custom attributes from the document
#set($T_NewType=$document.getValue("DT-LegacyWebLT"))
#set($T_ClientName=$document.getValue("ClientName"))

# Now inspect the value of the DT-LegacyWebLT attribute to move the file to the proper folder
#if($T_NewType=="Import")
  $log.info("Reclassify triggered but no new document type selected");
#elseif ($T_NewType=="Payoff Requests Quotes")
  #set ($T_NewFG="Call Center - Payoff Requests Quotes")
  
  # set the correct template and update a custom attribute
  $document.setTemplate($privateTemp);
  $document.setValue("FunctionalArea","$T_NewFG");
  
  # make the changes persisten into the database
  $DocTool.store($document);
  
  # move to a target folder caclulated dynamically
  $DocTool.move($document,"/$T_ClientName/$T_NewFG/",'_system');

  # exit the procedure
  #stop
#elseif (($T_NewType=="Lockbox Backup") || ($T_NewType=="2 day letters"))
  #set ($T_NewFG="Payment Processing - Lockbox")
  $document.setTemplate($privateTemp);
  $document.setValue("FunctionalArea","$T_NewFG");
  $DocTool.store($document);
  $DocTool.move($document,"/$T_ClientName/$T_NewFG/",'_system')
  #stop
#elseif ($T_NewType=="TN Reconciliation")
  #set ($T_NewFG="Payment Processing - Reconciliation")
  $document.setTemplate($privateTemp);
  $document.setValue("FunctionalArea","$T_NewFG");
  $DocTool.store($document);
  $DocTool.move($document,"/$T_ClientName/$T_NewFG/",'_system');
  #stop
#elseif ($T_NewType=="TX Loan")
  #set ($T_NewFG="Payment Processing - Texas Loans")
  $document.setTemplate($privateTemp);
  $document.setValue("FunctionalArea","$T_NewFG");
  $DocTool.store($document);
  $DocTool.move($document,"/$T_ClientName/$T_NewFG/",'_system');
  #stop
#elseif ($T_NewType=="RIE")
  #set ($T_NewFG="Payment Processing - Received In Error")
  $document.setTemplate($privateTemp);
  $document.setValue("FunctionalArea","$T_NewFG");
  $DocTool.store($document);
  $DocTool.move($document,"/$T_ClientName/$T_NewFG/",'_system');
  #stop
#elseif ($T_NewType=="Checks")
  #set ($T_NewFG="Payment Processing - Refunds")
  $document.setTemplate($privateTemp);
  $document.setValue("FunctionalArea","$T_NewFG");
  $DocTool.store($document);
  $DocTool.move($document,"/$T_ClientName/$T_NewFG/",'_system');
  #stop
#elseif ($T_NewType=="Client Reports" ||  $T_NewType=="Reconciliation")
  #set ($T_NewFG="Reporting")
  $document.setTemplate($privateTemp);
  $document.setValue("FunctionalArea","$T_NewFG");
  $DocTool.store($document);
  $DocTool.move($document,"/$T_ClientName/$T_NewFG/",'_system');
  #stop
#else 
  $log.info("Not in main $document.fileName");
#end

$log.info("Running 2")
#if ($T_NewType=="Proof of Purchase")  
  #set ($T_NewFG="Custodial - Imports")
  $document.setTemplate($privateTemp);
  $document.setValue("FunctionalArea","$T_NewFG");
  $DocTool.store($document);
  $DocTool.move($document,"/$T_ClientName/$T_NewFG/",'_system');
  #stop
#elseif ($T_NewType=="Redemption Notice" || $T_NewType=="Verification Request" || $T_NewType=="Redemption Packet" || $T_NewType=="Redemption Affidavit")
  #set ($T_NewFG="Custodial - Redemption Processing")
  $document.setTemplate($privateTemp);
  $document.setValue("FunctionalArea","$T_NewFG");
  $DocTool.store($document);
  $DocTool.move($document,"/$T_ClientName/$T_NewFG/",'_system');
  #stop
#elseif ($T_NewType=="Lien Release")
  #set ($T_NewFG="Custodial - Release Processing")
  $document.setTemplate($privateTemp);
  $document.setValue("FunctionalArea","$T_NewFG");
  $DocTool.store($document);
  $DocTool.move($document,"/$T_ClientName/$T_NewFG/",'_system');
  #stop
#elseif ($T_NewType=="Certs-Received, Not Processed" || $T_NewType=="Certs-Stickered and Recorded" || $T_NewType=="Certs-Stickered Only" || $T_NewType=="Deed")
  #set ($T_NewFG="Custodial - Collateral Management")
  $document.setTemplate($privateTemp);
  $document.setValue("FunctionalArea","$T_NewFG");
  $DocTool.store($document);
  $DocTool.move($document,"/$T_ClientName/$T_NewFG/",'_system');
  #stop
#elseif ($T_NewType=="Assignment Form" || $T_NewType=="List of Property")
  #set ($T_NewFG="Custodial - Lien Assignment Packets")
  $document.setTemplate($privateTemp);
  $document.setValue("FunctionalArea","$T_NewFG");
  $DocTool.store($document);
  $DocTool.move($document,"/$T_ClientName/$T_NewFG/",'_system');
  #stop
#else 
  $log.info("Not in Reclassify2 $document.fileName");
#end

$log.info("Running 3")
#if ($T_NewType=="Hello Letters" || $T_NewType=="Goodbye Letters" || $T_NewType=="Demand Letter" || $T_NewType=="Collection Letter" || $T_NewType=="General Collection Letter" || $T_NewType=="Annual Notice" || $T_NewType=="Take Notices" || $T_NewType=="Extension Letter" || $T_NewType=="10 Day Notice")
  #set ($T_NewFG="Asset Management - Noticing")
  $document.setTemplate($privateTemp);
  $document.setValue("FunctionalArea","$T_NewFG");
  $DocTool.store($document);
  $DocTool.move($document,"/$T_ClientName/$T_NewFG/",'_system');
  #stop
#elseif ($T_NewType=="Notice of Filing" || $T_NewType=="Notice of Hearing" || $T_NewType=="Notice of Discharge" || $T_NewType=="Notice of Dismissal" || $T_NewType=="Proof of Claim" || $T_NewType=="Motion to Lift Stay" || $T_NewType=="Motion to Appoint Trustee" || $T_NewType=="Motion to Dismiss" || $T_NewType=="Debtor's Motion to Extend Exclusive Period"|| $T_NewType=="Disclosure Statement" || $T_NewType=="Confirmed Plan" || $T_NewType=="Reorganization Plan" || $T_NewType=="Objections to Plan")
  #set ($T_NewFG="Asset Management - Bankruptcy")
  $document.setTemplate($privateTemp);
  $document.setValue("FunctionalArea","$T_NewFG");
  $DocTool.store($document);
  $DocTool.move($document,"/$T_ClientName/$T_NewFG/",'_system');
  #stop
#elseif ($T_NewType=="Project Status Report")
  #set ($T_NewFG="Project Status Report")
  $document.setTemplate($privateTemp);
  $document.setValue("FunctionalArea","$T_NewFG");
  $DocTool.store($document);
  $DocTool.move($document,"/$T_ClientName/$T_NewFG/",'_system');
  #stop
#else 
  $log.info("Not in Reclassify3 $document.fileName");
#end

This procedure analyzes the document(variable document) and classifies it depending on the value of a custom attribute. The classification implies the update of some metadata and the move to a specific folder calculated dynamically.