BUSINESS CHALLENGE
Salesforce users may not know what new changes or features are deployed with each release. We can address this issue by sending an email with a release document. It would be much better if this process is Automated.
In order to Automate, we can upload the release document to a file library which will fire a trigger. We will utilize this trigger to send email and/or Custom Notification to a group of users
STEPS TO IMPLEMENT
- Create a public group and add members
- Create a Library and give access to the public group
- Create a Custom Notification and enable relevant settings
- Create an Apex trigger
CREATE A PUBLIC GROUP AND ADD MEMBERS
- In Setup, search for Public Groups and select it.
- Click the New button.
- Enter name in the Label. The Group Name(API Name) will be automatically populated.
- Click on the dropdown beside the “Search” label and choose the type you want to add.
- Add the respective members to the group using the Add button.
- After adding all the required users, click Save.
CREATE A LIBRARY IN FILES AND GIVE ACCESS TO THE PUBLIC GROUP
- Click App Launcher, search for Files and select it.
- Select Libraries from the left pane and click on New Library.
- Enter a Name, an optional description and click Save.
- Click on the dropdown button of the library and select Manage Members.
- Under the Add Members label click the dropdown button and select Public Groups.
- Search for the required public group and select it.
- From the Access dropdown select the required access level for the group and click Add button.
- You can see the group added under Current Members. The members of the public group will now have access to all the files which will be added to this Library.
NOTE
We can upload files either individually or in a library. The files uploaded individually need to be shared with the respective users each time they are uploaded. The files uploaded to a library will be accessible by the users who have access to the library.
CREATE A CUSTOM NOTIFICATION AND ENABLE RELEVANT SETTINGS
- From Setup in the Quick Find, search for Notification and select Custom Notifications.
- Click the New button.
- Enter Custom Notification Name. The API Name will be automatically populated.
- Under Supported Channels you can choose Desktop and/or Mobile.
- Click Save.
- To send notifications to Salesforce App for mobile, go to Notification Delivery Settings and scroll down till you see Custom Notification Types.
- Now click the dropdown button for the newly created notification type and click on Edit.
- Under the Applications Label, if you do not see the options Salesforce for IOS and Salesforce for Android, download the Salesforce App on your mobile device and login. You will see the options once you login.
- Go to Salesforce Notifications and make sure that the options Enable in-app notifications and Enable push notifications are checked.
CREATE AN APEX TRIGGER
- Create an Apex trigger on ContentDocumentLink in either VS Code(Preferred) or the Developer Console.
- The code for the trigger is as follows
trigger ContentDocumentLinkTrigger on ContentDocumentLink (after insert) {
for(ContentDocumentLink documentLink : Trigger.New) {
ContentDocument documentFile = [Select ParentId, Id, Title from ContentDocument where Id = : documentLink.ContentDocumentId];
String libraryId = documentFile.ParentId;
Set<String> recipientIds = new Set<String>();
if (libraryId != null && libraryId != '') {
ContentWorkspace cws = [Select Id, Name from ContentWorkspace where Name = 'File Library'];
If (documentFile.ParentId == cws.Id) {
Messaging.CustomNotification notification = new Messaging.CustomNotification();
notification.setTitle('New Upgrade Notification');
notification.setBody('A new upgrade has been rolled to the Org');
CustomNotificationType notificationType = [SELECT Id, DeveloperName FROM CustomNotificationType WHERE DeveloperName='File_Notification'];
notification.setNotificationTypeId(notificationType.Id);
notification.setTargetId(documentLink.ContentDocumentId);
Group publicGroup = [Select Id from Group where Name ='DBT Group'];
List<GroupMember> groupMembers = [Select GroupId, UserOrGroupId from GroupMember where GroupId = : publicGroup.Id];
if (groupMembers != null && !groupMembers.isEmpty()) {
for (GroupMember member : groupMembers) {
recipientIds.add(member.UserOrGroupId);
}
}
notification.send(recipientIds);
list<Messaging.SingleEmailMessage> mailList =new List<Messaging.SingleEmailMessage>();
List<User> userList = [Select Id, Email from User where Id = : recipientIds];
String fileLink = URL.getSalesforceBaseUrl().toExternalForm()+'/lightning/r/ContentDocument/'+documentLink.ContentDocumentId+'/view';
for (User each : userList) {
Messaging.SingleEmailMessage userEmail = new Messaging.SingleEmailMessage();
List<String> sendToEmail = new List<String>();
sendToEmail.add(each.Email);
userEmail.setToAddresses(sendToEmail);
userEmail.setSenderDisplayName('System Administrator');
userEmail.setSubject('New Upgrade Changes');
userEmail.setHtmlBody('Here is the link to the file that contains the latest upgrades rolled out to the Org.'+' '+'<a href="'+fileLink+'">Click Here for the File</a>');
mailList.add(userEmail);
}
Messaging.sendEmail(mailList);
}
}
}
}
NOTE
In order to deploy this code into production an Unit Test class should be written with enough code coverage for the Apex Trigger.
Here is the test class for the Apex controller
@Istest
public class ContentDocumentLinkTriggerTest {
@Istest
public static void testCustomNotification(){
ContentVersion cv = new ContentVersion();
cv.ContentLocation = 'S';
cv.PathOnClient = 'TestFile.pdf';
cv.Title = 'Test File';
cv.VersionData = EncodingUtil.base64Decode('This is a test attachment');
insert cv;
Id cdId = [SELECT ContentDocumentId FROM ContentVersion WHERE Id = :cv.Id].ContentDocumentId;
ContentDocumentLink contentDocumentLink = new ContentDocumentLink();
contentDocumentLink.ContentDocumentId = cdId;
ContentWorkspace cws = [Select Id, Name from ContentWorkspace where Name = 'File Library'];
contentDocumentLink.LinkedEntityId = cws.Id;
insert contentDocumentLink;
System.debug(Limits.getEmailInvocations());
System.assertEquals(1, Limits.getEmailInvocations());
}
}
HOW IT WORKS
Whenever a file is uploaded to the folder we have created, a Notification and an email will be sent to the members of the public group.
Click on the notification and it will take you to the preview page of the uploaded file.
This is how the email looks like
WRAPPING IT UP
In this blog we have discussed how to send a custom notification and/or an email to users in salesforce whenever a file is uploaded to a file library.
Leave a Comment