Migrating Salesforce Documents and Attachments to SugarCRM

We recently performed a Salesforce to SugarCRM migration that required many documents to be added to their new CRM.  The data export out of Salesforce keeps documents in two components, just like SugarCRM.  The document records in .csv format, and a folder that contains any attachments.  Importing the actual records into SugarCRM is well documented using either the import tool, or with web services, so we're going to skip that part of the process.  Below are the only fields we're going to need  to import the files into sugar.

Id 00PA0000000mYZuMAM
Name Test Doc.pdf
ContentType application/pdf

The operation will fail if we try to attach a file to a record that doesn't exist in SugarCRM, so what we're going to do is build an array that stores the list of Documents that meet our criteria. In this case, we are going to run the script as a user that is not the administrator. When the rest call is executed later on, the last modified by will change from '1' (for admin user), to something else. That way we can limit the number of files we attempt to do in one batch, and not try to attach documents to records we've already updated. Here, I've put a maximum of 2000 SugarCRM records.

We're going to need the ID to mach up with the imported Document record.  We also need this to find the appropriate file in our Attachments folder.  The 'Name' field is the file name that will show up in sugar. Finally ContentType is for when the attachment file has an extension.  I've seen before.  Normally, the salesforce output saves the attachment file without extension, but I've seen them with the attachments as well.

00PA0000000mYZuMAM or 00PA0000000mYZuMAM.pdf
If you do have a folder full of attachments with their extensions in place, here's the code we used to get the proper file extension in place, so we can retrieve the file programmatically:  

Now there's a collection of the records in the .csv from Salesforce, and a list of potential SugarCRM Document Record Targets. The third requirement to be met is a file in the Attachments folder that we can stick to our SugarCRM Document. We found that trying to do this on a windows development machine did not work. The document added in would take the ID as the file name, and would be poorly formed. We ran this script from a linux machine, and it worked well.

Once the script has run, you can output your arrays of results, or write them to log files. In the future we'll be importing the attachments at the same time as the document, simplifying the process.

Comments are closed.