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
sfdc007sfdc007 

Chatter Post requirement help needed

Hi,

I have a following requirement for which i need help on it related to chatter post

1) I have enabled the chatter post for opportunity object
My requirment is

whenever i enter some chatter post in opportunity and post it it should capture account id and opportunity id automatically and those ids should be visible in the chatter post to whom i post to.


can it be done through configuration in salesforce ?

Let me know how to achieve this requirement

Thanks in Advance





 
Best Answer chosen by sfdc007
viruSviruS
Update Opportunity instead of Account

trigger FeedItemTrigger on FeedItem (before insert) {
    
    set<id> parentAccountIds = new set<id>();
    for(FeedItem fdItem : trigger.new){
        String idStr = fdItem.Parentid;
        if(idStr.startsWith('001')){
           parentAccountIds.add(idStr);
        }
    }
    
    Map<id,Account> accounts = new Map<id,Account>([Select id, name from Account where id in:parentAccountIds]);
    for(FeedItem fdItem : trigger.new){
        Account parentAccount = accounts.get(fdItem.Parentid);
        String chatterBody = fdItem.Body;
        
        fdItem.Body = chatterBody + '\n Account Id is :'+parentAccount.id + ':: Account Name ::'+parentAccount.Name;
    }

}

All Answers

viruSviruS
Write Trigger on FeedItem  and Feedcomment
Feed Item Have Parent id field from where you can determine the Opportunity and Information.

Just update the Body  with info you like on Before insert trigger

FeedItem post = new FeedItem(); post.ParentId = oId; //eg. Opportunity id, custom object id.. post.Body = 'Enter post text here'; insert post;
 
sfdc007sfdc007
can you please help me with the code pls
viruSviruS
Update Opportunity instead of Account

trigger FeedItemTrigger on FeedItem (before insert) {
    
    set<id> parentAccountIds = new set<id>();
    for(FeedItem fdItem : trigger.new){
        String idStr = fdItem.Parentid;
        if(idStr.startsWith('001')){
           parentAccountIds.add(idStr);
        }
    }
    
    Map<id,Account> accounts = new Map<id,Account>([Select id, name from Account where id in:parentAccountIds]);
    for(FeedItem fdItem : trigger.new){
        Account parentAccount = accounts.get(fdItem.Parentid);
        String chatterBody = fdItem.Body;
        
        fdItem.Body = chatterBody + '\n Account Id is :'+parentAccount.id + ':: Account Name ::'+parentAccount.Name;
    }

}
This was selected as the best answer