Double generation by a template

Double generation by a template can be used when creating a document in the system.
For example, you use a template for generating a document. At this moment, the document is not registered yet, therefore the registration number is not inserted into the generated file. You need the registration number to be inserted into the file automatically.

This can be implemented using a business process:

 

 

Let's take a look at each activity.

1. Select Document activity. Add a variable of a document type on the Context tab. This variable will store the final document version.

2. Primary Generation by Template activity. On the Settings tab, add a template and a File type variable, to which the generated document will be saved.

 

Since the generation is performed in two steps, the template will look like this:
Number{‘{$Document.RegistrationCard[0].RegNumber}’}
The first braces will be expanded at the fist generation, and the second - at the second generation.

3. Change Version Script activity. A script for adding a new version (File context variable) to the document (Document context variable) without using PublicAPI.

For the script to work correctly, add the assemblies:

Elewise.ELMA.Documents; Elewise.TemplateGenerator.Merged;

Namespaces:

using EleWise.ELMA.Documents.Managers;
using EleWise.ELMA.Runtime.Managers;
using EleWise.ELMA.Services; 
using EleWise.ELMA.Templates;
 

Script text:

var version = InterfaceActivator.Create<DocumentVersion>();
version.Document = context.Document;
version.File=context.File;
version.Name = "generated version";
version.Status = DocumentVersionStatus.Current;//set the Current status to the version
version.File.Name="file1.rtf";
context.Document.Versions.Add(version);
context.Document.Save(); 
 

Before executing the script, make sure that the File and Document variables are filled in.

Script with PublicAPI.   

Context variables: 

context.Document - variable of the Document type
context.File - variable of the File type

Namespace:

using EleWise.ELMA.API;
 

Script text:

var DocVersion = PublicAPI.Docflow.DocumentVersion.AddDocumentVersion(context.Document, context.File,PublicAPI.Enums.Documents.DocumentVersionStatus.Current);
PublicAPI.Docflow.DocumentVersion.RenameVersionFile(DocVersion, "file1.rtf", false);
 

4. Register Document activity. At this stage a registration number is created, which will be used in the template.

5. Second Generation by Template activity. The second generation is performed with a script; also, the document version is changed in this activity. The document, generated during the primary generation is used as a template.

Script without using PublicAPI.   

For the script to work correctly, add assemblies:

Elewise.ELMA.Documents; Elewise.TemplateGenerator.Merged;

Namespaces:

using EleWise.ELMA.Documents.Managers;
using EleWise.ELMA.Runtime.Managers;
using EleWise.ELMA.Services; 
using EleWise.ELMA.Templates;
 

Script text: 

var version = InterfaceActivator.Create<DocumentVersion>();
version.Document = context.Document;
var versionFile = Locator.GetServiceNotNull<IDocumentFileManager>().GetFileByDocumentId(context.Document.Id); 
var file = Locator.GetServiceNotNull<ITemplateFileManager>().CreateFromTemplate(versionFile.Id); 
// Create a generator 
var generator = Locator.GetServiceNotNull<DocumentGeneratorManager>().Create(file.ContentFilePath); 
// Generate a document 
generator.Generate(context); 
// Save the generated file
Locator.GetServiceNotNull<IFileManager>().SaveFile(file); 
// Write the result to a variable 
version.File=file;
version.Name = "generated version";
version.Status = DocumentVersionStatus.Current;//set the Current status to the version
version.File.Name="file2.rtf";
context.Document.Versions.Add(version);
context.Document.Save();
 
Creating a similar script with PublicAPI
Documentation on PublicAPI is available here

 

Namespace:

using EleWise.ELMA.API;
 

Script:

var versionFile = Locator.GetServiceNotNull<IDocumentFileManager>().GetFileByDocumentId(context.Document.Id);
var file = Locator.GetServiceNotNull<ITemplateFileManager>().CreateFromTemplate(versionFile.Id);
// Generate a document
PublicAPI.Services.DocumentGenerator.Generate(file, context);
// Save the generated file
PublicAPI.Services.File.SaveFile(file);
// Write the result to the variable
var DocVersion = PublicAPI.Docflow.DocumentVersion.AddDocumentVersion(context.Document, file, PublicAPI.Enums.Documents.DocumentVersionStatus.Current);
PublicAPI.Docflow.DocumentVersion.RenameVersionFile(DocVersion, "file2.rtf", false);
 

 

 
6. Display Document activity. On the context tab add the variable of the Document type (used in the Select Document operation). The variable will display the document.
Once the process is complete, the registration number will be added to the document:
Number B1-CRP-17-001