0

I have a script where if someone puts a chatter on an existing SF POST object it updates a timestamp. For some reason this doesn't work about 10% of the time, and it's only for ONE of my employees. My code is below:

trigger LastTouched 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 Support_Team_Member__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_Updated_By_Support__c = System.now()));
    }
}

// now we run the update.
update updates;
}

When running the query used above, I get back her user object

SELECT Id, Name FROM User WHERE Support_Team_Member__c = true

Is there something I'm missing? I can't understand an edge case where this would work sometimes and not others...

enter image description here

enter image description here

enter image description here

1
  • 1
    Have you consider both FeedComment and FeedItem? They are both related. FeedComment is obviously a comment, but FeedItem is the initial post. Or do you only want the Last Update By Support field to only be updated with comments and not initial posts?
    – Tyler Zika
    Commented Apr 15, 2020 at 16:52

1 Answer 1

2

Instead of doing a soql to get the case ids, I would try

Boolean feedCommentIsRelatedToCase = fc.ParentId.getSobjectType() == Schema.Case.SObjectType;

if(userMap.containsKey(fc.InsertedById) && feedCommentIsRelatedToCase) {
   updates.add(new Case(id = fc.ParentId, Last_Updated_By_Support__c = System.now()));
}

That way you get rid of an unnecessary soql and you don't need to rely on what's returned from a soql to update the desired data.

2
  • Sorry about the long delay in getting back to this, but I implemented this and it seems as though this issue is still coming up; which means that the first query must be SOMEHOW failing, which doesn't seem possible because the trigger is on feedcomment and running the SOQL query returns the user in question.
    – A_Elric
    Commented Apr 28, 2020 at 18:45
  • @A_Elric why not try using CreatedById in the check?
    – Tyler Zika
    Commented Apr 28, 2020 at 18:49

You must log in to answer this question.

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