function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Sabrina Oliveira 3Sabrina 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:
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());
            }
         }
    }
}

 
CharuDuttCharuDutt
Hii Sabrina
Try Below 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) {
              set<string> ownerId = new set<string>();
             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);
             
               Id recordId = item.ParentId;
        
          string query = 'select name,OwnerId from '+recordId.getSobjectType() +' where id = :recordId limit 1'; 
         for(sObject obj : Database.query(query)){
             Pre_Check_Compliance__c  acc=(Pre_Check_Compliance__c )obj;
             ownerId.add(acc.OwnerId);
         }
             
             try {
                notification.send(ownerId); // How to get Record Owner ID?
            }
            catch (Exception e) {
                System.debug('Problem sending notification: ' + e.getMessage());
            }
         }
    }
}
Please Mark It As best Answer If It Helps
Thank You!
Sabrina Oliveira 3Sabrina Oliveira 3
Hi CharuDutt, thanks for your answer!
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 :(