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
hithuhithu 

Is there any way to attach email to manually created cases

HI,

I have creatd case manually or through process builder. Need to notify an user after case got created. when user responds back to that email that needs to get assign to case. I know my other 2 things will be solved if we use email to case but  in my org we are not using email to case. Please suggest me if we have an option.

Thanks,
Ajay K DubediAjay K Dubedi

Hi,
You can send the Email using Apex.
You can either send Single Email Message or Mass Email Message.
Here are the Sample codes for the Both.

SingleEmailMessage
Code:

Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
message.toAddresses = new String[] { 'abc@gmail.com', 'xyz@gmail.com' };
message.optOutPolicy = 'FILTER';
message.subject = 'Opt Out Test Message';
message.plainTextBody = 'This is the message body.';
Messaging.SingleEmailMessage[] messages =   new List<Messaging.SingleEmailMessage> {message};
Messaging.SendEmailResult[] results = Messaging.sendEmail(messages);

if (results[0].success) 
{
    System.debug('The email was sent successfully.');
} else 
{
    System.debug('The email failed to send: ' + results[0].errors[0].message);
}
MassEmailMessage
Code:
public void SendEmail()
{
 List<contact> lstcon=[Select id from contact limit 2];
 List<Id> lstids= new List<Id>();
 for(Contact c:lstcon)
 {
  lstids.add(c.id);
 }
 EmailTemplate et=[Select id from EmailTemplate where name = 'EmailTemplatename' limit 1];
 
 Messaging.MassEmailMessage mail = new Messaging.MassEmailMessage();
 mail.setTargetObjectIds(lstIds);
 mail.setSenderDisplayName('System Admin');
 mail.setTemplateId(et.id);
 Messaging.sendEmail(new Messaging.MassEmailMessage[] { mail });
}

Thanks
 
hithuhithu
Hi, Thank you for the information. But what if a person responds to that email which we have sent using apex will it attach to the case? Thanks,
Ajay K DubediAjay K Dubedi
Hi,
You can use inbound Email Service Feature for that, Here is the sample code for that as well.
Code:
/**
 * Email services are automated processes that use Apex classes
 * to process the contents, headers, and attachments of inbound
 * email.
 */
global class CreateRecordFrmEmail implements Messaging.InboundEmailHandler {

    global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email,
    Messaging.InboundEnvelope envelope) {

        Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();

        String emailbody = email.body;//you can get email body
        String emailsubject = email.subject;//you can get email Subject
        String subToCompare = 'New user registered';//your own subject

        //put you logic for your three parameters like Name, Position and subject.
        if(email.body.equalsIgnoreCase(subToCompare))
        {

           //insert your code logic.

        }

    result.success = true;
        return result;
    }
}

Here is the Link to understand the Email Services Feature in Salesforce:​
https://developer.salesforce.com/page/An_Introduction_To_Email_Services_on_Force.com