+ Start a Discussion

Add multi word topic to chatter feed from apex!!

Hi Everyone,
              I have written a trigger that posts a feed on Accdount record whenever a child gets inserted. This trigger works fine , but I am struggling to add multi word topics to it. Please finde the code given below:

trigger CreateChatterFeedOnAccount on Chatter_Feed__c (after insert) {

List<FeedItem> lstFeeds = new List<FeedItem>();
String BodyOfFeed;
 if(Trigger.isInsert && Trigger.isAfter){
        for(Chatter_Feed__c cf : trigger.new) {
         FeedItem fi = new FeedItem();
         fi.ParentId = cf.Account__c;
         fi.Title= cf.Title__c;
         BodyOfFeed =  cf.Description__c;
         if(cf.link__c!= null){
            fi.Type = 'LinkPost';
            fi.LinkUrl = cf.link__c; 
          if(cf.Topic_1__c!= null){BodyOfFeed=BodyOfFeed+' #'+cf.Topic_1__c;}
          if(cf.Topic_2__c!= null){BodyOfFeed=BodyOfFeed+' #'+cf.Topic_2__c;}
          if(cf.Topic_3__c!= null){BodyOfFeed=BodyOfFeed+' #'+cf.Topic_3__c;}
          if(cf.Topic_4__c!= null){BodyOfFeed=BodyOfFeed+' #'+cf.Topic_4__c;}
          if(cf.Topic_5__c!= null){BodyOfFeed=BodyOfFeed+' #'+cf.Topic_5__c;}

         insert lstFeeds;
Now in the bold lines given above, if I add a topic that is of asingle word , it gets appended properly for eg  if Topic_1__c= "Sunshine" then post adds #Sunshine . But if Topic_1__c= "My Topic" post adds #My Topic to it. So how do we add muti word topis via apex. I found another link , but was not able to figure out much from this. Please help.


Shrey Tyagi
Best Answer chosen by shrey.tyagi88@tcs.com
I got it .... Given below is the modified code, f anyone is interested

if(cf.Topic_1__c!= null){BodyOfFeed=BodyOfFeed+' #['+cf.Topic_1__c+']';}