Open Documentation Menu

Managing and processing DMS documents

This section shows you how to work with DMS documents in Apex and manage and process DMS documents in a simple and efficient way.

Contents
Authentication in d.velop documents

This chapter shows you how to authenticate the DocumentManager class in d.velop documents and explains why this is advisable.

Contents
Background

Pre-authentication is not absolutely necessary to use the DocumentManager class methods in your custom code. However, we do recommend that you add a valid user session to an instance once using the different authentication methods.

This ensures that a valid session exists and that all outgoing HTTP requests to d.velop documents are performed in the context of the respective user.

It also avoids having to re-validate a user session cached in the Salesforce Platform Cache in the Identity Provider app. In this case, you may need to re-request the user session, potentially leading to a Mixed DML Operation error in Salesforce.

Class authentication

The following example shows you how to authenticate the DocumentManager class quickly and easily.

// Create the document manager
dvelop_docs_dev.DocumentManager documentManager = new dvelop_docs_dev.DocumentManager();

// Pick one of the authentication methods and authenticate
documentManager.authenticateInCurrentUserContext();

// Perform an operation
// ...
See also
Updating a DMS document

This chapter shows you how to update a DMS document’s properties and categories.

Contents
Updating a document

AttributYou can update a DMS document using the DocumentManager class updateDocument(documentId, updatedCategory, updatedAttributes) method.

The following example shows you how to use the method.

// Create a new manager instance
dvelop_docs_dev.DocumentManager documentManager = new dvelop_docs_dev.DocumentManager();
 
// Authenticate against connected DMS
documentManager.authenticateInCurrentUserContext();
 
// Define the updated details
List<dvelop_docs_dev.DocumentAttribute> updatedAttributes = new List<dvelop_docs_dev.DocumentAttribute>{
    new dvelop_docs_dev.DocumentAttribute('objecttitle', 'Ja'),
    new dvelop_docs_dev.DocumentAttribute('multivalue_property__c', new List<String>{ 'A', 'B' })
};
 
// Perform the action - Update the document
documentManager.updateDocument('XH00014213', 'correspondence__c', updatedAttributes);
Error trapping

When you update a document, you may receive the following error message: “A mapping is not possible because several values were transferred to the same destination”.

The following shows you the possible error causes and solutions:

An individual attribute appears in the attribute list multiple times.

Ensure that the transferred list contains each attribute just once. You can remove identical attributes by converting the list into a set. You can then convert the set back into a list.

Multiple source properties are assigned to the same d.3 target field

Check the mappings and remove any duplicates.

See also
Retrieving a DMS document

This chapter shows you how to determine a DMS document’s properties and categories from the connected DMS.

Contents
Retrieving a document

You can retrieve an individual document from the DMS using the DocumentManager class getDocument(documentId) method.

The following example shows you how to use the method.

// Create a new manager instance
dvelop_docs_dev.DocumentManager documentManager = new dvelop_docs_dev.DocumentManager();
 
// Authenticate against connected DMS
documentManager.authenticateInCurrentUserContext();
 
// Perform the action - Get the document and work with ouput
dvelop_docs_dev.Document dmsDocument = documentManager.getDocument('XH00014213');
System.debug(dmsDocument);
See also
Creating a folder

This chapter shows you how to create a folder.

Contents
Creating a folder

You can create a folder in the DMS using the DocumentManager class createFolder(category, attributes) method.

The following example shows you how to use the method.

// Create a new manager instance
dvelop_docs_dev.DocumentManager documentManager = new dvelop_docs_dev.DocumentManager();
 
// Authenticate against connected DMS
documentManager.authenticateInCurrentUserContext();
 
// Define the details
List<dvelop_docs_dev.DocumentAttribute> attributes = new List<dvelop_docs_dev.DocumentAttribute>{
    new dvelop_docs_dev.DocumentAttribute('objecttitle', 'Account4711'),
    new dvelop_docs_dev.DocumentAttribute('objectkey', '001AW00000B53c5YAB'),
    new dvelop_docs_dev.DocumentAttribute('account', 'Account4711'),
    new dvelop_docs_dev.DocumentAttribute('accountnumber', '001AW00000B53c5YAB'), 
    new dvelop_docs_dev.DocumentAttribute('foldertype', 'Account')
};
 
// Perform the action - Create the folder
documentManager.createFolder('akte_safo__c',  attributes);
See also
Downloading DMS documents

This chapter shows you how to download documents from the DMS and attach them to a record.

Contents
Technical limitations

As with all Apex transactions, you also have to keep the Salesforce governor limits in mind when downloading documents. The following technical limitations with regard to the size of HTTP responses apply:

  • In a synchronous transaction, only documents with a maximum file size of 6 MB can be downloaded.

  • In an asynchronous transaction, only documents with a maximum file size of 12 MB can be downloaded.

Downloading a document

You can download an individual document from the DMS using the DocumentManager class downloadDocument(documentId) and downloadDocumentToRecord(recordId, documentId) methods.

The following examples show you how to use the two methods.

Downloading a document – Processing the document
// Create a new manager instance
dvelop_docs_dev.DocumentManager documentManager = new dvelop_docs_dev.DocumentManager();
 
// Authenticate against connected DMS
documentManager.authenticateInCurrentUserContext();
 
// Perform the action - Download the document and work with ouput
dvelop_docs_dev.DocumentDownloadResult downloadResult = documentManager.downloadDocument('XH00014213');
System.debug(downloadResult.getFilename());
System.debug(downloadResult.getBody());
Downloading a document – Saving the document to the record
// Create a new manager instance
dvelop_docs_dev.DocumentManager documentManager = new dvelop_docs_dev.DocumentManager();
 
// Authenticate against connected DMS
documentManager.authenticateInCurrentUserContext();
 
// Perform the action - Download the document and attach it to a record
Id createdContentDocumentId = documentManager.downloadDocumentToRecord('001Aa0000059kyPIAQ', 'XH00014213');
System.debug(createdContentDocumentId);
Downloading multiple documents

You can download multiple documents with an action from the DMS using the DocumentManager class downloadDocuments(documentIds,finisher, proceedOnFailure) method.

Since the method downloads multiple potentially large files for the DMS documents, the downloads are performed in a queue in an asynchronous process. To ensure you can respond to the completion of the queue and access the downloaded documents, you must transfer a queueable job of the type dvelop_docs_dev.AsyncProcessFinisher when calling the method. This job is added to the queue following the completion of the last download. The output (the downloaded documents) is transferred from dvelop_docs_dev.DocumentDownloadResults as a list.

The following example shows you how to define a finish job to download multiple documents asynchronously:

Downloading multiple documents – Defining the finish job 
public class DownloadProcessFinishJob extends dvelop_docs_dev.AsyncProcessFinisher {
    private static final String DEFAULT_PARAM_NAME = 'downloadResults';
 
    public DownloadProcessFinishJob() {
        // Call of parent constructor with an identifier for the default parameter name
        super(DEFAULT_PARAM_NAME);
    }
     
    public override void execute(Map<String, Object> processOutput) {
        // Read the default parameter and cast to correct type
        Object defaultParamOutput = processOutput.get(DEFAULT_PARAM_NAME);
        List<dvelop_docs_dev.DocumentDownloadResult> downloadResults = (List<dvelop_docs_dev.DocumentDownloadResult>) defaultParamOutput;
         
        // Process the results
        for (dvelop_docs_dev.DocumentDownloadResult result : downloadResults) {
            System.debug(result.getFilename());
            System.debug(result.getBody());
        }
    }
}
Downloading multiple documents – Starting the process 
// Create a new manager instance
dvelop_docs_dev.DocumentManager documentManager = new dvelop_docs_dev.DocumentManager();
 
// Authenticate against connected DMS
documentManager.authenticateInCurrentUserContext();
 
// Defining download params
List<String> documentIds = new List<String>{ 'XH00014226', 'XH00014222', 'XH00014225' };
DownloadProcessFinishJob finishJob = new DownloadProcessFinishJob();
 
// Perform the action - Download the documents in a queue
documentManager.downloadDocuments(documentIds, finishJob, false);
See also
Deleting DMS documents

This chapter shows you how to delete documents and folders from the DMS.

Contents
Deleting a DMS item

You can delete a document or item from the DMS using the DocumentManager class deleteDocument(documentId, ?reason) method.

The following example shows you how to use the method.

// Create a new manager instance
dvelop_docs_dev.DocumentManager documentManager = new dvelop_docs_dev.DocumentManager();
 
// Authenticate against connected DMS
documentManager.authenticateInCurrentUserContext();
 
// Perform the action - Delete the document
documentManager.deleteDocument('XH00014213', 'My custom reason');
See also
Searching for DMS documents

This chapter shows you how to search for and find documents for a specific Salesforce record or Salesforce item in the DMS.

Contents
Searching for DMS documents

This example shows you how to perform a simple search for documents on a specific record using the DocumentManager class search(options) method.

By default, the search criteria used to restrict the documents are determined based on the settings configured in the d.velop documents configuration.

Note

The search(options) method requires you to enter a DocumentSearchOptions class instance.

The Using “DocumentSearchOptions.Builder” class section shows you how to create simplified search parameters. 

Searching for DMS documents – Simple search 
// Create a new manager instance
dvelop_docs_dev.DocumentManager documentManager = new dvelop_docs_dev.DocumentManager();
  
// Authenticate against connected DMS
documentManager.authenticateInCurrentUserContext();
 
// Define the search options
dvelop_docs_dev.DocumentSearchOptions searchOptions = new dvelop_docs_dev.DocumentSearchOptions();
searchOptions.useRecordContext('00011100001tBgaxAAC');
  
// Perform the action - Search for documents using the defined options
List<dvelop_docs_dev.Document> documents = documentManager.search(searchOptions);
Searching for DMS documents – Identifying all the documents in a Salesforce record 
dvelop_docs_dev.DocumentManager documentManager = new dvelop_docs_dev.DocumentManager();
documentManager.authenticateInCurrentUserContext();
 
dvelop_docs_dev.DocumentSearchOptions searchOptions = new dvelop_docs_dev.DocumentSearchOptions();
searchOptions.useRecordContext('00011100001tBgaxAAC');
 
List<dvelop_docs_dev.Document> documents = documentManager.search(searchOptions);
Searching for DMS documents – Identifying all the documents in a Salesforce item 
dvelop_docs_dev.DocumentManager documentManager = new dvelop_docs_dev.DocumentManager();
documentManager.authenticateInCurrentUserContext();
 
dvelop_docs_dev.DocumentSearchOptions searchOptions = new dvelop_docs_dev.DocumentSearchOptions();
searchOptions.useObjectContext('Account');
 
List<dvelop_docs_dev.Document> documents = documentManager.search(searchOptions);
Searching for DMS documents – Identifying all the documents in a Salesforce item with overridden search criteria 
dvelop_docs_dev.DocumentManager documentManager = new dvelop_docs_dev.DocumentManager();
documentManager.authenticateInCurrentUserContext();
 
dvelop_docs_dev.DocumentSearchOptions searchOptions = new dvelop_docs_dev.DocumentSearchOptions();
searchOptions.useObjectContext('Account');
searchOptions.addSearchAttribute('objectkey', '0100X00QGA');
searchOptions.addSearchAttribute('string1', 'Max Mustermann');
 
List<dvelop_docs_dev.Document> documents = documentManager.search(searchOptions);
Searching for documents using IDs

You can also identify multiple documents based on their IDs. You can do this using the DocumentManager class searchDocumentsWithIds(searchAttributeKey, documentIds) method.

dvelop_docs_dev.DocumentManager docManager = new dvelop_docs_dev.DocumentManager();
docManager.authenticateInCurrentUserContext();
 
List<String> documentIds = new List<String>{ 'C200000024', 'C200000023' };
 
List<dvelop_docs_dev.Document> documents = docManager.searchDocumentsWithIds('externaldocumentnumber', documentIds);
See also
Sharing DMS documents

This chapter shows you how to share DMS documents with other people.

Contents
Creating links for sharing documents

You can create links for sharing documents using the DocumentManager class shareDocuments(downloadUrls) method.

The following example shows you how to use the method.

// Create a new manager instance
dvelop_docs_dev.DocumentManager documentManager = new dvelop_docs_dev.DocumentManager();
 
// Authenticate against connected DMS
documentManager.authenticateInCurrentUserContext();

// Get a document
dvelop_docs_dev.Document dmsDocument = documentManager.getDocument('FP00000010');
 
// Define the urls
Set<String> downloadUrls = new Set<String>{
    dmsDocument.downloadUrl
};
 
// Perform the action - Create the temporary links
Map<String, String> linksByDownloadUrl = documentManager.shareDocuments(downloadUrls);
System.debug(linksByDownloadUrl.get(dmsDocument.downloadUrl));
See also
Sending e-mails with attachments

This chapter shows you how to send e-mails with Salesforce and DMS documents as attachments.

Contents
Technical limitations

As with all Apex transactions, you also have to keep the Salesforce governor limits in mind when sending e-mails with documents. Due to the maximum heap size (magnitude of dynamic data storage) for e-mail services, the following technical limitations apply:

  • The maximum permitted size of the e-mail attachments together is 36 MB.

  • Since DMS documents must be temporarily downloaded from the DMS before sending them, the same technical limitations as described in the Downloading DMS documents chapter also apply.

Sending an e-mail

You can send documents from Salesforce and the DMS via e-mail using the DocumentManager class sendEmail(options) method.

The following example shows you how to use the method.

// Create a new manager instance
dvelop_docs_dev.DocumentManager documentManager = new dvelop_docs_dev.DocumentManager();
   
// Authenticate against connected DMS
documentManager.authenticateInCurrentUserContext();
  
// Define the email options
dvelop_docs_dev.DocumentEmailOptions emailOptions = new dvelop_docs_dev.DocumentEmailOptions();
emailOptions.setSubject('Important Message');
emailOptions.setBody('Have a look at this.');
emailOptions.addRecipient('astro@salesforce.com');
emailOptions.addContentDocumentId('069AP0000006hQbYAI');
emailOptions.addDmsDocumentId('XH00014846');
   
// Perform the action - Send an email with documents from Salesforce and the DMS
documentManager.sendEmail(emailOptions);
See also