Last active
November 15, 2022 02:14
-
-
Save sfboss/77ecce7cc0d604d2b1548d0899c627b8 to your computer and use it in GitHub Desktop.
This is the code to have content doc ids emailed to you
This file contains hidden or 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
| // get the contentDocumentIds in the system and put them in a list to use for CDL query | |
| // after we have our CDL Query, use the list of included objects to loop through and decide if the ContentDocumentId makes the cut for our final list of contentDocumentIds | |
| // take the final list and make it 1 contentdocumentid per line and attach it to the running users record | |
| // file is content-doc-ids.txt and it should be placed in the folder where you are running the script where the logs/content-doc-ids.txt file is (if the script ran already, you should see the old file there) | |
| /** | |
| BELOW IS ALL THAT SHOULD NEED EDITED TO BE ABLE TO USE .... PUT API NAMES AND RUN IT AND HOPE TO GET AN EMAIL | |
| */ | |
| String[] objectsToInclude = new List<String>{'Account','Contact','User'}; | |
| String[] theIds = new List<String>(); | |
| String[] theFinalContentDocumentIds = new List<String>(); | |
| String[] logsTosave = new List<String>(); | |
| // get a base list of ContentDocumentIds from the system | |
| for (ContentDocument cd : [SELECT Id FROM ContentDocument]) theIDs.add(cd.Id); | |
| // get contentdocumentlinks and pass in the list of Ids of ContentdocumentIds that the system makes us filter on for ContentDocumentLink object queries | |
| ContentDocumentLink[] theCDLs = [SELECT id,contentdocumentid,linkedentityid,linkedentity.type from contentdocumentlink where ContentDocumentId in :theIds]; | |
| // this is where we will loop through CDLs, and add it to the "finalListofContentDocumentIds" if our object list includes the one in the current cdl record (LinkedEntity.Type) | |
| for (contentdocumentlink cdl : theCDLs){ | |
| logsTosave.add('Object : ' + cdl.LinkedEntity.Type); | |
| logsTosave.add('Object Id : ' + cdl.LinkedEntityId); | |
| logsTosave.add('ContentDocumentId : ' + cdl.ContentDocumentId); | |
| if (objectsToInclude.contains(cdl.LinkedEntity.Type)) { | |
| theFinalContentDocumentIds.add(cdl.ContentDocumentId); | |
| system.debug ('ContentDocumentId : ' + cdl.ContentDocumentId + ' added to Final List because it is on object : ' + cdl.LinkedEntity.Type); | |
| } | |
| } | |
| // print out the final list of contentdocumentids as debugs | |
| logsTosave.add ('Final List of ContentDocumentIds: ' + theFinalContentDocumentIds); | |
| logsTosave.add ('Final List of ContentDocumentIds: ' + String.join(theFinalContentDocumentIds,',')); | |
| logsTosave.add ('Final Count of ContentDocumentIds: ' + theFinalContentDocumentIds.size()); | |
| // overkill notification options, should attach content-doc-ids.txt to your User record, and also email you the list. | |
| // content-doc-ids.txt is in the "logs" folder and needs to have these ids pasted into it. | |
| String theCDIdsListTextFileBody = ''; | |
| String theCDIdsListAttachmentFileBody = ''; | |
| for(String cdId : theFinalContentDocumentIds) theCDIdsListTextFileBody += cdId + '<br/>'; | |
| for(String cdId : theFinalContentDocumentIds) theCDIdsListAttachmentFileBody += cdId + '\n'; | |
| theCDIdsListTextFileBody += '<br/><br/><br/><br/><br/><br/><br/>'; | |
| for(String logitem : logsTosave) theCDIdsListTextFileBody += logitem + '<br/>'; | |
| ContentVersion cv = new ContentVersion(); | |
| cv.Title = 'content_docs_ids.txt'; | |
| cv.PathOnClient = 'content_docs_ids.txt'; | |
| cv.VersionData = Blob.valueOf(theCDIdsListTextFileBody); | |
| cv.FirstPublishLocationId = UserInfo.getUserId(); | |
| insert cv; | |
| logsTosave.add('Added content_docs_ids.txt file to user record'); | |
| logsTosave.add('Triggered email to running user'); | |
| // email us the details we need plus some debug logs | |
| Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage(); | |
| message.setToAddresses(new list<String>{UserInfo.getUserEmail()}); | |
| message.setSubject('[Complete] -- List of ContentDocumentIds Inside'); | |
| message.setHTMLBody(theCDIdsListTextFileBody); | |
| List<Messaging.EmailFileAttachment> attachments = new List<Messaging.EmailFileAttachment>(); | |
| Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment(); | |
| efa.setFileName('content_docs_ids.txt'); | |
| efa.setBody(Blob.valueOf(theCDIdsListAttachmentFileBody)); | |
| efa.setContentType('text/html'); | |
| attachments.add(efa); | |
| message.setFileAttachments(attachments); | |
| Messaging.SingleEmailMessage[] messages = new List<Messaging.SingleEmailMessage> {message}; | |
| Messaging.SendEmailResult[] results = Messaging.sendEmail(messages); | |
| if (results[0].success) { | |
| System.debug('The email was sent successfully.'); | |
| } else{ | |
| System.debug('The email failed to send: ' + results[0].errors[0].message); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment