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
shrey.tyagi88@tcs.comshrey.tyagi88@tcs.com 

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;}

          fi.Body=BodyOfFeed;
         
                    
         lstFeeds.add(fi);
        }
    }
   if(lstFeeds.size()>0){
         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.

http://salesforce.stackexchange.com/questions/31163/how-to-add-topics-names-containing-spaces-to-feeditem-using-hashtag-via-apex?rq=1

Regards
Shrey Tyagi
Best Answer chosen by shrey.tyagi88@tcs.com
shrey.tyagi88@tcs.comshrey.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+']';}