Tuesday, December 16, 2014

Parsing Emails From Salesforce and Salesforce For Outlook

When emails are sent or received in SFDC, either through SFDC itself or the Salesforce for Outlook plugin, the information is written to the Activities object.  This code helps you parse out the info from either type of email so you can, for example, save it to another table too.


  
 Trigger CopyActivityToCommunication on Task (after insert) {

   Set<ID> ids = Trigger.newMap.keySet();
   List<Task> taskList = [SELECT Id, WhoId, WhatId, CallType, Type, Description,
   Status,Subject,OwnerId,WhatCount,AccountId,CallObject,WhoCount, RecurrenceActivityId  FROM task WHERE id in :ids];
   List<Communication__c> comsToUpdate = new List<Communication__c>();
   RecordType RecType = [Select Id,SobjectType,Name From RecordType  Where SobjectType = 'Communication__c' and Name= 'Outreach'];
   
   for (Task taskItem : taskList ){
       if (taskItem.Type == 'Email'){
           String subjectBody = taskItem.Description;
           
           /* Emails sent from SFDC will either have 'Additional To:' or nothing.  Emails sent from Outlook always only have a 'To:'*/
           Boolean isFromSFDC = subjectBody.contains('Additional To:');
           List<String> AllEmails = new List<String>();
               if(isFromSFDC){
                   System.Debug('Inside isFromSFDC');
                   String[] bodyLines = subjectBody.split('\n');
                   
                   //Additional To:
                   String[] ToEmails = bodyLines[0].substring(14).split(';'); 
                   for(String email : ToEmails){
                       AllEmails.Add(email.trim());
                   }
                   //CC:
                   String[] ccEmails = bodyLines[1].substring(3).split(';'); 
                   for(String email : ccEmails ){
                       if(email.trim() != null || email.trim() != ''){
                           AllEmails.Add(email.trim());
                       }
                   }
                   //BCC:
                   String[] bccEmails = bodyLines[2].substring(4).split(';'); 
                   for(String email : bccEmails ){
                       if(email.trim() != null || email.trim() != ''){
                           AllEmails.Add(email.trim());
                       }
                   }          
               } else {
                   System.Debug('Inside From Outlook');   
                   String[] bodyLines = subjectBody.split('\n');
                   
                   //Additional To:
                   String[] ToEmails = bodyLines[0].substring(3).split(','); 
                   for(String email : ToEmails){
                       AllEmails.Add(email.trim());
                   }
                   
                   integer maxSize = bodyLines.size();
                   if(maxSize > 4) {maxSize = 5;}
                   System.Debug('maxSize  : ' + maxSize );
                   
                   if(maxSize > 2){
                       System.Debug('bodyLines[0] : ' + bodyLines[0]);
                       System.Debug('bodyLines[1] : ' + bodyLines[1]);
                       System.Debug('bodyLines[2] : ' + bodyLines[2]);
                   }
                   
                   for(integer i = 0; i < maxSize; i++){
                       if(bodyLines[i].contains('Cc:')){
                           System.Debug('Inside Cc:');
                           String[] ccEmails = bodyLines[i].substring(3).split(','); 
                           for(String email : ccEmails ){
                               if(email.trim() != null || email.trim() != ''){
                                   AllEmails.Add(email.trim());
                               }
                           }
                       }
                       if(bodyLines[i].contains('Bcc:')){
                           System.Debug('Inside Bcc:');
                           String[] bccEmails = bodyLines[i].substring(4).split(','); 
                           for(String email : bccEmails ){
                               if(email.trim() != null || email.trim() != ''){
                                   AllEmails.Add(email.trim());
                               }
                           }
                       }
                   } 
               }
               

           List<Contact> individualList = [Select Id, Name, Current_Coach__r.Id, Current_Financial_Services_Staff_Contact__r.Id   From Contact Where email = :AllEmails];
           for(Contact individual : individualList){
               
               Communication__c com = new Communication__c();
               
               /* Add stuff to communication (Custom) object*/
               
               comsToUpdate.add(com);
           }
       }
   }
   
  
  insert(comsToUpdate);
   

}

No comments:

Post a Comment