Save an attachment in APEX

Attachment field allows users to be able to attach notes and attachments to custom object records. This allows you to attach external documents to any object record, in much the same way that you can add a PDF or photo as an attachment to an email. This option is only available when you are creating a new object. Here I have an example for save an attachment in APEX.


public class status{
private final Applicant__c applicant;
public Blob resume {get; set;}
public String contentType {get; set;}
public String fileName {get; set;}

public status(ApexPages.StandardController stdController) {
public PageReference saveApplication() {

}catch(System.DMLException e){
return null;

Attachment attach=new Attachment();
try {
} catch(System.DMLException e) {
return null;
PageReference p = Page.Confirmpage;
return p;

<apex:page standardController="Applicant__c" extensions="status">
<apex:form >
<td>Applicant Name </td>
<td><apex:inputField value="{!Applicant__c.Name__c}"/></td>
<td>CV </td>
<td> <apex:inputFile accept="doc, txt, pdf" filename="{!fileName}" contentType="{!contentType}" filesize="1000" size="50" value="{!resume}"/> </td>
<td><apex:commandButton id="submitApplicant" value="Submit" action="{!saveApplication}"/></td>

You can try it with your own object and particular fields.


  1. We're having trouble talking to
    Either login to Facebook directly or try again later.

  2. but I think the Name__c value is null and the the field name value is the id of custom object

  3. Is there a way to validate if the file is chosen or not - the validation error should be on page and not as a pop-up? Can we do it?

  4. How to write test class for this code?Please help.

  5. Insert Attachment successfully . But the form accept 'HTML,TAR ,etc' files
    "accept="doc, txt, pdf" is not working !!

  6. I have read your blog its very attractive and impressive. I like your blog salesforce Online Training Bangalore

  7. Hi! I could have sworn I've been to this website before but after checking through some of the post I realized it's new to me. Anyways, I'm definitely happy I found it and I'll be bookmarking and checking back frequently!Clinically Oriented Anatomy test bank & solutions manual

  8. Thanks for sharing this great article. It made me understand few things about this concept which I never knew before. Keep posting such great articles so that I gain from it. Java Training in Chennai | J2EE Training in Chennai | Advanced Java Training in Chennai | Core Java Training in Chennai | Java Training institute in Chennai

  9. Appreciating the persistence you put into your blog and detailed information you provide.
    Aws training chennai | AWS course in chennai

  10. And indeed, I’m just always astounded concerning the remarkable things served by you. Some four facts on this page are undeniably the most effective I’ve had.

    Best PHP Training Institute in Chennai|PHP Course in chennai
    Best .Net Training Institute in Chennai
    Big Data Hadoop Training in Chennai
    Linux Training in Chennai
    Cloud Computing Training in Chennai

  11. This comment has been removed by the author.

  12. Thanks for sharing valuable information
    We are the best piping design course in Hyderabad, India. Sanjary academy Offers Piping Design Course and Best Piping Design Training Institute in Hyderabad. Piping Design Institute in India Piping Design Engineering.
    Piping Design Course
    Piping Design Course in india
    Piping Design Course in hyderabad

  13. Good Information
    Sanjary kids is the best playschool, preschool in Hyderabad, India. Start your play school,preschool in Hyderabad with sanjary kids. Sanjary kids provides programs like Play group,Nursery,Junior KG,Serior KG,and Teacher Training Program.
    play school in hyderabad
    Preschool in hyderabad
    Preschool teacher training course in hyderabad
    pre and primary teacher training course in hyderabad


Post a Comment

Popular posts from this blog

Parameter passing using Javascript+actionFunction in visualforce

Displaying pop-up summaries on hover in visualforce