+ Start a Discussion
lakshman.mattilakshman.matti 

Chatter post on case record

Hi Everyone,

i have scenario . can you suggest me how to write trigger.
scenario is i have a standard object case. i have created a custom field which is of type checkbox.
i have created a record. on the record detail page we can post some comments using @mention xyz user.

when some one is posted comments using @mention username(owner of the record), i want to make that checkbox true.
what is relationship between case and casefeed.
can some one tel me how to write trigger for this.

Thanks
Lakshman
Best Answer chosen by lakshman.matti
Vinoth Vijaya BaskerVinoth Vijaya Basker
Hi Lakshman, 

Please find the below code which is updated to check if the owner of Case is equal to User inserts the Feed, 
 
trigger ChatterFeedItemTrigger on FeedItem (after insert) {
    Set<Id> Ids = new Set<Id>();
    Set<Id> IdsOfCasesToBeUpdated = new Set<Id>();
    List<Case> toUpdate = new List<Case>();
    
    for(FeedItem allCases : trigger.New){
       
            Ids.add(allCases.ParentId);
        
    }
    
    Map<Id, Case> mapOfAllCases = new Map<Id, Case>([SELECT Id, OwnerId FROM Case WHERE Id IN :Ids]);
    
    for(FeedItem i : trigger.New){
    Id idofRec = mapOfAllCases.get(i.ParentId).OwnerId;
    
        if(i.ParentId.getSobjectType() == Case.SobjectType && i.Type == 'TextPost' && i.Body.contains('@') 
                    && mapOfAllCases.get(i.ParentId).OwnerId != i.createdById){
            IdsOfCasesToBeUpdated.add(i.ParentId);
        }
    }
    
    List<Case> caseList = [SELECT Id, Check1__c from Case where id in :IdsOfCasesToBeUpdated];
    if(toUpdate != null){
        for(Case inc : caseList){
            inc.Check1__c = true;
            toUpdate.add(inc);
        }
        if(toUpdate.size()>0){
        Database.update(toUpdate,false);    
        }
    
    }    
}

Thanks,
Vinoth

All Answers

Vinoth Vijaya BaskerVinoth Vijaya Basker
Hello Lakshman,

Please find the below code which is for updating the check box, if Feed entered by user contains '@'. 
 
trigger ChatterFeedItemTrigger on FeedItem (after insert) {
    Set<Id> Ids = new Set<Id>();
    List<Case> toUpdate = new List<Case>();
    
    
    for(FeedItem i : trigger.New){
        if(i.ParentId.getSobjectType() == Case.SobjectType && i.Type == 'TextPost' && i.Body.contains('@')){
            Ids.add(i.ParentId);
        }
    }
    
    List<Case> caseList = [SELECT Id, Check1__c from Case where id in :Ids];
    if(toUpdate != null){
        for(Case inc : caseList){
            inc.Check1__c = true;
            toUpdate.add(inc);
        }
        if(toUpdate.size()>0){
        Database.update(toUpdate,false);    
        }
    
    }    
}

If you would like to write a trigger for FeedComment, you have to write a trigger for FeedComment.

Please find the below link for more information on FeedComment.

https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_feedcomment.htm 

Thanks,
Vinoth
lakshman.mattilakshman.matti
Hi Vinoth,

Thanks for the code. and i want to check one more condition here.
the criteria is i want to enable check box only if any user other than the owner of that record, posts comment on the record.
For example. i have created one record.so i'm the owner of that record.
say you are posting some comments on that record .so you are the another user.when you are posting u have mentioned my name on the comment using @mention notation.(Ex:@Mention Laskhman). 
how to check this condition.

Thanks
Lakshman
Vinoth Vijaya BaskerVinoth Vijaya Basker
Hi Lakshman, 

Please find the below code which is updated to check if the owner of Case is equal to User inserts the Feed, 
 
trigger ChatterFeedItemTrigger on FeedItem (after insert) {
    Set<Id> Ids = new Set<Id>();
    Set<Id> IdsOfCasesToBeUpdated = new Set<Id>();
    List<Case> toUpdate = new List<Case>();
    
    for(FeedItem allCases : trigger.New){
       
            Ids.add(allCases.ParentId);
        
    }
    
    Map<Id, Case> mapOfAllCases = new Map<Id, Case>([SELECT Id, OwnerId FROM Case WHERE Id IN :Ids]);
    
    for(FeedItem i : trigger.New){
    Id idofRec = mapOfAllCases.get(i.ParentId).OwnerId;
    
        if(i.ParentId.getSobjectType() == Case.SobjectType && i.Type == 'TextPost' && i.Body.contains('@') 
                    && mapOfAllCases.get(i.ParentId).OwnerId != i.createdById){
            IdsOfCasesToBeUpdated.add(i.ParentId);
        }
    }
    
    List<Case> caseList = [SELECT Id, Check1__c from Case where id in :IdsOfCasesToBeUpdated];
    if(toUpdate != null){
        for(Case inc : caseList){
            inc.Check1__c = true;
            toUpdate.add(inc);
        }
        if(toUpdate.size()>0){
        Database.update(toUpdate,false);    
        }
    
    }    
}

Thanks,
Vinoth
This was selected as the best answer
lakshman.mattilakshman.matti
Thanks Vinoth ..:)