Last active
May 24, 2017 21:42
-
-
Save pchittum/35ec9392d1d13cb0c42a6e0866f3badc to your computer and use it in GitHub Desktop.
bulk notifications for enterprise messaging
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void publishNotifications(List<String> messages){ | |
List<Notification__e> notifications = new List<Notification__e>(); | |
for (String message: messages) { | |
notifications.add(new Notification__e(Message__c = message)); | |
} | |
List<Database.SaveResult> results = EventBus.publish(notifications); | |
//process results | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trigger TopicNotificationTrigger on TopicAssignment (after insert) { | |
//Collect Feed Ids for any FeedItems Triggering this Trigger | |
//(TopicAssignments also work for SObjects so we don't want those) | |
Set<Id> feedIds = new Set<Id>(); | |
for (TopicAssignment ta : Trigger.new){ | |
if (ta.EntityId.getSObjectType().getDescribe().getName().equals('FeedItem')){ | |
feedIds.add(ta.EntityId); | |
} | |
} | |
//Get a map of FeedItem records | |
Map<Id,FeedItem> feedItems = new Map<Id,FeedItem>([SELECT Body from FeedItem where Id in: feedIds]); | |
//Collect FeedItem bodies for our 'BearAlert' topic | |
List<String> messages = new List<String>(); | |
for (TopicAssignment ta : [SELECT Id, EntityId, Topic.Name FROM TopicAssignment where Id IN: Trigger.new AND Topic.Name = 'BearAlert']){ | |
messages.add(feedItems.get(ta.EntityId).body.stripHtmlTags().abbreviate(255)); | |
} | |
//if we have some messages, pass them to the Apex class | |
if (messages.size() > 0){ | |
NotificationController.publishNotifications(messages); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment