How to Access Large Document in Chunks from SpringCM Using Apex

August 12, 2016 Appirio

By Satyanarayan Choudhary

SpringCM is a cloud-based enterprise content management system that lets you manage contracts, documents, and all types of content in Salesforce. SpringCM provides access to content for desktop, web, and all mobile platforms.

How to use SpringCM for all standard or custom objects

  • We can add SpringCM to any Salesforce standard or custom object so that we can have documents associated with those objects. This makes it easy to view or manage a document within the object record.
  • SpringCM provides a separate folder for each object record, so the target objects must be identified and the folder structure defined.
  • For the Salesforce account and contact objects, SpringCM provides an out-of-the-box configuration that lets you access documents associated with these objects without doing any further customization. For all other Salesforce objects, we need to create a separate Visualforce page.
  • We can access the document of a record by adding a Visualforce page to the page of the object. Sometimes though, we may need to access the document from SpringCM by using Apex to convert it into a Salesforce document or to download the document from SpringCM. We can then process it like an xml document, parse it, and create records based on parsed data.


  • Install SpringCM app from AppExchange.
  • Configure SpringCM for objects.
  • Download WSDL from here.
  • Generate class from WSDL.

public with sharing class SpringCMFileManager {
static final String OBJECT_TYPE = 'Account';
static final String SFTYPE_PREFIX = 'Salesforce.';
static final String DOC_TYPE = 'xml';
static final Integer CHUNK_SIZE = 500000;

// A Method which fetch document from Spring CM
public static Document getDocument(String userName, String userKey, String accountId,String apiKey, String folderAccountId, String docName){
// Variables

Document doc;
string downloadStream;
string docData = '';
integer position = 0;
integer chunkSize = CHUNK_SIZE;
SpringCMSF.SCMDocument fetchedDoc;
SpringCMSF.SpringCMServiceSoap service = new SpringCMSF.SpringCMServiceSoap();

// get the token
string token = service.AuthenticateNonDefault(userName, userKey, accountId, apiKey);

// Get eos object to get information about folder
SpringCMEos.SpringCMUtilities.EosObject resultObj = SpringCMEos.SpringCMUtilities.createEOSObject(folderAccountId, OBJECT_TYPE);
string sfType = SFTYPE_PREFIX + resultObj.getSfType();
string sfId = resultObj.getSfId();
string path = resultObj.getPath();
string folderName = resultObj.getFoldername();
SpringCMSF.SCMFolder scmFolder;
SpringCMSF.SCMList results;

// Access folder
scmFolder = service.FindOrCreateEOSFolder(token, sfId , sfType, folderName, path, true);

// Create pager
SpringCMSF.SCMDocumentPager pager = new SpringCMSF.SCMDocumentPager();
pager.StartIndex = 0;
pager.PageSize = 100; // This is the large size of page
pager.SortAscending = true;
String folderId = scmFolder.Id;
Boolean loadExtendedMetadata = false;

// fetch documents

results = service.FolderGetContents(token, scmFolder.Id, loadExtendedMetadata, pager);

if(results != null && results.SCMDocuments != null && results.SCMDocuments.SCMDocument.size() > 0){
for(SpringCMSF.SCMDocument scmDoc : results.SCMDocuments.SCMDocument){
if(scmDoc.Name == docName){
fetchedDoc = scmDoc;

fetchedDoc = scmDoc;

//The position increases until we have all the bytes
//so loop while there is still more to download

while (position < fetchedDoc.FileSize){ if (position + chunkSize >= fetchedDoc.FileSize){
chunkSize = (integer)(fetchedDoc.FileSize - position);

downloadStream = service.DocumentDownload(token, scmDoc.Id, position, chunkSize, 'Native');
docData += EncodingUtil.base64Decode(downloadStream).toString();

//Move the position for next chunk
position += chunkSize;



if(fetchedDoc != null){
doc = new Document();
doc.Body = Blob.valueOf(docData);
doc.FolderId = UserInfo.getUserId();
doc.Name = docName;
doc.ContentType = fetchedDoc.MIMEType;
doc.Type = DOC_TYPE;

return doc;

Previous Article
How to Create a Custom Required Block for a Visualforce Page
How to Create a Custom Required Block for a Visualforce Page

Next Article
Quotes Management in Salesforce
Quotes Management in Salesforce