Last active
February 1, 2023 20:03
-
-
Save douglascayers/fb011b94db9e97b52c4b60cd06d2172c to your computer and use it in GitHub Desktop.
Example code to convert GoogleDoc into ContentVersion (external file reference)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class GoogleDocToFilesBatchable implements Database.Batchable<SObject>, Database.Stateful { | |
private ID externalDataSourceId { get; set; } | |
public GoogleDocToFilesBatchable( ID externalDataSourceId ) { | |
this.externalDataSourceId = externalDataSourceId; | |
} | |
public Database.QueryLocator start( Database.BatchableContext context ) { | |
return Database.getQueryLocator([ | |
SELECT | |
id, name, url, ownerId, parentId, | |
createdById, createdDate, lastModifiedById, lastModifiedDate | |
FROM | |
GoogleDoc | |
ORDER BY | |
parentId | |
]); | |
} | |
public void execute( Database.BatchableContext context, List<GoogleDoc> googleDocs ) { | |
List<ContentVersion> filesToInsert = new List<ContentVersion>(); | |
for ( GoogleDoc gd : googleDocs ) { | |
String gdocId = gd.url.substringAfter( '/d/' ).substringBefore( '/' ); | |
filesToInsert.add( new ContentVersion( | |
contentLocation = 'E', | |
origin = 'H', | |
ownerId = gd.ownerId, | |
title = gd.name, | |
pathOnClient = gd.name, | |
externalDataSourceId = this.externalDataSourceId, | |
externalDocumentInfo1 = gd.url, | |
externalDocumentInfo2 = gdocId, | |
firstPublishLocationId = gd.parentId, | |
// Enable "Create Audit Fields" in Setup then you can | |
// preserve the original create/update values and owner if original owner is inactive | |
// https://help.salesforce.com/articleView?id=Enable-Create-Audit-Fields&language=en_US&type=1 | |
createdById = gd.createdById, | |
createdDate = gd.createdDate, | |
lastModifiedById = gd.lastModifiedById, | |
lastModifiedDate = gd.lastModifiedDate | |
)); | |
} | |
insert filesToInsert; | |
// optionally, can delete the GoogleDoc bookmarks since now redundant of Files | |
// delete googleDocs; | |
} | |
public void finish( Database.BatchableContext context ) { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This was very useful for me. I ended up writing a python script, migrate-google-docs.py . It is a lot more flexible about parsing urls to get google drive ids.