0

I need to write trigger when user Post comment from chatter Post, Update the Accounts field Subject__c with chatter comments.For that I wrote a trigger but It is not working. I am new to salesforce. Please help in this trigger.

    trigger FeedCommentUpdate_Account on FeedComment(after Insert, after Update) {
    Map<Id, feedComment> mapId = new Map<Id, FeedComment>();
        for(FeedComment fc : trigger.new){ 
            mapId.Put(fc.ParentId, fc);
    } 
   List<Account> accList = [select  Id,last_Completed_Date_Time__c ,Subject__c, Link__c from Account where ID =: mapId.KeySet()]; 

        for(Account acc : acclist){
               if (mapId.containsKey(acc.Id)){
                    acc.last_Completed_Date_Time__c  = system.now();
                    acc.Subject__c=mapId.get(acc.Id).FeedItemId; 
               } 
        }
        Update accList;
}`

`

2
trigger FeedCommentUpdate_Account on FeedComment(after Insert, after Update) {
    String accKeyPrefix = Account.sObjectType.getDescribe().getKeyPrefix();
    Map<Id, feedComment> mapIdByFeedComment = new Map<Id, FeedComment>();
    for(FeedComment fc : trigger.new){ 
        if (parentId.startsWith(accKeyPrefix)){
            mapIdByFeedComment.Put(fc.ParentId, fc);
        }
    } 
    if(!mapIdByFeedComment.IsEmpty()){
        List<Account> accList = new List<Account>();
        for(Account acc : [select Id,last_Completed_Date_Time__c ,Subject__c, Link__c from Account where ID =: mapIdByFeedComment.KeySet()]){
            acc.last_Completed_Date_Time__c  = system.now();
            acc.Subject__c=mapIdByFeedComment.get(acc.Id).CommentBody; 
        }
        update accList;
    }
}

Debug Pointers :

  1. Apply System.Debugs to see if values are coming correctly in the mapIdByFeedComment/accList
  2. Check the Debug Log for any exceptions while updating Account.
  3. Ensure that size of Subject__c field is proper i.e. more than actual comment.

Not the answer you're looking for? Browse other questions tagged or ask your own question.