trigger feeditemnofication on FeedItem (after update,after insert) {
   for (feeditem F : Trigger.new)

                feeditem EE = [Select body,parentid from feeditem where Id = :F.Id];   
                opportunity op=[select id,ownerid from opportunity where id=:EE.parentid] ; 
                User U= [select id,Email,Name from user where id =:op.ownerid ];
                 Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                String[] toAddresses = new String[] {U.email};                   
                mail.setSubject('Record Feed Notification to record  owner' );                 
                mail.setPlainTextBody('Hello,' + '\n\n' +'Please consider the following feed'+' '+'\n\n'+'Subject :'+F.body );
                Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail }); 


In this parentid will of any thing like accound id ,opportunity id,quote id,userid--- i need to send email to the record owner when feed is posted .how i can write the code as global way (or) we need to write it seperately (if account id,if oppid,if quoteid).it woud be great it would have some sample code.

  • 2
    This isn't an answer to your question but you seriously need to bulkify your trigger. Having SQOL inside a for-loop is bad - you have 3 SQOL statements. Also, you should bulkify your sendEmail other governor limits will apply - in this case 10. Commented Apr 30, 2014 at 8:04

1 Answer 1


Are you sure that the built-in email notifications are not sufficient? And that your users want what could be a large number of emails?

I don't think there is any way to avoid having to run a separate query per SObject type. Below is an outline of some code that might help reduce the coding effort by using dynamic SOQL. It also bulkifies the work. I don't know if feed items for more than 100 different SObject types would ever come through, but if they did the SOQL governor limit would be exceeded.

public class Emailer {

    private class Collector {
        SObjectType sobType;
        String ownerIdTerm;
        Set<Id> parentIds = new Set<Id>();
        Collector(SObjectType sobType, String ownerIdTerm) {
            this.sobType = sobType;
            this.ownerIdTerm = ownerIdTerm;
        Map<Id, Id> query() {
            Map<Id, Id> parentToOwner = new Map<Id, Id>();
            if (parentIds.size() > 0) {
                String soql = ''
                        + ' select Id, ' + ownerIdTerm
                        + ' from ' + sobType
                        + ' where Id in :parentIds';
                for (SObject sob : Database.query(soql)) {
                    parentToOwner.put((Id) sob.get('Id'), (Id) sob.get(ownerIdTerm));
            return parentToOwner;

    private static final Map<SObjectType, Collector> COLLECTORS
            = new Map<SObjectType, Collector> {
            Account.SObjectType => new Collector(Account.SObjectType, 'OwnerId'),
            Opportunity.SObjectType => new Collector(Opportunity.SObjectType, 'OwnerId'),

    public static void send(FeedItem[] feedItems) {
        for (FeedItem f : feedItems) {
            Collector c = COLLECTORS.get(f.ParentId.getSObjectType());
            if (c != null) {
        Map<Id, Id> parentToOwner = new Map<Id, Id>();
        for (Collector c : COLLECTORS.values()) {
        Messaging.SingleEmailMessage[] messages = new Messaging.SingleEmailMessage[] {};
        for (FeedItem f : feedItems) {
            Id ownerId = parentToOwner.get(f.ParentId);
            Messaging.SingleEmailMessage message = ...
  • The map should be Map<SObjectType, Collector> (if that is allowed).
    – Keith C
    Commented Apr 30, 2014 at 9:26
  • A values() was missing in that line - I've added it to the code.
    – Keith C
    Commented Apr 30, 2014 at 9:49
  • Presumably you added code for that part? If you did then I'm afraid you'll have to debug further (and creating tests would be a good way to do that).
    – Keith C
    Commented Apr 30, 2014 at 10:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .