Copy the file content from one attribute to a collection

<< Click to Display Table of Contents >>

Navigation:  Bizagi Studio > Process wizard > Business Rules > Business rules examples > File management with expressions >

Copy the file content from one attribute to a collection

It is possible to copy the content of a file-type attribute to another of the same type contained in a collection.

 

Imagine a process where documents are uploaded along the process. The process keeps a log of uploads; a collection stores the files, dates of upload and users that uploaded them as shown bellow.

 

 

AdvancedFilesManagement19

 

A single file-type attribute is used to upload documents to each activity of the process. Each time an activity is finished, the files uploaded are copied into the collection and the file-type attribute is then initialized to be reused in the next activity.

 

 

 

AdvancedFilesManagement20

 

 

 

The data model shows the file-type attribute used to upload the files (ActivityDocument) and the collection that keeps the log (Documents). Note the collection has another file-type attribute used to store the documents (DocumentFile).

 

AdvanceFilesManagement15

 

 

To copy the documents from the file-type attribute to the collection you can use the following expression:

 

AdvancedFilesManagement21

 

//Create a new record in the collection

var NewDocument= Me.addRelation("Process.Documents")

//Add the additional information to the new record (upload date and uploaded by)

NewDocument.setXPath("UploadDate", DateTime.Now)

NewDocument.setXPath("Uploadedby", Me.Case.WorkingCredential.UserId)

 

//Get the data of the file-type attribute used to upload the document

var OriginalFile = Me.getXPath("Process.ActivityDocument");

 

//Go through the array of files

for(var i=0; i < OriginalFile.size(); i++)

{

//Obtain the file data

      var FiletoCopy = OriginalFile.get(i);

      var Name = FiletoCopy.getXPath("FileName");

      var Data = FiletoCopy.getXPath("Data");

//Copy the file in the file-type attribute of the collection

      var NewFile = NewDocument.addRelation("DocumentFile");

      NewFile.setXPath("FileName", Name);

      NewFile.setXPath("Data", Data);

}

//Intitialize the file-type attribute

Me.deleteAllCollectionItems("Process.ActivityDocument");

 

The expression above creates a new record in the destination collection using the addRelation method. Then obtains the array of files stored in the ActivityDocument file-type attribute and navigates through this to obtain the name and data of the file to be copied.

Finally each file name and content is copied to the DocumentFile attribute. Note Me.addRelation changes in the first variable to the related to the collection (NewDocument.addRelation).