• Abdul Ayan Choudhary
  • NEWBIE
  • 0 Points
  • Member since 2017

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 2
    Replies
public with sharing class SendEmailMultipleAttachments {
        public List<Attachment>   attachList{get;set;}
        public List<SelectOption> numberList{get;set;}
        public String emailValue{get;set;}
        public String noofAttachments{get;set;}
        public String emailBody{get;set;}
        public Boolean rex{get;set;}


        public SendEmailMultipleAttachments() {
                numberList =  new List<Selectoption>();
                for(Integer i=0;i<5;i++){
                    String str =String.valueof(i);
                    numberList.add(new Selectoption(Str,str));
                }
                //attachList.add(new Attachment());
                rex=true;

                    
        }

        
        public void sendEmail(){

            System.debug(attachList);
            Messaging.SingleEmailMessage email = new 
            Messaging.SingleEmailMessage();
            String [] list1= emailValue.split(',');
            email.setToAddresses(list1);
            email.setSubject('test');
            email.setPlainTextBody(emailBody);
            List<Messaging.EmailFileAttachment> emailAttachList = new 
            List<Messaging.EmailFileAttachment>();
        
            for(Attachment att : AttachList){
                Messaging.EmailFileAttachment emailAttach = new 
                Messaging.EmailFileAttachment();
                   emailAttach.setFileName(att.name);
                
                emailAttach.setBody(att.body);
                emailAttachList.add(emailAttach);
               }
            email.SetFileAttachments(emailAttachList);
            Messaging.sendEmail(new Messaging.SingleEmailMessage[]{email});
            attachList=null;
            }


        
        public void fillList(){
                attachList =  new List<Attachment>();
                System.debug('In' + noofAttachments);
                for(Integer i=0 ; i < Integer.valueof(noofAttachments) ; i++){
                        attachList.add(new Attachment());
                }
                rex=false;
                System.debug('In' + noofAttachments);

        }
       }


Visualforce PAge:



             <apex:page tabStyle="Account" 
             controller="SendEmailMultipleAttachments">
             <apex:form >
              <apex:pageBlock >
                <apex:pageBlockSection columns="1"  id="repeat">
        
                   <apex:inputText label="Email:" value="{!emailValue}" 
                   id="emailfield" />
        
        
        <apex:selectList value="{!noofAttachments}" size="1" style="width: 
            250px" rendered="{!rex}" label="No Of Attachments" >
            <apex:selectOptions value="{!numberList}" />
            <apex:actionSupport event="onchange" action="{!fillList}" 
          reRender="repeat"   />
                
            
        </apex:selectList>
            <apex:repeat value="{!attachList}" var="att"   >
                <apex:inputFile title="Attachment " value="{!att.Body}" 
                fileName="{!att.Name}" id="file" rendered="
                {!if(noofAttachments!=null && noofAttachments!='',true,false)}" 
                />
            </apex:repeat>
        </apex:pageBlockSection>
        <apex:inputTextarea richText="true" value="{!emailBody}"/>
        <apex:pageBlockButtons location="bottom" id="btn">
            <apex:commandButton value="Send Email" action="{!sendEmail}" />
        </apex:pageBlockButtons>
        </apex:pageBlock>
        </apex:form>
        </apex:page>



 

wanted to count total number of records 

public List<Contact> contactsize = new List<Contact>();
     global void execute(Database.BatchableContext bc,  List<Contact> Scope){
          conupdate = new List<Contact>();
       system.debug('calling update check execute');
    
       For(Contact c: Scope){  
            c.Direct_send__c= true;
              conupdate.add(c);   
           }
       system.debug('contact list' + conupdate.size());  
         contactsize.addAll(conupdate);
         update conupdate;
     }  
    global void finish(Database.BatchableContext bc){
     system.debug('contacts size :' + contactsize.size());
        recordcount = contactsize.size();