function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Jesper Klang.Jesper Klang. 

Need to make the Case Status change when there's a comment on a Post

I need the Status of a Case to change from "Pending..." to "Notes Recieved" when there's a reply on a Post. I've tried to make this happen with Process Builder but found out that it's only possible with a Apex Trigger. That's where I need some help, and hope that someone can help me with the code.

So the Status of the Case need to change to "Notes Recieved" when:
  • Case Status contains "Pending" (we have different statuses ex "Pending Internal", "Pending Customer" etc.)
  • Someone other than the Case Owner writes a comment on a Post in the Case.
Thanks is advance!
Best Answer chosen by Jesper Klang.
Akhil AnilAkhil Anil
Hi Jesper,

Here goes the code for the Trigger.
 
trigger UpdateStatus on FeedComment (after insert) {
   
   List<Case> caselist = new List<Case>();
   List<Case> casestoupd = new List<Case>();
   List<Id> caseids = new List<Id>();
   
   for(FeedComment f:Trigger.New) {
      
	  caseids.add(f.ParentId);
   
   }
   
   try {
      caselist = [Select Id, Status, OwnerId from Case where Id IN :caseids];
   }
   catch(Exception e) {
      caselist = null;
   }
   
   Id loggedinuserid = userinfo.getUserId();
   
   for(Case c:caselist) {
      
	  String s = c.Status;
	  
      if(s.contains('Pending') && (c.OwnerId != loggedinuserid)) {
	     c.Status = 'Notes Recieved';
		 casestoupd.add(c);
	  }
	  
   }
   
   List<Database.SaveResult> updateResults = Database.update(casestoupd,False);
   
}

That should do the trick !

All Answers

Akhil AnilAkhil Anil
Hi Jesper,

Here goes the code for the Trigger.
 
trigger UpdateStatus on FeedComment (after insert) {
   
   List<Case> caselist = new List<Case>();
   List<Case> casestoupd = new List<Case>();
   List<Id> caseids = new List<Id>();
   
   for(FeedComment f:Trigger.New) {
      
	  caseids.add(f.ParentId);
   
   }
   
   try {
      caselist = [Select Id, Status, OwnerId from Case where Id IN :caseids];
   }
   catch(Exception e) {
      caselist = null;
   }
   
   Id loggedinuserid = userinfo.getUserId();
   
   for(Case c:caselist) {
      
	  String s = c.Status;
	  
      if(s.contains('Pending') && (c.OwnerId != loggedinuserid)) {
	     c.Status = 'Notes Recieved';
		 casestoupd.add(c);
	  }
	  
   }
   
   List<Database.SaveResult> updateResults = Database.update(casestoupd,False);
   
}

That should do the trick !
This was selected as the best answer
Jesper Klang.Jesper Klang.
Thanks again Akhil! I've done some tests and it works just as espected. I'm really thanksfull for your help!
Akhil AnilAkhil Anil
Glad it worked :)
Jesper Klang.Jesper Klang.
I want to change now so that this runs when Status is not 'Closed', instead of containing 'Pending'. Is it just to change line 26 to this below?
if(s != 'Closed' && (c.OwnerId != loggedinuserid)) {
Juan_FachJuan_Fach
Is this can be used when the agent "Reply" a case instead of Post?

JF