Friday 3 June 2016

stop sending email to setTargetobjectId in single email message even using Email template

child__c (child) is custom object Contact (parent) lookup relationship.
i need to send a mail to email field which is present in child object with email template.
when ever we are using Email template setTargetObjectId is mandatory .
if our requirement is like we dont want to send mail to TargetObjectId , only need to send to email field which is present on child object.

to achieve this we need to use setTreatTargetObjectAsRecipient(false) Boolean method so it will stop sending email to TargetObjectId .


setTargetObjectId(targetObjectId)

Required if using a template, optional otherwise. The ID of the contact, lead, or user to which the email will be sent. The ID you specify sets the context and ensures that merge fields in the template contain the correct data.

setTreatTargetObjectAsRecipient(treatAsRecipient)

Optional. If set to true, the targetObjectId (a contact, lead, or user) is the recipient of the email. If set to false, thetargetObjectId is supplied as the WhoId field for template rendering but isn’t a recipient of the email. The default is true.

Signature

public void setTreatTargetObjectAsRecipient(Boolean treatAsRecipient)

Visual force page:

<apex:page controller="WrapperClsOnContactMass">
    <apex:form >
    <apex:pageMessages >
    </apex:pageMessages>
        <apex:pageBlock >
            <apex:pageBlockTable value="{!wrapperObj}" var="x">
                <apex:column value="{!x.conobj.name}"/>
                <apex:column value="{!x.conobj.email__c}"/>
                <apex:column >
                  <apex:inputcheckbox value="{!x.checkBox }"/>
                </apex:column>
            </apex:pageBlockTable>
            <apex:pageBlockSection >      
                               
            <apex:commandButton value="SendEmail" action="{!sendEmail}"/>

            </apex:pageBlockSection>
            </apex:pageBlock>
             <apex:pageBlock >
           
           <apex:pageBlockSection columns="1">
    <apex:pageBlockSectionItem >
        <apex:outputLabel value="Select template" for="template"/>
      <apex:selectList value="{!selectedTemplateId}" id="template" size="1">
                     <apex:actionSupport event="onchange" reRender="table" />
             <apex:selectOptions value="{!myPersonalTemplateOptions}"/>
         </apex:selectList>
            </apex:pageBlockSectionItem>
    <apex:pageBlockSectionItem >
      <apex:outputLabel value="File" for="file_File"/>
     <!-- <apex:inputFile id="file_File" value="{!fileBody}" filename="{!fileName}"/> -->
  <!--   <apex:inputFile value="{!fileBody}" fileName="{!filename }" contentType="{!conType}"/> -->

    </apex:pageBlockSectionItem>

   <!--<apex:commandButton value="sendEmail" action="{!sendEmail}" id="table"/> -->

               </apex:pageBlockSection>
         
           </apex:pageBlock>
    </apex:form>
</apex:page>

Apex Class :

public with sharing class WrapperClsOnContactMass {

    public List<WrapperClassEx> WrapperList{get;set;}
    public String selectedTemplateId { get; set; }
    public WrapperClsOnContactMass(ApexPages.StandardController controller) {
        
    }
    public WrapperClsOnContactMass() {

    }
    public List<SelectOption> getMyPersonalTemplateOptions() {
        List<SelectOption> options = new List<SelectOption>();
        for (EmailTemplate t : [
            select Id,Name 
            from EmailTemplate
            // Each User has a 'My Personal Templates' folder
            // of EmailTemplates, whose Id is the User's Id
            //where FolderId = :UserInfo.getUserId()
        ]) {
            options.add(new SelectOption(t.Id,t.Name));
        }
        return options;
    }
    public List<WrapperClassEx> getwrapperObj(){
        List<Child__c> conList =[select id, name, email__c,physition__r.email  from Child__c limit 5];   
        WrapperList = new List<WrapperClassEx>();
        for(Child__c con: conList){
            
            
            WrapperList.add(New WrapperClassEx(con,false));  
        }
        return WrapperList;  
    }
    
    public class WrapperClassEx{
        
       public WrapperClassEx(){
            
        }
       public Child__c conObj{get;set;}
       public Boolean checkBox{get;set;}
       public WrapperClassEx(Child__c conRec, boolean SelectedBox){               
          conObj= conRec;
          checkBox = SelectedBox;
       }
    }
        
       public void sendEmail(){
         List<Messaging.SingleEmailMessage> lstEmailId=new List<Messaging.SingleEmailMessage>();
         EmailTemplate et = [Select Id from EmailTemplate where id =:selectedTemplateId ];
          
         for(WrapperClassEx w: WrapperList){
            if(w.checkBox == true){
                Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                mail.setToAddresses(new String[] {w.conObj.Email__c});
                 mail.setTemplateId(et.Id);
                 mail.setWhatId(w.conObj.id);
                 mail.setTargetObjectId(w.conObj.physition__r.id);
                 mail.setTreatTargetObjectAsRecipient(false);
                mail.setReplyTo('hareeshn2008@magicsw.com');
                //mail.setplainTextBody('Hello');
               

                mail.setSenderDisplayName('Your Company Name');
                lstEmailId.add(mail);    
                                
                    
            }
          }
            if(lstEmailId.size()>0){
                try{
                    Messaging.sendEmail(lstEmailId);
                    ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.Confirm,'Sent!'));
                    
                }Catch(Exception ee){
                    ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.Error,ee.getMessage()));
                }
                
            }
       }     
}

customize omni channel logic to distribute cases based on Case Creation Date

Omni Channel queues distributes cases, based on Date/Time the case is assigned to the queue. we can customize this logic to look for some ...