4

When we convert the lead automatic chatter feed appears on Account. UserName converted a lead to this account.I have chatter feed enabled on Lead and Account.Is there an option to prevent this post other than disabling chatter feed on Account ?

Update :

I created the After Update trigger on Lead to delete account feed.I could get the convertedAccountID from conversion.How to access Account Feed.I see listAccountFeedsToDelete as empty in debug logs but I see feed created on Account after conversion.

trigger DeleteAccountFeed on Lead (after update) {


    Set<String> convertedLeads = new Set<String>();
    List<AccountFeed> listAccountFeedsToDelete = new List<AccountFeed>();
    Set<Id> convertedAccountSet = new Set<Id>();

    FOR(Lead l : trigger.new){
        IF(l.IsConverted == TRUE && trigger.oldmap.get(l.Id).IsConverted == FALSE){
            convertedLeads.add(l.Id);
            system.debug('convertedLead'+l.Id);
            convertedAccountSet.add(l.convertedAccountId);
            system.debug('convertedAccountId'+l.convertedAccountId);
        }
    }

    system.debug('Converted Leads size is ' + convertedLeads.size());

    system.debug('convertedAccountSet'+convertedAccountSet);

    IF(convertedLeads.size() > 0){

        FOR(AccountFeed objFeed : [SELECT Id,Type,ParentId,(SELECT Id,FieldName FROM FeedTrackedChanges) 
                                       FROM AccountFeed WHERE Type = 'TrackedChange' and ParentId in: convertedAccountSet]){
                                       system.debug('AccFeed....'+AccFeed);
                                       FOR(FeedTrackedChange objChange : objFeed.FeedTrackedChanges){
                                           system.debug('objChange...'+objChange);
                                           IF(objChange.FieldName == 'accountCreatedFromLead' || objChange.FieldName == 'accountUpdatedByLead'){
                                                System.debug('Account Feeds To Delete is ' + listAccountFeedsToDelete);
                                                listAccountFeedsToDelete.add(objFeed);
                                                System.debug('Account Feeds To Delete is ' + listAccountFeedsToDelete);
                                           }

                                       }

                                   }
        System.debug('Accound Feeds To Delete is ' + listAccountFeedsToDelete);
        System.debug('Accound Feeds To Delete size is ' + listAccountFeedsToDelete.size());

        DELETE listAccountFeedsToDelete;

         System.debug('Accound Feeds To Delete is ' + listAccountFeedsToDelete);
    }
 }

3 Answers 3

2

Move your code to @future method (or a batch job). Things like field history tracking are saved to database after the transaction actually commits.

I don't think it's explicitly written down anywhere in the docs. You'd have to combine knowledge from these 2:

Since test methods don't commit data, they don't result in the creation of FeedTrackedChange records. Similarly, field history tracking records (such as AccountHistory) can't be created in test methods because they require other sObject records to be committed first (for example, Account).

Yes, this means there might be slight delay between the lead conversion and actual delete. So you can't guarantee that users will never see these feed posts...

0
5

You can write a trigger to do this by deleting this post .Trigger will be on FeedItem object with ParentId as the AccountId and Body equal to one you recieved .

The psuedo code for the same will be as below

trigger FeedItemLeadConvDelete on FeedItem(after insert){

    List<FeedItem> lstfeedItem=new List<FeedItem>();
    String keyPrefix;
    for(FeedItem f:trigger.new){
         keyPrefix = String.valueof(f.parentId).substring(0,3);
         if((keyPrefix =='001') && (f.body.contains('updated this account by converting a lead')||(f.body.contains('converted a lead to this account')))){
          lstfeedItem.add(f);
         }
       }  
     delete lstfeedItem;

}
3
  • 1
    kumar .This is not working.When I tried to convert the lead to account.I don't see trigger running in the debug logs.So feed on account is not deleted.
    – sfdc
    May 21, 2014 at 20:35
  • @sf.dev thanks for info.I never tried the approach .It was just an Idea .I will try and see if i have alternative here May 22, 2014 at 8:07
  • Close but no cookie ;)
    – eyescream
    May 22, 2014 at 19:21
1
Trigger DeleteAccountFeed on Lead (after update) {


    Set<String> convertedLeads = new Set<String>();
    List<AccountFeed> listAccountFeedsToDelete = new List<AccountFeed>();
    List<Id> convertedAccountList = new List<Id>();

    FOR(Lead l : trigger.new){
        IF(l.IsConverted == TRUE && trigger.oldmap.get(l.Id).IsConverted == FALSE){
            convertedLeads.add(l.Id);
            system.debug('convertedLead'+l.Id);
            convertedAccountList.add(l.convertedAccountId);
            system.debug('convertedAccountId'+l.convertedAccountId);
        }
    }

    system.debug('Converted Leads size is ' + convertedLeads.size());

    system.debug('convertedAccountList'+convertedAccountList);

    IF(convertedLeads.size() > 0){
        DeleteAccountFeedFutureClass.DeleteFeed(convertedAccountList);
    }
}

global class DeleteAccountFeedFutureClass {

 @future
 public static void DeleteFeed(List<ID> recordIds)
  { 
       list<AccountFeed> listAccountFeedsToDelete = new list<AccountFeed>(); 
        FOR(AccountFeed objFeed : [SELECT Id,Type,ParentId,(SELECT Id,FieldName FROM FeedTrackedChanges) 
                                       FROM AccountFeed WHERE Type = 'TrackedChange' and ParentId in: recordIds]){
                                       system.debug('ObjFeed....'+ObjFeed); 
                                       FOR(FeedTrackedChange objChange : objFeed.FeedTrackedChanges){
                                           system.debug('objChange...'+objChange);
                                           IF(objChange.FieldName == 'accountCreatedFromLead' || objChange.FieldName == 'accountUpdatedByLead'){
                                                System.debug('Accound Feeds To Delete is ' + listAccountFeedsToDelete);
                                                listAccountFeedsToDelete.add(objFeed);
                                                System.debug('Accound Feeds To Delete is ' + listAccountFeedsToDelete);
                                           }

                                       }

                                   }
        System.debug('Accound Feeds To Delete is ' + listAccountFeedsToDelete);
        System.debug('Accound Feeds To Delete size is ' + listAccountFeedsToDelete.size());

        DELETE listAccountFeedsToDelete;

         System.debug('Accound Feeds To Delete is ' + listAccountFeedsToDelete); 
}
}
1
  • Sometimes there is a slight delay in deletion of account feed
    – sfdc
    May 23, 2014 at 13:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .