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.

/*****Controler*****/

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) {
this.applicant=(Applicant__c)stdController.getRecord();
}
public PageReference saveApplication() {

try{
insert(applicant);
}catch(System.DMLException e){
ApexPages.addMessages(e);
return null;
}

if(resume!=null){
Attachment attach=new Attachment();
attach.Body=resume;
attach.Name=filename;
attach.ContentType=contentType;
attach.ParentID=applicant.id;
try {
insert(attach);
} catch(System.DMLException e) {
ApexPages.addMessages(e);
return null;
}
}
PageReference p = Page.Confirmpage;
p.setRedirect(true);
return p;
}
}

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

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

Comments

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

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

    ReplyDelete
  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?

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

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

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

    ReplyDelete
  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

    ReplyDelete
  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

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

    ReplyDelete
  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

    ReplyDelete
  11. This comment has been removed by the author.

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete

Post a Comment

Popular posts from this blog

Parameter passing using Javascript+actionFunction in visualforce

Displaying pop-up summaries on hover in visualforce