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
Susheel Reddy BSusheel Reddy B 

Need help to write apex trigger for sending auto acknowledgement emails in Email to Case

Hi,

 

In Email-to-Case if any person sends an email to Email routing address with other email id's in To, Cc or Bcc fields, then we need to send an acknowledgement email to all those email addresses in To, Cc or Bcc fields.

 

I wrote a trigger on Email object to get To, CC and Bcc fields, it's working fine for To,Cc fields but unable to get Bcc field.

When there is single email id in To or Cc field, i am able to send auto acknowledgement email but when there is multiple emails unbale to send emails using Outbond Email method.

 

Please suggest me the best possible way.

 

Please find the below code :

 

trigger testToCcBccAddress on EmailMessage (after insert) {
List<Case> cases = new List<Case>();
for (Integer i = 0; i < Trigger.new.size(); i++ )
{
try
{
Case c = new Case();
c=[SELECT id, Description,Subject, CaseNumber,RecordTypeId,To_Address__c,Cc_Address__c,Bcc_Address__c,Case_Thread_ID__c FROM Case WHERE Id = :Trigger.new[i].ParentId];
if(Trigger.new[i].ToAddress!= null && c.To_Address__c == null && Trigger.new[i].CcAddress == null)
{
c.To_Address__c= Trigger.new[i].ToAddress;
}
if(Trigger.new[i].CcAddress!= null && c.Cc_Address__c == null && Trigger.new[i].ToAddress == null)
{
c.Cc_Address__c= Trigger.new[i].CcAddress;
}
if(Trigger.new[i].ToAddress!= null && c.To_Address__c == null && Trigger.new[i].CcAddress!= null && c.Cc_Address__c == null)
{
c.To_Address__c= Trigger.new[i].ToAddress;
c.Cc_Address__c= Trigger.new[i].CcAddress;
}
cases.add(c);
update cases;
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();{
String emailtoAddr = [select To_Address__c from Case Case WHERE Id = :Trigger.new[i].ParentId].To_Address__c;
String emailCcAddr = [select Cc_Address__c from Case Case WHERE Id = :Trigger.new[i].ParentId].Cc_Address__c;
system.debug('******* TO Address ******'+emailtoAddr);
system.debug('******* CC Address ******'+emailCcAddr);
if (emailtoAddr != null)
{
emailtoAddr = emailtoAddr.replace(';\n', ', ');
String[] toAddresses = new String[] {emailtoAddr};
system.debug('****** To Address *******'+toAddresses);
mail.setToAddresses(toAddresses);
}
if (emailCcAddr != null)
{
emailCcAddr = emailCcAddr.replace(';\n', ', ');
String[] ccAddresses = new String[] {emailCcAddr};
system.debug('****** Cc Address *******'+ccAddresses);
mail.setCcAddresses(ccAddresses);
}
// String[] toAddresses = new String[] {'susheel.tecnics@yahoo.com','susheel_reddy@tecnics.com'};
// String[] ccAddresses = new String[] {'susheel.tecnics@yahoo.com','susheel_reddy@tecnics.com'};
if (c.RecordTypeId == '01290000000OPJp')
{
OrgWideEmailAddress owa = [select id, DisplayName, Address from OrgWideEmailAddress where DisplayName= 'Tecnics Consultng Inc.'];
mail.setOrgWideEmailAddressId(owa.id);
}
if (c.RecordTypeId == '01290000000OPJk')
{
OrgWideEmailAddress owa = [select id, DisplayName, Address from OrgWideEmailAddress where DisplayName= 'Tecnics Integration Technologies'];
mail.setOrgWideEmailAddressId(owa.id);
}
mail.setSubject(c.Subject+' - ' +c.CaseNumber );
mail.setPlainTextBody('Dear Customer \n Case created : '+c.CaseNumber +'\n \n Regards \n Tecnics Consulting Inc \n \n ' +c.Case_Thread_ID__c );
mail.setHtmlBody('Dear Customer<br/><br/> Case Createdd - '+c.CaseNumber+' <br/> <br/> Regards<br/>Tecnics Consulting Inc <br/> <br/> '+c.Case_Thread_ID__c);
system.debug('****** Email *******'+mail);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
}
catch(exception e)
{
system.debug('*******'+e);
}
}
}

 

 

 

Thanks in advance.

Susheel Reddy