Skip to content

Instantly share code, notes, and snippets.

@pchittum
Last active May 24, 2017 21:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pchittum/35ec9392d1d13cb0c42a6e0866f3badc to your computer and use it in GitHub Desktop.
Save pchittum/35ec9392d1d13cb0c42a6e0866f3badc to your computer and use it in GitHub Desktop.
bulk notifications for enterprise messaging
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
}
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