You need to sign in to do that
Don't have an account?

Trigger on FeedItem
Hi,
I am trying to write a chatter trigger to repost to an account if it meets a certain criteria ?
For example: if i post something aboutn # canada in my personal feed or in a group feed, it should automatically reposted to the account feed which is having account name as canada.
I have trigger a trigger on this.
trigger chatterfeed on FeedItem (after insert)
{
set<string> fbody= new set<string>();
for(feedItem f:trigger.new)
{
fbody.add(f.body);
}
List<FeedItem> feedItems = new List<FeedItem>();
list<account> a=[select id,name,ownerId from account ];
for(FeedItem f:trigger.new)
{
for(account ac:a){
if(f.body.contains(ac.name))
{
string s= f.body;
FeedItem fitem = new FeedItem();
fitem.type = 'LinkPost';
fitem.ParentId = ac.id;
system.debug(fitem.parentId+'IIIIIIIIII');
fitem.linkurl='https://ap1.salesforce.com/_ui/core/userprofile/UserProfilePage?u=00590000000NI6s&fId='+f.id;
fitem.Title = 'view';
fitem.Body=s;
system.debug(fitem.body+'BBBBBBBBB');
feedItems.add(fitem);
system.debug(feedItems+'FFFFFFFFf');
}
}
}
if(feedItems.size() > 0) {
try{
Database.insert(feedItems,false);
system.debug(feedItems+'OOOO');}
catch(exception e){}
}
}
I am getting internal salesforce error when I am fetching feetitem body. If I fetch feed Item id it is working fine.
Can anyone pls help me out its very urgent.
Thanks in advance.
The problem here is that the trigger continues firing everytime the post is posted to the account causing an infinite loop.
Example: The initial post is: "My post with a tag #AccountName". The trigger finds #AccountName and re-posts the same post to that account. Now, the trigger fires again because of the new post, it finds #AccountName again and continues repeating this over and over.
To solve this problem you can create a class with a static variable and prevent the triger from runing more than once.
Class with static variable:
I also noticed that your trigger queries all the account in your org, which if the org has more than 50,000 account the query will fail. Also it will post to accounts where the name is in the post even if these wheren't tagged with #. I tweaked you trigger to look for all the tags # in the post body. If a tag # is found, it will get the string following the tag until the next delimeter, which could be any character or a space.
Example:
Post = "Post about #XYZ Company, and #123 Company LLC,"
My delimeter is set to be ",". You can set the delimeter to be whatever you want.
From this post the trigger will get
Then, it will query for the accounts with these names. If the query returns any accounts the trigger will copy the post there.
Hope this helps!
Izay Ramos-Irizarry
All Answers
The problem here is that the trigger continues firing everytime the post is posted to the account causing an infinite loop.
Example: The initial post is: "My post with a tag #AccountName". The trigger finds #AccountName and re-posts the same post to that account. Now, the trigger fires again because of the new post, it finds #AccountName again and continues repeating this over and over.
To solve this problem you can create a class with a static variable and prevent the triger from runing more than once.
Class with static variable:
I also noticed that your trigger queries all the account in your org, which if the org has more than 50,000 account the query will fail. Also it will post to accounts where the name is in the post even if these wheren't tagged with #. I tweaked you trigger to look for all the tags # in the post body. If a tag # is found, it will get the string following the tag until the next delimeter, which could be any character or a space.
Example:
Post = "Post about #XYZ Company, and #123 Company LLC,"
My delimeter is set to be ",". You can set the delimeter to be whatever you want.
From this post the trigger will get
Then, it will query for the accounts with these names. If the query returns any accounts the trigger will copy the post there.
Hope this helps!
Izay Ramos-Irizarry
Hi Izay,
Do you know if there's a way to extract the User Id from an @mentions within a trigger?
Awesome response Izay!
One question:
Is there a way to throw and catch an error for just one User and process the rest? I have an .addError but it stops the entire batch from going through. I was under the assumption that the .addError would only apply to the single record.
Again, thanks!