You need to sign in to do that
Don't have an account?
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;
}
}
}
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;
}
}
}
All Answers
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;
}
Thanks for the fast reply. It works!! :)
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);
}
}
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: