Custom Notification in Salesforce Apex, Apex Trigger

 trigger CaseTrigger on Case (after insert) {

    

    CustomNotificationType notificationType = 

        [SELECT Id, DeveloperName 

         FROM CustomNotificationType 

         WHERE DeveloperName='Notification'];

    

    List<Messaging.CustomNotification> notifications = new List<Messaging.CustomNotification>();

    

    for (Case c : Trigger.new) {

        

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

        

        notification.setTitle('New Case Created: ' + c.CaseNumber);

        notification.setBody('A new case has been created with the subject: ' + c.Subject);

        notification.setTargetId(c.Id); 

        notification.setNotificationTypeId(notificationType.Id); 

        Id userId = UserInfo.getUserId();

        notification.setSenderId(userId);

        

        try {

            notification.send(new Set<String> {userId});

        }

        catch (Exception e) {

            System.debug('Problem sending notification: ' + e.getMessage());

        }

    }

}


Comments

Popular posts from this blog

Digital E-Sign Signature In Salesforce

Custom List View Button In Salesforce