Thursday, January 30, 2020

Fetch All Workflow Email Alert Details Related to Object Using Tooling API

Through this blog, I will sharing script which can help to get complete information about all the email alerts configured in Salesforce using workflow rules for any sObject.

Imagine a scenario in which you need to extract all email alert configured or created for different workflow rules for specific object so that it can be reviewed or analyzed. If you do this activity manually then you have to open each workflow email alert and note down details like recipient, ccEmails, senderAddress, email template etc. This will be very hectic if you have lots of email configured.

In order to solve this scenario, I am using Tooling API through which we can get complete details about workflow email alert. Through apex script, we can generate these details and send email (with csv file as attachment) to user with all details.

First of all, inorder to parser JSON response, we need to create apex class (SK_EmailAlertJSONParser) which will act as parser for JSON. After this we can run the script in developer console and user will get email with all information regarding email alert.


Below is snapshot of csv file that we will receive after running above script in developer console.
I have specified objectId as "Contact". For custom object, specify 15 digit or 18 digit id.


This script can be used to fetch information from other salesforce org also. Suppose you don't want to deploy this script in production, then save SK_EmailAlertJSOnParser class in sandbox and in developer console (using execute anonymous window)  just specify the domainUrl for production and sessiond id production for admin user.

Don't forget to add domainUrl in remote site settings before running this script in execute anonymous window in developer console.

Hope this will help!!1

Thursday, January 9, 2020

How to Delete all Files from Library using Apex

If you have to delete any library, then first you have to delete all the files present in that library. For this purpose you can run below mentioned simple apex script to delete all files from library.

string LibraryName='Demo Library';
ContentWorkspace ws = [SELECT Id, RootContentFolderId FROM ContentWorkspace WHERE Name = :LibraryName LIMIT 1];
List<ContentDocument> contentDocumentIds = new List<ContentDocument>();
for(ContentDocumentLink con:[select id,LinkedEntityId,ContentDocumentId   from ContentDocumentLink  where LinkedEntityId=:ws.Id]){
    contentDocumentIds.add(new ContentDocument(id=con.ContentDocumentId));
}
system.debug('********contentDocumentIds:'+contentDocumentIds);
system.debug('********contentDocumentIds.size:'+contentDocumentIds.size());
database.delete(contentDocumentIds,false);


Hope this will help!!

Wednesday, January 8, 2020

Download Multiple Files from Libraries

Consider a scenario in which we are suppose to download all files from libraries. Below are different approach which can be utilized:

Approach 1: Salesforce UI Download Button

If the file size is around 60 or less in library then you can use Salesforce UI to download all files at one time.

Open any library, click on show all button, then click on display options and select "Show 60 results per page". By doing this, you will be able to see 60 files at once in library. Now you can click on select all checkbox and click download button. This will help you to download files at once.
If you have more than 60 files, then click on next page and then again download all files. This process will become hectic if you have more than 1000 files in library.

Approch 2:URL hack

I performed some search on google and found out that there is another way to download all files as zip by specifying the contentversion Ids of file in URL. I can call it as URL hack and everyone mentioned that it is not recommended way. Below is URL format that you need to use:

https://xxxxxx.salesforce.com/sfc/servlet.shepherd/version/download/068xxxxxxxxxxxxxxx/068yyyyyyyyyyyyyyy/068wwwwwwwwwwwwwww/068zzzzzzzzzzzzz?

Just paste this URL and files will be downloaded as zip file.

If you have more than 1000 files in library, then you have to build different URL by specifying 10 or 15 record id in one URL. I was able to download zip file with size around 400Mb using one URL.

Approach 3: Using Apex Script & Tab Save Chrome Extension

One of my friend told me about Tab Save  chrome extension. You just need to paste all files download url and click on download.



This will download all files in your downloads folder or folder that you have specified in chrome download settings.

Now we wrote simple apex script which will send email with all URLs(file download URL) in csv file. Suppose you want to get download URLs for all files present in library "Demo Download Library", then refer below script:

Run above script in developer console by specifying your library name. You will receive email with all file download URLs. Now you just copy and paste these URLs in Tab Save edit window and click on download.

I have used Tab Save extension to download around 1500 files and it was working fine.

Now you have all 3 approaches and you can decide what to chose based on your requirements.

Hope this will help!!