Based on the requirement we have created few custom objects ,visual force pages and buttons etc for the cases.
When customer sends an email ,keeping in cc and bcc.The email is recieved to the concern dept and the ticked is raised ,while responding to the customer by clicking the SendEmail button ,the standard functionality SEND EMAIL page is opened where the FROM ,To,SUBject line Addresses gets copied but the cc and Bcc is getting disappeared.
When the From or Subject line Addresses are wrong the trigger gets fired and throws an error .But unable to findout why the CC and Bcc Addresses are not getting copied.Below is the trigger ,please help me out or correct me the code written is right or wrong.Any Suggestion highly appreciated.
trigger InboundEmailMessage on EmailMessage (before insert) {
List<String> caseEmailFieldList = new List<String>{'Email_CC_Address1__c','Email_CC_Address2__c','Email_CC_Address3__c','Email_CC_Address4__c','Email_CC_Address5__c','Email_CC_Address6__c',
'Email_CC_Address7__c','Email_CC_Address8__c','Email_CC_Address9__c','Email_CC_Address10__c'};
Map<id,Case> casemap = new Map<Id,Case>();
List<Id> csid = new List<Id>();
for(EmailMessage message : trigger.new) {
csid.add(message.parentId);
}
for(Case cs : [select id,Email_to_Case_Address__c,Email_CC_Address__c,Email_BCC_Address__c,Contact.Email,caseNumber,Subject from case where id IN: csid]){
casemap.put(cs.id,cs);
}
List<Case> caseList = new List<Case>();
for(EmailMessage message : trigger.new){
String textBodyString = message.Textbody;
if(!message.Incoming && textBodyString != null){
Integer indexOf = textBodyString.indexOf('ref:_00D');
if(indexOf > 0){
String textBody = textBodyString.subString(0,indexOf - 1);
message.Textbody = textBody;
}
if(indexOf == 0){
String textBody = '';
message.Textbody = textBody;
}
}
System.debug('Message Status = '+ message);
System.debug('Subject = '+message.Subject);
if(!message.Incoming ){
if(casemap.containsKey(message.parentId)){
if(casemap.get(message.parentId).Email_to_Case_Address__c != null && casemap.get(message.parentId).Email_to_Case_Address__c.length() > 0 &&
message.FromAddress != casemap.get(message.parentId).Email_to_Case_Address__c){
message.addError('Please select the Correct FROM Address');
}
else{
Case ca=casemap.get(message.parentId);
String subjectCompl = message.subject;
if(subjectCompl == null){
message.addError('Please enter proper subject.');
return;
}
Integer indexOf = subjectCompl.lastIndexOf('[');
String subjectSubString ='';
if(indexOf > 0){
subjectSubString = subjectCompl.subString(0,indexOf -1);
}
String caseSubject = ca.caseNumber +' - '+ca.Subject;
if(ca!= null && ca.Subject!= null && ca.Subject.length() > 0 && !caseSubject.startsWith(subjectSubString.trim())){
message.addError('Please enter proper subject.');
}
else{
Case cas = casemap.get(message.parentId);
if(cas != null){
Set<String> ccEmailSet = new Set<String>();
Set<String> bccEmailSet = new Set<String>();
if(cas.Email_CC_Address__c != null){
String ccEmailOnCaseString = cas.Email_CC_Address__c.trim() ;
if(ccEmailOnCaseString != null && ccEmailOnCaseString.length() > 0){
String[] ccEmailCaseArray = ccEmailOnCaseString.split(';');
for(String ccEmail : ccEmailCaseArray ){
ccEmailSet.add(ccEmail.trim());
}
}
}
if(cas.Email_BCC_Address__c != null){
String bccEmailOnCaseString = cas.Email_BCC_Address__c.trim() ;
if(bccEmailOnCaseString != null && bccEmailOnCaseString.length() > 0){
String[] bccEmailCaseArray = bccEmailOnCaseString.split(';');
for(String bccEmail : bccEmailCaseArray ){
bccEmailSet.add(bccEmail.trim());
}
}
}
Boolean check =false;
if(message.CcAddress != null && message.CcAddress != ''){
String ccEmailString = message.CcAddress.trim();
if(ccEmailString != null && ccEmailString.length() > 0){
String[] ccEmailArray = ccEmailString.split(';');
for(String ccEmail : ccEmailArray){
ccEmailSet.add(ccEmail.trim());
}
}
String caseCCEmailString = '';
Integer index=0;
for(String ccStr: ccEmailSet){
caseCCEmailString +=ccStr+';';
try{
Object oldValue = cas.put(caseEmailFieldList[index],ccStr);
//System.debug('Old Value = '+oldValue );
}
catch(Exception e){
// System.debug('Exception = '+e);
}
index++;
}
if(caseCCEmailString != null && caseCCEmailString.length() > 0){
caseCCEmailString = caseCCEmailString.subString(0,caseCCEmailString.length()-1);
cas.Email_CC_Address__c = caseCCEmailString ;
check = true;
}
}
else{
cas.Email_CC_Address__c = '';
for(String caseEmail : caseEmailFieldList){
cas.put(caseEmail ,'');
}
check = true;
}
if(message.BccAddress != null && message.BccAddress != ''){
String bccEmailString = message.BccAddress.trim();
if(bccEmailString != null && bccEmailString.length() > 0){
String[] bccEmailArray = bccEmailString.split(';');
for(String bccEmail : bccEmailArray){
bccEmailSet.add(bccEmail.trim());
}
}
String caseBCCEmailString = '';
for(String bccStr: bccEmailSet){
caseBCCEmailString +=bccStr+';';
}
if(caseBCCEmailString != null && caseBCCEmailString .length() > 0){
caseBCCEmailString = caseBCCEmailString.subString(0,caseBCCEmailString.length()-1);
cas.Email_BCC_Address__c = caseBCCEmailString;
check = true;
}
}
else{
cas.Email_BCC_Address__c = '';
check = true;
}
if(check){
caseList.add(cas);
}
}
}
}
}
}
}
if(!caseList.isEmpty()){
update caseList;
}
}
Any help very much appreciated.
check
statements and the other code surrounding those to see what's happening to help you debug it. Also, be careful of where you declare variables likeSet<String> ccEmailSet = new Set<String>();
these and other similar variables, when declared inside If statements, may not exist when referenced in later code.