1

Hi everyone i want to send multiple attachment through mail. So i created a checkbox to select the required document and send them but it is throwing this error

"Conversion Error setting value '01590000004fxbiAAA' for '#{documentId}'."

Please guide me how to fix it.

Controller

public with sharing class Send_Doc_Controller {
    // public ID documentId {get;set;}
    Public List<Id> documentId {get;set;}
    public String email {get;set;}
    public string[] s = new string[]{};

      public List<SelectOption> documents {
    get {
            documents = new List<SelectOption>();
            for(Document d : [SELECT id,Name FROM Document ]){
                Documents.add(new SelectOption(d.Id,d.name));
            }
            return documents;
    }
    set;
  }

  public PageReference sendDoc(){
          Document doc = [select id, name, body, contenttype, developername, type 
      from Document where id IN: documentId];

      system.debug('!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'+doc);

    Messaging.EmailFileAttachment attach = new Messaging.EmailFileAttachment();
    attach.setContentType(doc.contentType);
    attach.setFileName(doc.developerName+'.'+doc.type);
    attach.setInline(false);
    attach.Body = doc.Body;

    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
    mail.setUseSignature(false);

    s = email.split(',');
    system.debug('@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@'+s);
    mail.setToAddresses(s);
    system.debug('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%'+string.valueof(s));
    mail.setSubject('Document Email Demo');
    mail.setHtmlBody('Here is the email you requested: '+doc.name);
    mail.setFileAttachments(new Messaging.EmailFileAttachment[] { attach }); 

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

    ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'Email with Document sent to '+email));

    return null;
  }
}

My VF Page

<apex:page controller="Send_Doc_Controller" >
  <apex:sectionHeader title="Document Example" subtitle="Email a Document" 
    description="Example of how to email a Document."/>

  <apex:form >
    <apex:pageMessages />
    <apex:pageBlock title="Document Input">

      <apex:pageBlockButtons >
        <apex:commandButton action="{!sendDoc}" value="Send Document"/>
      </apex:pageBlockButtons>

      <apex:pageBlockSection >

        <apex:pageBlockSectionItem >
            <apex:outputLabel value="Email to send to" for="email"/>
          <apex:inputText value="{!email}" id="email"/>
        </apex:pageBlockSectionItem>

     <!--   <apex:pageBlockSectionItem >
            <apex:outputLabel value="Document" for="document"/>
            <apex:selectList value="{!documentId}" id="document" size="1">
                 <apex:selectOptions value="{!documents}"/>
            </apex:selectList>
        </apex:pageBlockSectionItem> -->

      </apex:pageBlockSection>

      <apex:pageBlockSection title="Select Documents" columns="2" >
          <apex:selectCheckboxes layout="pageDirection"  borderVisible="true" value="{!documentId}" id="document" legendInvisible="false"><apex:selectoptions value="{!documents}" /></apex:selectCheckboxes>
      </apex:pageBlockSection>

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

Add this to controller:

public Send_Doc_Controller(){
    documentId = new List<Id>();
}

Note that you should always declare variables if you intend to use them later. And make sure you add some other controls; for example: 1) if you didn't choose any document, you shouldn't be making queries and adding them to email, 2) email syntax,..

  • no luck still the same error – Sharat Sep 9 '14 at 12:30
  • I copy pasted your code in my org and added my code and it works like a charm. – ziga Sep 9 '14 at 19:51
  • Please guide me where i can put patch code in controller. Thanks in advance. Thanks, Manohar – user11492 Sep 10 '14 at 5:43
  • Hi Zinga, I was facing same problem your suggestion help me thanks.. When I checked more then one document it failed to sent mail... It show error "Error is in expression '{!sendDoc}' in component <apex:commandButton> in page senddoc: Class.SendDocController.sendDoc: line 21, column 1" – Dilip Mar 11 '15 at 14:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.