Migrating Google Drive URLs to Drive Connect Default Record Folders

If you have been storing Google Drive folder urls on your Salesforce records we can help you move them with a script.


The below script will need to be modified for the Object (red) and any object field or variable references as well as the custom URL field (blue) you would want to run this script on.

Map<String, Account> accountsById = new Map<String, Account>([SELECT Id, Name, Google_Drive_Folder__c  FROM Account WHERE Google_Drive_Folder__c LIKE ‘%.google.com%’]);
Set<String> recordIds = accountsById.keySet();
Map<String, Map<String, String>> filesByUrl = new Map<String, Map<String, String>>();
Map<String, ContentVersion> cvByUrl = new Map<String, ContentVersion>();
List<ContentDocumentLink> cdlsToInsert = new List<ContentDocumentLink>();
List<String> cvIds = new List<String>();
List<ContentDocumentLink> cdls = [
    SELECT Id, LinkedEntityId
    FROM ContentDocumentLink
    WHERE LinkedEntityId IN :recordIds
    AND ContentDocument.LatestPublishedVersion.driveconnect__Default_Record_Folder__c = true
for(ContentDocumentLink cdl : cdls) {
    String accountId = cdl.LinkedEntityId;
    if(accountsById.containsKey(accountId)) accountsById.remove(accountId);
List<Account> accounts = accountsById.values();
if(accounts.size() > 0) {
    Integer max = accounts.size() > 200 ? 200 : accounts.size();
    for(Integer i = 0;  i < max; i++) {
        Account account = accounts[i];
        filesByUrl.put(account.Google_Drive_Folder__c, new Map<String, String> {
            ‘url’ => account.Google_Drive_Folder__c,
            ‘mimeType’ => ‘application/vnd.google-apps.folder’,
            ‘IsDefaultRecordFolder’ => ‘true’,
            ‘name’ => account.Name,
            ‘recordId’ => account.Id
    for(Map<String, String> file : filesByUrl.values()) {
        ContentVersion cv = new ContentVersion(
            ContentUrl = file.get(‘url’),
            Title = file.get(‘name’),
            driveconnect__Default_Record_Folder__c = true
        cvByUrl.put(file.get(‘url’), cv);
    insert cvByUrl.values();
    for(ContentVersion contentVersion : cvByUrl.values()) {
    for(ContentDocument cd : [SELECT Id, LatestPublishedVersion.ContentUrl FROM ContentDocument WHERE LatestPublishedVersionId IN :cvIds WITH SECURITY_ENFORCED]) {
        Map<String, String> file = filesByUrl.get(cd.LatestPublishedVersion.ContentUrl);
        cdlsToInsert.add(new ContentDocumentLink(
            ContentDocumentId = cd.Id,
            LinkedEntityId = file.get(‘recordId’),
            ShareType = ‘V’
    insert cdlsToInsert;

Running the Script

Once you have made the necessary modifications and have the custom URL fields populated for the desired records you can run the code. This can be done by going to setup -> developer console -> debug -> Open Execute Anonymous Window; pasting the script into the Apex window and then hitting execute. Once this has been successfully executed you will see default record folders on the Drive Links component. There is a max limit of 200 records that can be affected per execution which means that you will need to run this script multiple times until the desired number of records are affected. Below is a demonstration of running the script: