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
Sajjad MussaniSajjad Mussani 

trigger on case object - after insert

Hi
Please help me with some code snippet for trigger that fires after a case is created using Email To Case functionality in SF.
Trigger should capture the case number, contact email, case subject and case body for the newly created case.
thanks
AnudeepAnudeep (Salesforce Developers) 
Hi Sajjad, 

This is just a sample. Please make changes as required
 
trigger EmailToCase on Case (after insert) {
List<Id> caseIds = new List<Id>();
List<Case> cases = new List<Case>();
  for(Case cs: Trigger.new) {
    caseIds.add(cs.Id);
    System.debug('Case Number is' + cs.CaseNumber); 
    System.debug('Subject is' + cs.Subject);
    for(CaseComment comm : cs.CaseComments)
    {
        System.debug(comm.CommentBody);
    }
  }

//You can access the required information through the SOQL query as well

  cases = [Select CaseNumber, contact.Email, Subject from Case];
}

If you find this information helpful, please mark this answer as best. It may help others in the community. Thank You!

Anudeep
Sajjad MussaniSajjad Mussani
thanks.. how do I add the filter for cases that were created only by Email to Case?  Basically, a customer sending us an email creates a case in our org.  I would like this trigger to filter only for those cases and not for case that are created manually or by other means.

also, how do I get the sender email address (contact.email) without using SOQL?
one more: once I have the Id, how do I write an update command to add a comment or close the case?
Sajjad MussaniSajjad Mussani
@Anudeep hoping you could provide a little more help.  see questions above. thanks