back to blog
Automate Notifications for a File uploaded in a library
January 31 2022 • 20 min read

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

  1. Create a public group and add members 
  2. Create a Library and give access to the public group
  3. Create a Custom Notification and enable relevant settings
  4. Create an Apex trigger

CREATE A PUBLIC GROUP AND ADD MEMBERS

  1. In Setup, search for Public Groups and select it.

screenshot 29

  1. Click the New button.
  2. Enter name in the Label. The Group Name(API Name) will be automatically  populated.

screenshot 30

  1. Click on the dropdown beside the “Search” label and choose the type you want to add.
  2. Add the respective members to the group using the Add button.

screenshot 31

  1. After adding all the required users, click Save.

CREATE A LIBRARY IN FILES AND GIVE ACCESS TO THE PUBLIC GROUP

  1. Click App Launcher, search for Files and select it.

screenshot 18

  1. Select Libraries from the left pane and click on New Library.

screenshot 20

  1. Enter a Name, an optional description and click Save.

screenshot 21

  1. Click on the dropdown button of the library and select Manage Members.

screenshot 23

  1. Under the Add Members label click the dropdown button and select Public Groups

screenshot 25

  1. Search for the required public group and select it.

screenshot 26

  1. From the Access dropdown select the required access level for the group and click Add button.
  2. 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.

screenshot 27

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

  1. From Setup in the Quick Find, search for Notification and select Custom Notifications.

screenshot 39

  1. Click the New button.
  2. Enter Custom Notification Name. The API Name will be automatically populated.

screenshot 40

  1. Under Supported Channels you can choose Desktop and/or Mobile. 
  2. Click Save.
  3. To send notifications to Salesforce App for mobile, go to Notification Delivery Settings and scroll down till you see Custom Notification Types.

screenshot 42

  1. Now click the dropdown button for the newly created notification type and click on Edit.

screenshot 43

  1. 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.

screenshot 44

  1. Go to Salesforce Notifications and make sure that the options Enable in-app notifications and Enable push notifications are checked.

screenshot 41

CREATE AN APEX TRIGGER

  1. Create an Apex trigger on ContentDocumentLink in either VS Code(Preferred) or the Developer Console.
  2. 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.

screenshot 48

screenshot 49

Click on the notification and it will take you to the preview page of the uploaded file.

screenshot 57

screenshot 51

This is how the email looks like

screenshot 58

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

Your email address will not be published

© 2024 Digital Biz Tech