Can I migrate 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
WITH SECURITY_ENFORCED
];
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() > 100 ? 100 : 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()) {
cvIds.add(contentVersion.Id);
}
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;
}
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 Salesforce Setup > Developer Console > Debug > Open Execute Anonymous Window, pasting the script into the Apex window, and clicking Execute. Once this has been successfully executed you will see default record folders on the Drive Links component.
There is a max limit of 100 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. Here is a video demonstration of how to run the script.