Script Examples
It is possible to make impromptu changes to the Folderize database using APEX and SOQL scripts, if needed. Refer to the App Development page for information about objects used in these scripts.
As examples, we first show how to perform two mass deletion operations that may be useful under certain circumstances (as alternatives to one-by-one deletion via the Folderize user interface). Following that, we show how to link documents to object records.
How To Execute a Script:
- Click your name in the Salesforce header and select Developer Console.
- In the Developer Console use the menu: Debug – Open Execute Anonymous Window (or CTRL+E).
- In the Enter Apex Code popup window, delete any script from previous use. Insert your script. Check the Open Log checkbox. Click the Execute button.
- The log file should be opened automatically. Review the results (you may click the Debug Only checkbox at the bottom of the Log window to see only results of System.debug command).
- Recommended: Remove the delete …; line from any script below before executing it the first time. Review records in the log that were selected by the SOQL-query. If you see only the correct items which should be deleted, then restore the “delete” command and execute again.
Examples: Delete Document Mappings
Documents appear in folders by a mapping of the document to the folder. (More about mappings in App Development.) Removing documents from folders is done by removing the mappings. These example scripts remove mappings but do not delete documents themselves — they remain in the Salesforce repository for Content/Libraries/Files. (Also these scripts do not remove documents appearing in folders via tag association; instead, remove those by deleting tags in each folder’s settings popup.)
1. Delete all mappings for a folder (by folder ID, taken from the Edit Folder popup in the Folderize UI):
sharem__SmDocumentFolder__c[ ] mappings = [SELECT Id FROM sharem__SmDocumentFolder__c WHERE sharem__Folder__c = '@FOLDER_ID']; delete mappings;
2. Delete all mappings for a folder by folder Name (if you don’t have folders with the same name at other levels):
sharem__SmDocumentFolder__c[ ] mappings = [SELECT Id FROM sharem__SmDocumentFolder__c WHERE sharem__Folder__r.Name = '@FOLDER_NAME']; delete mappings;
3. Delete all mappings for a folder created by a user on June 20th, 2016 between 15:35 and 15:40 (3:35PM – 3:40PM), in time zone of user who executes the script:
sharem__SmDocumentFolder__c[ ] mappings = [SELECT Id FROM sharem__SmDocumentFolder__c WHERE sharem__Folder__c = '@FOLDER_ID' AND CreatedDate > :DateTime.newInstance(2016, 6, 20, 15, 35, 0) AND CreatedDate < :DateTime.newInstance(2016, 6, 20, 15, 40, 0) AND CreatedById = '@USER_ID']; delete mappings;
4. Delete all mappings created by a user on June 20th, 2016 between 15:35 and 15:40 (3:35PM – 3:40PM), in time zone of user who executes the script:
sharem__SmDocumentFolder__c[ ] mappings = [SELECT Id FROM sharem__SmDocumentFolder__c WHERE CreatedDate > :DateTime.newInstance(2016, 6, 20, 15, 35, 0) AND CreatedDate < :DateTime.newInstance(2016, 6, 20, 15, 40, 0) AND CreatedById = '@USER_ID']; delete mappings;
Examples: Undo a Replicator Upload
When using Folderize Replicator, you can undo your action immediately after an upload using the Cancel & Rollback button. But if you already started another upload process or closed the app, then rollback is no longer available. Instead,
you can execute APEX scripts to accomplish the same.
About placeholders used in these scripts:
@USER_ID is the ID of a user who performed the upload. This is needed to be sure that scripts will not delete documents uploaded by another user. Do not remove single quotes around this ID.
@ADDITIONAL_CONDITION is a condition that helps identify records of a particular upload. Here are possible examples of conditions. (These assume you do not set audit fields in the Folderize Replicator.)
- Records created today:
CreatedDate = TODAY
- Records created yesterday:
CreatedDate = YESTERDAY
- Records created on June 20th, 2016 between 15:35 and 16:17 (3:35PM – 4:17PM) in time zone of user who executes the script:
CreatedDate > :DateTime.newInstance(2016, 6, 20, 15, 35, 0) AND CreatedDate < :DateTime.newInstance(2016, 6, 20, 16, 17, 0)
Code to delete documents is as follows. Note that this script acts against the Salesforce standard object, ContentDocument. When you run this, triggers will automatically delete Folderize database entries for the deleted documents.
ContentDocument[ ] documents = [SELECT Id, Title FROM ContentDocument
WHERE CreatedById = '@USER_ID' AND @ADDITIONAL_CONDITION LIMIT 3000];
for (ContentDocument document : documents)
{
System.debug('- Document: ' + document.Title);
}
System.debug('***** number of documents ' + documents.size());
delete documents;
Code to delete folders is as follows.
sharem__SmFolder__c[ ] folders = [SELECT Id, Name FROM sharem__SmFolder__c
WHERE CreatedById = '@USER_ID' AND @ADDITIONAL_CONDITION LIMIT 3000];
for (sharem__SmFolder__c folder : folders)
{
System.debug('- Folder: ' + folder.Name);
}
System.debug('***** number of folders ' + folders.size());
delete folders;
Examples: Document Related to Account
The general approach for putting documents into Folderize programmatically is described on our App Development page. But if using Object-Record Mode, you may need, in addition, to do something like the following, which creates a mapping between a document an Account record. This is accomplished using the Salesforce standard object ContentDocumentLink.
ContentDocumentLink cdl = new ContentDocumentLink(); cdl.LinkedEntityId = '0010H00002S8MAk'; //this is just Id of one of my accounts cdl.ContentDocumentId = '0690H000004LlyBQAS'; //Id of a Content Document cdl.ShareType = 'V'; //The document will be shared in "Viewer" mode //cdl.ShareType = 'I'; //Use this line if you would want the document be shared in "Set By Record" mode. insert cdl;
If you have rules defining which records and documents should be mapped to each other, you can insert multiple ContentDocumentLink records at a time. For example, in the next script, all documents containing the word “Microsoft” in their name are to be shared with the Microsoft account:
ContentDocument[] documents = [SELECT Id FROM ContentDocument WHERE Title LIKE '%Microsoft%'];
Account msAccount = [SELECT Id FROM Account WHERE Name = 'Microsoft'];
List<ContentDocumentLink> cdls = new List<ContentDocumentLink>();
for (ContentDocument document : documents)
{
ContentDocumentLink cdl = new ContentDocumentLink();
cdl.LinkedEntityId = msAccount.Id; //this is just Id of one of my accounts
cdl.ContentDocumentId = document.Id; //Id of a Content Document
cdl.ShareType = 'V';
//cdl.ShareType = 'I';
cdls.add(cdl);
}
insert cdls;