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
VT-VT- 

Post feeditem to parent account

Hi,

I'm trying to write an Apex code which will create a chatter post on an account when someone post on an opportunity related to this account.
I'm not very experienced in Apex, this is where I am so far :
 
trigger Post_to_parent on FeedItem (after insert) {
  
    for (feeditem f : trigger.new){
        String ParentID = f.ParentId;

        if (ParentID.startsWith('006')){
            List<Opportunity> lstOppts = [select AccountID from Opportunity where id=:f.parentid];
            FeedItem item = new FeedItem(
            parentId = lstOppts[0].id,
            body = '<p>test clone</p>',
            isRichText = true);
            
            insert item;      
        }
    }
}
It fails because it loops recursively. I don't understand why since it create a post only when the chatter post is related to an opportunity and the code itself create a chatter post related to an account.

Could anyone give me a clue on what is going wrong ?

Thanks.

VT.

 
Best Answer chosen by VT-
Raj VakatiRaj Vakati
Try this code
trigger Post_to_parent on FeedItem (after insert) {
    
    for (feeditem f : trigger.new){
        String ParentID = f.ParentId;
        if (ParentID.startsWith('006')){
            Opportunity lstOppts = [select AccountID from Opportunity where id=:f.parentid];
            FeedItem item = new FeedItem(
                parentId = lstOppts.AccountID,
                body = '<p>test clone</p>',
                isRichText = true);
            
            insert item;      
        }
    }
}

 

All Answers

Raj VakatiRaj Vakati
Try this code
trigger Post_to_parent on FeedItem (after insert) {
    
    for (feeditem f : trigger.new){
        String ParentID = f.ParentId;
        if (ParentID.startsWith('006')){
            Opportunity lstOppts = [select AccountID from Opportunity where id=:f.parentid];
            FeedItem item = new FeedItem(
                parentId = lstOppts.AccountID,
                body = '<p>test clone</p>',
                isRichText = true);
            
            insert item;      
        }
    }
}

 
This was selected as the best answer
v varaprasadv varaprasad
Hi 

Try This : 

 
trigger Post_to_parent on FeedItem (after insert) {
    set<id> oppIds = new set<id>();
	
    for (feeditem f : trigger.new){
        String ParentID = f.ParentId;
        if (ParentID.startsWith('006')){
            oppIds.add(f.ParentId);  
        }
    }
	
	list<opportunity> opss = [select id,accountid from opportunity where id in : oppIds];
	
	set<id> acIds = new set<id>();
	for(opportunity opp : opss){
	     if(opp.accountid != null){
		   acIds.add(opp.accountid);
		 }
	
	   }
	   
	   list<FeedItem> lstfeed = new list<FeedItem>();
	   
	   if(acIds != null){
	    for(id aid : acIds){
	      FeedItem item = new FeedItem();
          item.parentId = aid;
          item.body = 'test clone';
          item.isRichText = true;
		  lstfeed.add(item);
	       }
	   }
	   
	insert lstfeed;
}

Hope this helps you!
If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.

Thanks
Varaprasad
@For SFDC Support: varaprasad4sfdc@gmail.com
Blog: http://salesforceprasad.blogspot.com/

Salesforce latest interview questions  :
https://www.youtube.com/channel/UCOcam_Hb4KjeBdYJlJWV_ZA?sub_confirmation=1


 
Raj VakatiRaj Vakati
try this
 
trigger Post_to_parent on FeedItem (after insert) {
    set<id> oppIds = new set<id>();
    for (feeditem f : trigger.new){
        if (f.ParentId.startsWith('006')){
            oppIds.add(f.ParentId);  
        }
    }
	
	list<opportunity> opss = [select id,accountid from opportunity where id in : oppIds];
	set<id> acIds = new set<id>();
	for(opportunity opp : opss){
	     if(opp.accountid != null){
		   acIds.add(opp.accountid);
		 }
	
	   }
	   
	   list<FeedItem> lstfeed = new list<FeedItem>();
	   
	   if(acIds.size()>0){
	    for(id aid : acIds){
	      FeedItem item = new FeedItem();
          item.parentId = aid;
          item.body = 'test clone';
          item.isRichText = true;
		  lstfeed.add(item);
	       }
	   }
	   
	insert lstfeed;
}

 
VT-VT-
Thank you all, it helped a lot :)