Tutorials

Using Automation

Updated: February 22, 2017 Page Information Edit on GitHub

Goal

Leverage Automation and trigger business logic to obtain the contract and all of its attachments as a single PDF file.

Prerequisites

Procedure

  1. Create a file called generateContractWithAttachments.js  with the following content. Be sure to replace the filePath variable with the path to the folder where the PDF file should be downloaded.

    let Nuxeo = require('nuxeo');
    let fs = require('fs');
    let nuxeo = new Nuxeo({
      auth: {
        method: 'basic',
        username: 'sconnor',
        password: 'sconnor'
      }
    });
    
    // Change this value to indicate in which folder the full PDF contract should be downloaded
    // Don't forget the trailing slash
    let filePath = '/tmp/';
    // Which contract to fetch a rendition for
    let contractToFetch = '/default-domain/workspaces/North America/awesome-tech/awesome-contract';
    // Launch the automation chain to generate the final PDF file
    console.log('We\'ll launch an automation chain.');
    console.log('This chain will:');
    console.log('- Gather the contract and all of its attachments');
    console.log('- Convert them to PDF');
    console.log('- And concatenate them all in a single file.');
    nuxeo.operation('BCGenerateCompletePDFContract_ac')
      .input(contractToFetch)
      .execute()
      .then(response => {
        // Download file
        console.log('The file has been generated, we\'ll download it now.');
        try {
          var stats = fs.statSync(filePath);
          if (!stats.isDirectory()) {
            console.log(`${filePath} is is not a folder.\nPlease check the filePath variable (currently set to: ${filepath} )\nand make sure you have the proper rights on that folder.`);
            return;
          }
          const writable = fs.createWriteStream(`${filePath}contract-with-attachments.pdf`);
          response.body.pipe(writable);
          console.log('The complete PDF contract has been downloaded!');
          console.log(`You can take a look at it here: ${filePath}contract-with-attachments.pdf`)
        } catch (error) {
          console.log(`The folder where the rendition should be downloaded cannot be accessed.\nPlease check the filePath variable (currently set to: ${filePath})\nand make sure you have write access on that folder.`);
          console.log(error);
          return;
        }
      })
      .catch(error => {
        console.log('Apologies, an error occurred while generating the final PDF file.');
        console.log(error);
        return;
      });
    
  2. Save and run:

    $ node generateContractWithAttachments.js
    

Automation chains allow you to optimize the number of calls needed to execute business logic while maintaining data integrity. All automation chains are executed in a transaction: if the chain fails at some point, it will be rolled back to keep your data safe.

Learn more

You have now completed this tutorial. To start discovering how to customize the Nuxeo Platform, check our Quick Start Dev Guide.

a month ago Manon Lumeau fix image on quick overview page
6 months ago Solen Guitter 18
6 months ago Solen Guitter 17
6 months ago Solen Guitter 16
6 months ago Solen Guitter 15
6 months ago Solen Guitter 14
6 months ago Solen Guitter 13
6 months ago Solen Guitter 12
6 months ago Solen Guitter 11
6 months ago Solen Guitter 10
6 months ago Solen Guitter 9
7 months ago Arnaud Kervern 8
7 months ago Solen Guitter 7
7 months ago Solen Guitter 6
7 months ago Bertrand Chauvin 4
7 months ago Bertrand Chauvin 5
7 months ago Bertrand Chauvin 3
8 months ago Bertrand Chauvin 2
8 months ago Bertrand Chauvin 1
History: Created by Bertrand Chauvin