function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SMasterSMaster 

Problem related to select an Attachment in Visualforce Page.

Hi,

 

Happy New Year to you all..   :)

 

I want to select an attachment in visualforce page and sent an email with that selected attachment.. all the attachments are getting displayed on the visualforce page from the Notes and Attachment related list..

 

Now while clicking the "Send" button all the attachments are going out.. but i need to send only the selected attachments.. please suggest what am i missing......

 

 

 

 

I have the following Apex Class:

 

public class testmail
{
 ApexPages.StandardController controller;   
 
 public opportunity_proposals__c q
 {
 get;set;
 }
    
 String op = ApexPages.currentPage().getParameters().get('id');
 
 public Id oppr   
 {    get;set;    }       
 
 public testmail(ApexPages.StandardController ctrl)   
 {      
 oppr = ctrl.getRecord().Id;        
 }
 
 public PageReference emailAtt()
 {
 
 Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        String[] toAddresses = new String[]{'SMaster@gmail.com'};
        mail.setToAddresses(toAddresses);
        mail.setReplyTo('SMaster@gmail.com');
        mail.setSenderDisplayName('CRM Support');
        mail.setBccSender(false);
        mail.setUseSignature(false);
        mail.setTargetObjectId('005Q0000000FR7f');
        mail.setTemplateId('00XQ0000000QULj');
        mail.saveAsActivity = false;    
        
      //Set email file attachments
        List<Messaging.Emailfileattachment> fileAttachments = new List<Messaging.Emailfileattachment>();
        for (Attachment a : [select Id, Name, Body, BodyLength from Attachment where ParentId = :oppr])
        {
     // Add to attachment file list
        Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment();
        efa.setFileName(a.Name);
        efa.setBody(a.Body);
        fileAttachments.add(efa);
        }
        mail.setFileAttachments(fileAttachments);

      //Send email
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
        return null;
}
}

 

 

and the following Visualforce page:

 

<apex:page standardController="opportunity_proposals__c" extensions="testmail">
  <apex:form >
   <apex:pageBlock title="Attachments">
   <apex:pageBlockButtons >
            <apex:commandButton value="Send" action="{!emailAtt}"/>
        </apex:pageBlockButtons>


     <apex:pageBlockTable value="{!opportunity_proposals__c.Attachments}" var="Att">
     <apex:column headerValue="Select">
            <apex:inputCheckbox value="{!opportunity_proposals__c.for_sendemailwithattachment__c}"/>
        </apex:column>
       <apex:column value="{!Att.createddate}"/>
       <apex:column value="{!Att.name}"/>
       <apex:column value="{!Att.description}"/>
     </apex:pageBlockTable>
     
      </apex:pageBlock>

  </apex:form>
</apex:page>

Cory CowgillCory Cowgill

Your query is gettting all of them for the given record. This query is gettting all the attachments:

 

for (Attachment a : [select Id, Name, Body, BodyLength from Attachment where ParentId = :oppr])

 

Inside the Controller before you call this SOQL you should build a list of the selected Attachment ID's andmodify your query to use and in clause on those ids like this:

 

for (Attachment a : [select Id, Name, Body, BodyLength from Attachment where ParentId = :oppr and Id in:attachids])

SMasterSMaster

Hi,

 

will iave to use the wrapper class for this...