You need to sign in to do that
Don't have an account?
Sabrina Oliveira 3
Trigger to Record Owner when new FeedItem is created
I've been trying to notify a Record Owner when a new FeedItem is created without having its username marked in the post.
I did this easily through the Process Builder for the Lead object, but for some reason it doesn't work the same for custom objects and from what I googled it's a known issue.
Now I'm trying to do it coding, but I really don't know how to get Record Owner.
My Code:
I did this easily through the Process Builder for the Lead object, but for some reason it doesn't work the same for custom objects and from what I googled it's a known issue.
Now I'm trying to do it coding, but I really don't know how to get Record Owner.
My Code:
trigger FeedItemNotification on FeedItem (after insert) { List<Pre_Check_Compliance__c> PreCheckIds = new List<Pre_Check_Compliance__c>(); PreCheckIds = [SELECT Id, OwnerId FROM Pre_Check_Compliance__c]; Set<String> exstIds = new Set<String>(); For (Pre_Check_Compliance__c pc : PreCheckIds) { exstIds.add(pc.Id); } For(FeedItem item : Trigger.New) { Boolean result = exstIds.contains(String.valueOf(item.ParentId)); if(result = TRUE) { CustomNotificationType notificationType = [SELECT Id, DeveloperName FROM CustomNotificationType WHERE DeveloperName='Notificao_Padrao']; Messaging.CustomNotification notification = new Messaging.CustomNotification(); // Set the contents for the notification notification.setTitle('New Chatter post: '+item.Parent); notification.setBody('New chatter post has been created.'); // Set the notification type and target notification.setNotificationTypeId(notificationType.Id); notification.setTargetId(item.ParentId); try { notification.send(item.Parent:Pre_Check_Compliance__c.OwnerId); // How to get Record Owner ID? } catch (Exception e) { System.debug('Problem sending notification: ' + e.getMessage()); } } } }
Try Below Code Please Mark It As best Answer If It Helps
Thank You!
It works, but when I try to post at Chatter in another object (Opportunity for example), I receive the following error: FeedItemNotification: execution of AfterInsert caused by: System.TypeException: Invalid conversion from runtime type Opportunity to Pre_Check_Compliance__c Trigger.FeedItemNotification: line 33, column 1
It isn't necessary to get notifications in other objects, but the problem is that I can't even post with that error :(