Monday 15 December 2014

Populating (Overriding) From Email Addresses with Display Name



Recently, I was working on a client requirement to populate the From Email Addresses in Service cloud console based on the Case Origin. The From Email Addresses should be fetched from Email2Case Addresses.

Hope you know that there is no way in salesforce to query the Email2Case addresses.


  1.  I created an email to case settings in Custom Settings (E2C_Settings__c) and stored the Display Name, Case Origin and Email Address in the separate fields.
  2.  Salesforce apex:emailPublisher (Read here) incorporates all the components of Email. This can be only used in Service Console. Since, I need to display the Email Addresses based on Case Origin, I used apex:emailPublisher.
  3.  After, all the implementation, I encountered the issue of the From Email Address. The From Email Addresses populated like the following,
“person1@mycompany.com”< person1@mycompany.com>

“person2@mycompany.com”< person2@mycompany.com>


Then, I started thinking about overriding the From Address (select Component) dynamically by using JQuery with the help of Apex Remoting.

I opened the Developer Tools to see the From Addresses (select component) Id. This is from_CASEID(15 digits). I passed the cased id to a hidden field and accessed it in the jQuery dynamically to append the From list. The following Controller and Visualforce page code explains the logic.

The Controller code :

global class AnswerCustomerController {
    
    private final Case caseRec;
    
    public String caseID { get;set; }
    
    public String fromAddresses { get;set; }
    
    global static list<wEmailWrapper> wEmailWrapperList { 
    
     get{
     if(wEmailWrapperList==null)
     wEmailWrapperList = new list<wEmailWrapper>();
     return wEmailWrapperList;
     }
     set
    }

    public AnswerCustomerController(ApexPages.StandardController stdController) {
        
        this.caseRec = (Case)stdController.getRecord();
        
    }//Constructor ends
    
    @remoteaction
    global static list<wEmailWrapper> getCaseOriginList(String cId) {
    
     list<Case> casList = [select id, Origin from Case where id =: cId];
     
     String caseOrigin = '';
        
         //Copy Case Origin if the caseList is not empty
         if(!casList.isEmpty()){
         //system.debug('*** : '+casList);
         caseOrigin = casList.get(0).Origin;
         }
        
         //Accessing all the Email2Case Settings
       map<String,E2C_Settings__c> E2CSettings = E2C_Settings__c.getAll();
      
       if(caseOrigin != ''){
       //Add the email addresses with the same Case Origin
       for(E2C_Settings__c e2c:E2CSettings.values()){
        
         if(e2c.Case_Origin__c.equals(caseOrigin)){
         system.debug('*** : '+e2c);
         wEmailWrapperList.add(new wEmailWrapper(e2c.Email__c,e2c.Display_Name__c,e2c.Case_Origin__c));//Adding to Wrapper List
         }
       }//for ends
       }
      
       
       //If Case Origin is not found
       if(wEmailWrapperList.size()==0){
       //Add all the email addresses
       for(E2C_Settings__c e2c:E2CSettings.values()){
         wEmailWrapperList.add(new wEmailWrapper(e2c.Email__c,e2c.Display_Name__c,e2c.Case_Origin__c));//Adding to Wrapper List
       }//for ends
       }
    
        return wEmailWrapperList;
    }
    
    /* Wrapper Class to hold the Email Address into a JSON String */
    global class wEmailWrapper{
     public String wEmail { get;set; }
     public String wDisplayName { get;set; }
     public String wOrigin { get;set; }
    
     public wEmailWrapper(String email,String displayName,String origin){
     this.wEmail = email;
     this.wDisplayName = displayName;
     this.wOrigin = origin;
     }
    }

}

VF Code :
<apex:page standardController="Case" extensions="AnswerCustomerController">

<apex:includeScript value="{!URLFOR($Resource.JQuery, 'jquery-ui-1.11.2/external/jquery/jquery.js')}"  /> 


<script type="text/javascript">
var $j = jQuery.noConflict();
$j(document).ready(function(){
var caseid = $j("[id*='tempcaseid']").val();
var selList = $j("#from_"+caseid.substring(0,15)).val();
 
  //Remove the list – Otherwise it populates all the addresses from Orgganization default
  $j("#from_"+caseid.substring(0,15)).empty();
 
  //Javascript Remoting to get the dynamic List
  Visualforce.remoting.Manager.invokeAction(
         '{!$RemoteAction.AnswerCustomerController.getCaseOriginList}',
         caseid,
         function(result, event){
         if (event.status) {
         for (var i=0; i<result.length; i++){
         var res = result[i];
         //$j("#from_"+caseid.substring(0,15)).append(new Option(res.wDisplayName+"<"+res.wEmail+">",res.wEmail+":"+res.wEmail));
         $j("#from_"+caseid.substring(0,15)).append(new Option("\""+res.wDisplayName+"\""+"<"+res.wEmail+">",res.wEmail+":"+res.wEmail));
         }
         }
         },
         {escape: true}
         );
 
});
 
</script>

<apex:form id="theform">
<apex:inputHidden value="{!case.id}" id="tempcaseid"/>
</apex:form>
 <apex:emailPublisher entityId="{!case.id}"
  toAddresses="{!case.contact.email}"
  emailBodyFormat="textAndHTML"
  bccVisibility="editable"/>
</apex:page>

Now, the console page (populated From Addresses) look like below,


Hope this solves the problem for some of them to populate the Display Name with Email Address.