+ Start a Discussion
Ofir PartokOfir Partok 

EmailMessage error when trying to create an Apex Trigger

Hi,
I'm new to the SF Apex triggers, and I am following this manual (https://help.salesforce.com/articleView?id=entitlements_milestones_trigger.htm&type=5) to enable Auto-Complete Case Milestones.
I managed to add create the first two Apex classes and the first Apex case trigger (Sample Trigger 1), but when I try to insert the third code (Sample Trigger 2, embedded below), I get the following error:
Error: Compile Error: Incorrect SObject type: EmailMessage should be Case at line -1 column -1

I tried looking online for a solution, but could'nt find any.
Does anyone have an idea how to solve this issue?
Thanks!
 
trigger CompleteFirstResponseEmail on EmailMessage (after insert) {
    if (UserInfo.getUserType() == 'Standard'){
        DateTime completionDate = System.now();
        Map<Id, String> emIds = new Map<Id, String>();
        for (EmailMessage em : Trigger.new){
            if(em.Incoming == false)
                emIds.put(em.ParentId, em.ToAddress);
        }
        if (emIds.isEmpty() == false){
            Set <Id> emCaseIds = new Set<Id>();
            emCaseIds = emIds.keySet();
                List<Case> caseList = [Select c.Id, c.ContactId, c.Contact.Email,
                    c.OwnerId, c.Status,
                    c.EntitlementId,
                    c.SlaStartDate, c.SlaExitDate
                    From Case c where c.Id IN :emCaseIds];
            if (caseList.isEmpty()==false){
                    List<Id> updateCases = new List<Id>();
                    for (Case caseObj:caseList) {
                        if ((emIds.get(caseObj.Id)==caseObj.Contact.Email)&&
                            (caseObj.Status == 'In Progress')&&
                            (caseObj.EntitlementId != null)&&
                            (caseObj.SlaStartDate <= completionDate)&&
                            (caseObj.SlaStartDate != null)&&
                            (caseObj.SlaExitDate == null))
                                updateCases.add(caseObj.Id);
                    }
                    if(updateCases.isEmpty() == false)
                        milestoneUtils.completeMilestone(updateCases, 
                                'First Response', completionDate);
            }
        }
    }        
}

 
Best Answer chosen by Ofir Partok
bob_buzzardbob_buzzard
When I've seen this before it's been because of how I've created the trigger. Did you create your new trigger from the Case setup area? I.e. by going to the case sobject Triggers -> New, and then paste in the sample code?  What I think happens is that the new trigger is created against the case object and tied to it through metadata. If you then update the trigger to point at a different sobject (EmailMessage in this case) then you get this error.  

I would delete this trigger and then create it through the developer console. Then you can choose EmailMessage as your sobject type.

All Answers

bob_buzzardbob_buzzard
When I've seen this before it's been because of how I've created the trigger. Did you create your new trigger from the Case setup area? I.e. by going to the case sobject Triggers -> New, and then paste in the sample code?  What I think happens is that the new trigger is created against the case object and tied to it through metadata. If you then update the trigger to point at a different sobject (EmailMessage in this case) then you get this error.  

I would delete this trigger and then create it through the developer console. Then you can choose EmailMessage as your sobject type.
This was selected as the best answer
Ramakrishna Reddy GouniRamakrishna Reddy Gouni
emCaseIds is must be list type not in set type
Ofir PartokOfir Partok
Thanks guys!