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 Post Key Word Trigger

Hi All,
We created a trigger on Chatter. If a post mentions “legal" keyword, a new record under ChatterPost object is created automatically. But it's not working, no new record is created, please help.

Trigger ChatterKeywordLegal on FeedItem (after insert) {
    List<FeedItem> FeedItems = new List<FeedItem>();
   for (FeedItem f : Trigger.new) {
           if (f.body == '!legal' ) {
               ChatterPost__c C = new ChatterPost__c();
               c.Description__c = f.body;
                     
              insert C;
        }
    }
   }
Best Answer chosen by KatherineC
Hargobind_SinghHargobind_Singh
Try replacing == with contains : 

Trigger ChatterKeywordLegal on FeedItem (after insert) {
    List<FeedItem> FeedItems = new List<FeedItem>();
   for (FeedItem f : Trigger.new) {
           if (f.body!=null &&   f.body.contains('legal' ) {
               ChatterPost__c C = new ChatterPost__c();
               c.Description__c = f.body;
                     
              insert C;
        }
    }
   }





All Answers

Hargobind_SinghHargobind_Singh
Try replacing == with contains : 

Trigger ChatterKeywordLegal on FeedItem (after insert) {
    List<FeedItem> FeedItems = new List<FeedItem>();
   for (FeedItem f : Trigger.new) {
           if (f.body!=null &&   f.body.contains('legal' ) {
               ChatterPost__c C = new ChatterPost__c();
               c.Description__c = f.body;
                     
              insert C;
        }
    }
   }





This was selected as the best answer
Pavan DavePavan Dave
Give a try to this:

Trigger ChatterKeywordLegal on FeedItem (after insert) {
   List<FeedItem> FeedItems = new List<FeedItem>();
   List<ChatterPost__c> cList = new List<ChatterPost__c>();   
   ChatterPost__c c;
   for (FeedItem f : Trigger.new) {
           if (f.body.contains.toUpperCase.('LEGAL')) {
               c = new ChatterPost__c();
               c.Description__c = f.body;
               cList.add(c);     
        }
  }
  if(cList.size()>0) 
     insert cList;
   }
KatherineCKatherineC
Hi Hargobind,
Thanks for the fast reply. It works!! :)
KatherineCKatherineC
Hi Hargobind,

Could you help me with the test class too? I got error, please help, thank you so much.

Compile Error: Illegal assignment from Schema.SObjectField to Id at line 8 column 9

@isTest
public class ChatterPostTest
{
    static testMethod void insertNewChatterPostTest()
    {
        Test.StartTest();
        FeedItem post = new FeedItem();
        post.ParentId = ChatterPost__c.Id;
        post.Body = 'legal test';
        insert post;
        Test.StopTest();
        System.assertEquals ('legal test', post.body);
    }
}
Hargobind_SinghHargobind_Singh
Hi Katherine, 

I did reply to your other test class post as well... :) 

For this one, you can't access ID on the standard object name, and you need to create an instance of your object. I am not sure how many mandatory fields do you have on Chatterpost__c object, so I've just specified name. 


Use this:

@isTest
public class ChatterPostTest
{
    static testMethod void insertNewChatterPostTest()
    {
        Test.StartTest();
		ChatterPost__c cObj = new ChatterPost__c(); 
        cObj.name = 'Test Name'; 
        //if you have other mandatory fields, add them here 
        insert cObj; 
        FeedItem post = new FeedItem();
        post.ParentId = cObj.Id;
        post.Body = 'legal test';
        insert post;
        Test.StopTest();
        System.assertEquals ('legal test', post.body);
    }
}





KatherineCKatherineC
Yes, I noticed that. :D  I did not explain well about the test class in this post so I created another post, problem sovled!!! Kudo to you.