1

I have written an apex trigger based largely off another answer on here which updates a timestamp field on a case, my question is -- how can I do the update on the field only if a certain user has performed the feedcomment update?

trigger FeedThis on FeedComment(after insert, after update){

    List<Case> updates = new List<case>();
    List<id> userList = new List<ID>();
    List<Id> feedItemList = new List<id>();
    for(FeedComment fc: trigger.new){
        feedItemList.add(fc.FeedItemId);
        userList.add(fc.InsertedById);
    }
    Map<Id, FeedItem> feedMap = new Map<id, FeedItem>([select id,InsertedById,Visibility from feedItem where Id IN :feedItemList]);
    Map<Id, User> userMap = new Map<Id, User>([select id, usertype, name from user where ID IN :userList]);
    for(FeedComment fc: trigger.new){
        if (feedMap != null && feedMap.containsKey(fc.feedItemId) && fc.ParentId.getSObjectType() == Case.SObjectType) {
            updates.add(new Case(
                    Id = fc.ParentId,

                    //IF User == A_Elric || User == Ethan_H{
                    Last_Chatter_Feed_Timestamp__c = System.now()
                    //}
                    ));
        }

    }
    if(updates != null && updates.size() > 0)
    update updates;
}

I threw in some comments to show what I'm trying to do vs what I have implemented.

Thanks,

1 Answer 1

1

A_Elric, From a design standpoint, I would probably add a checkbox custom field to the User object, which is used to indicate whether that user's feed comments should automatically update the Cases' Last_Chatter_Feed_Timestamp__c. I also would like to point out that FeedComment objects are the reply comments added to existing FeedItem objects. So if a user adds a new Feed Post to a case, this trigger will not fire. However, if anyone else adds a comment TO his Feed Post, at THAT point the trigger will fire.

Anyway, let's suppose that we add a checkbox to the User object called "Update Feed Timestamps" (Update_Feed_Timestamps__c, for our purposes). Then I would use the following trigger code:

trigger FeedThis on FeedComment(after insert, after update){

    List<Case> updates = new List<case>();
    Set<Id>feedItemIds = new Set<Id>();
    List<Id> feedItemList = new List<id>();

    // We'll narrow down our user list here, and use a Map, so their Ids are easy to get to.
    Map<id,user> userMap = new Map<Id,User>([SELECT Id FROM User WHERE Update_Feed_Timestamps__c = true]); 


    // first loop through to get the FeedItemIds
    for(FeedComment fc: trigger.new){
        if(userMap.containsKey(fc.InsertedById)){ // only add them to the list if they are the right user
            feedItemIds.add(fc.FeedItemId);
        }
    }

    // Next, query the Cases using a nested query.  This lets us hop across two layers of parent/child relationship in one query.
    if(!feedItemIds.isEmpty()){
        for(Case c : [SELECT Id FROM Case WHERE Id IN (SELECT ParentId FROM FeedItem WHERE Id IN :feedItemIds)]){
            // Add the cases to the update list
            updates.add(new Case(id = c.Id,Last_Chatter_Feed_Timestamp__c = System.now()));
        }
    }

    // now we run the update.
    if(!updates.isEmpty()) update updates;
}

Let me know if that makes sense.

3
  • 2
    nice answer; note that if(!updates.isEmpty()) update updates; should be update updates; as SFDC won't burn a DML on an empty list. Collections never need to be tested for empty before a DML statement.
    – cropredy
    Commented Sep 25, 2019 at 3:39
  • @cropredy oh snap. Good to know. Thanks for that!
    – Aubry
    Commented Sep 25, 2019 at 4:48
  • @Aubry This looks like it will perfectly fit my usecase, but I'm not exactly sure of how to write the test-case for this (as writing unit tests for Apex triggers is a bit different than Junit) can you give me some advice on that? Either way, marking this as correct
    – A_Elric
    Commented Oct 22, 2019 at 18:44

You must log in to answer this question.

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