Send Custom Notifications Through APEX

Custom Notifications are used to send notifications to users when records change. It is available only in Salesforce Lightning Experience which supports Desktop and Mobile devices.

How to create Custom notification?

Go to Setup and Click on Custom Notification in Notification Builder.

Click the New button to create a new custom Notification.

Enter Custom Notification Name, API Name and Supported Channels as follows,

Click the Save button.

Custom Notification for Internal Users using APEX:

The below example shows how to send a custom notification when the related contact is delinked. Call this method from a trigger, flow, or wherever you want to send a custom notification from an Apex Class.

Code Snippet:

public class CARS_CustomNotification {

    //This method is used to send notification when the related contact is delinked.

            Public static void sendNotification(AccountContactRelation currAccCon){

        //Get the custom Notification type

                        List<CustomNotificationType > TemplateInfoDetail = [SELECT Id, DeveloperName FROM CustomNotificationType

                                                                                                                                                                                    WHERE DeveloperName=’Notify_Delink’];

                        if(currAccCon.CARS_IsActive__c == false && currAccCon.CARS_ContactUser__c != Null){

            // Create a new custom notification

                                    Messaging.CustomNotification currNotification = new Messaging.CustomNotification();

            // Set the contents for the delinking notification

                                    currNotification.setTitle(‘You have been DeLinked’ + currAccCon.CARS_Account_Name__c);

                                    currNotification.setBody(currAccCon.CARS_Contact_Name__c + ‘ has de-linked from ‘ + currAccCon.CARS_Account_Name__c +

                                                                    ‘ on ‘ + system.today());

                            // Set the notification type and target

                                    currNotification.setNotificationTypeId(TemplateInfoDetail[0].Id);

                                    currNotification.setTargetId(currAccCon.CARS_ContactUser__c);

            // Send the notification

                                    try {

                                                currNotification.send(currAccCon.CARS_ContactUser__c);

                                    }

                                    catch (Exception ex) {

                                                            System.debug(‘Notification Failed: ‘ + ex.getMessage());

                                    }

        }

   } 

}

Considerations:

  1. Custom Notifications are used to send notification for internal users.

Workaround

To notify the External users, an Email Alert can be used in the immediate action of the Process Builder in place of the Notification to notify the desired recipient.

  • Bulk Notifications are not supported using APEX, since it is designed to notify a bunch of people with single update, not for bulk updates.

Workaround

For Bulk Notification, use Process Builder or restructure your logic using Batch or Queueable class.

  • It is available only in Lightning Experience.

About MST

At MST Solutions our cornerstone is to adapt, engage and create solutions which guarantee the success of our clients. The talent of our team and experiences in varied business verticals gives us an advantage over other competitors.

Recent Articles

Harnessing Generative AI in Healthcare Insurance: Streamlining Operations and Enhancing Member Insights

Healthcare insurance providers are navigating new ground and capitalizing on new opportunities made possible through artificial intelligence. AI can be utilized to lower costs for payers and improve the member experience as well as the overall health and well-being of members, leading to greater member satisfaction and improving trust and reputation for the payer’s brand.

Read Article »

Work with us.

Our people aren’t just employees, they are key to the success of our business. We recognize the strengths of each individual and allow them time and resources to further develop those skills, crafting a culture of leaders who are passionate about where they are going within our organization.