+ Start a Discussion

Getting the error "Error: Compile Error: Incompatible types since an instance of SObject is never an instance of emailMessage at line 9 column 33 "

Hello All,

I have written the below snippet code to auto-populate the template when the type is problem.

global class EmailPublisherForCaseType implements QuickAction.QuickActionDefaultsHandler {
    // Empty constructor
    global EmailPublisherForCaseType() {
    // The main interface method
    global void onInitDefaults(QuickAction.QuickActionDefaults[] defaults) {
    QuickAction.SendEmailQuickActionDefaults sendEmailDefaults = (QuickAction.SendEmailQuickActionDefaults)defaults.get(0);
    EmailMessage emailMessage = (EmailMessage)sendEmailDefaults.getTargetSObject(); 
    Case c = [SELECT CaseNumber, Type FROM Case WHERE Id=:sendEmailDefaults.getContextId()];    
    // If case type is “Problem,” insert the “First Response” email template
    if (c.CaseNumber != null && c.Type.equals('Problem')) { 
        sendEmailDefaults.setTemplateId('Insert Email Template ID Here');   // Set the template Id corresponding to First Response

AnudeepAnudeep (Salesforce Developers) 
Hi VSk98, 

Can you check if you have an apex class with EmailMessage as a name in your org? If yes, can you please try deleting it?

Let me know if it helps