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. 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
  7. This comment has been removed by the author.

    ReplyDelete
  8. 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
  9. Good article!!! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging…
    DevOps Training in Chennai

    DevOps Course in Chennai

    ReplyDelete

  10. Quick up for the best offer of AWS DevOps Training in Chennai from Infycle Technologies, Excellent software training in Chennai. A massive place to learn other technical courses like Power BI, Cyber Security, Graphic Design and Animation, Block Security, Java, Oracle, Python, Big data, Azure, Python, Manual and Automation Testing, DevOps, Medical Coding etc., with outstanding training with experienced trainers with a fresh environment with 100+ Live Practical Sessions and Real-Time scenario after the finalisation of the course the trainee will able to get through the interview in top MNC’s with an amazing package for more enquiry approach us on 7504633633, 7502633633.

    ReplyDelete
  11. It is very interesting! Really useful for me and thank you for this amazing blog.
    VA Divorce Lawyers
    How to get a Divorce in VA

    ReplyDelete

Post a Comment

Popular posts from this blog

Displaying pop-up summaries on hover in visualforce

Do you want to be certified as a Salesforce Admin?

Unit Testing in Salesforce