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
KatherineCKatherineC 

Chatter Feed Key Word Email Alert

Hi All,

We want to send out email alert when certain key words (like legal, tax) are mentioned in Chatter post. We notice Feed Item Trigger, can we use that to achieve this and how? Based on our research it seems Feed Item Trigger is for insert/delete post, can it trigger to send out email alert or create a new record? Ideally it can trigger to send out email alert, if not we're thinking to create a process like this, when someone mentions "legal" in the post, trigger will fire to create a record under a custom object we build and subsequently send out email alert by workflow rule. Any suggestions? Thanks.
SVS SolutionsSVS Solutions
Yes. Create a trigger on Feed Item and have the apex code send out an email if the word "legal" appears in the body of the chatter post.
KatherineCKatherineC
Hi SVS,
Do you have any sample codes that can help me start? I'm new to apex coding. Thanks.
Hargobind_SinghHargobind_Singh
Here are some links to get you started:

http://stackoverflow.com/questions/17047778/sending-email-with-template-using-trigger
http://stackoverflow.com/questions/17860289/apex-beginner-email-from-a-trigger
http://salesforce.stackexchange.com/questions/38947/sending-email-notification-using-trigger
https://developer.salesforce.com/forums?id=906F00000008yhSIAQ
http://www.sfdc99.com/2014/03/01/sending-emails-using-apex/
http://abhithetechknight.blogspot.sg/2013/10/send-email-using-trigger.html

Hope it helps.
KatherineCKatherineC
Hi Hargobind,

Thanks so much for the links, lots of helpful information. Based on that we created a trigger and test class: when a new record under Keyword object is created, we want an email notification sends out automatically to record owner and confirm we receive his keyword request. But we got error message for test class, we don't know how to reference folderId for email template and could not find information that makes it work. Please help. 

Error Message System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [FolderId]: [FolderId]

@isTest
public class SendKRNotificationTest
{
    static testMethod void SendEmail()
    {
        Test.StartTest();
         
EmailTemplate et = new EmailTemplate (developerName = 'test', TemplateType= 'text', Name = 'test');
insert et;
        Keyword__c k = new Keyword__c();
        insert k;
        Test.StopTest();
       
    }
}

TRIGGER:
trigger SendKRNotification on keyword__c (after insert) {

  keyword__c keyword = trigger.new[0];
  String[] toAddresses = new String[] {keyword.email__c};
  Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

  mail.setTargetObjectId(keyword.OwnerID);
  mail.setSenderDisplayName('Admin');
  mail.setUseSignature(false);
  mail.setBccSender(false);
  mail.setSaveAsActivity(false);

  {
          EmailTemplate et=[Select id from EmailTemplate where DeveloperName=:'Keyword_Request_Confirmation'];
          mail.setTemplateId(et.id);
          Messaging.SendEmailResult [] r =
    Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});  
      }
      }