Tuesday 23 May 2017

Insert Attachment in to custom object using visualforce page and custom controller.

1. Create the custom object 'Applicant' and add the 'Notes and Attachment' related list in to the custom object page layout so that user can see attached files in this section.
2. Use '<apex:inputfile>' visualforce tag and define 'Attachement' type variable in class and use variable in tag to define attachment body and name.
3. After insert displaying list of inserted attachment names below of text field.
4. Define the 'Applicant__c'  in standard controller to use property of this object in page and create custom controller to insert and display the attachments names and use this controller in extension tag of visualforce page.
5. When attachment insert in Applicant custom object then provide the Applicant record id in to Attachment parentId field.

Click here for Demo


Visualforce Page:

<apex:page standardController="Applicant__c" extensions="attachfile" showHeader="false" sidebar="false">
      <apex:form id="frm">
          <apex:pageBlock title="Submit File">
             <apex:pageMessages />
              <apex:pageBlockButtons >
                  <apex:commandButton value="Save" action="{!saveApplicant}"/>
              </apex:pageBlockButtons>
              <apex:pageBlockSection >
                  <apex:inputField value="{!objApplicant.name}"/>
                  <apex:inputfile value="{!objAttachment.body}" filename="{!objAttachment.name}"></apex:inputfile>
              </apex:pageBlockSection>
              <apex:pageBlockTable value="{!Attachments}" var="att" id="table">
                  <apex:column headerValue="Attachment Names" value="{!att}"/>
              </apex:pageBlockTable>
          </apex:pageBlock>
          
      </apex:form>
</apex:page>

Apex Class:

Public class attachfile
{
    Public attachment objAttachment{get; set;}
    Public Applicant__c objApplicant{get; set;}
    public List<String> attchNames{get;set;}
    Public attachfile(apexpages.standardcontroller stdCon)
    {
        objAttachment = new Attachment();
        objApplicant = new Applicant__c();
        getAttachments();
    }
    Public PageReference saveApplicant()
    {
        insert objApplicant;
       
        objAttachment.ParentId = objApplicant.id;
        insert objAttachment;
        PageReference page=new PageReference('/apex/attach'); 
        page.setRedirect(true);
        return page;
    }
    public List<String> getAttachments()
    {
        attchNames=new List<String>();
        List<Applicant__c> lstAttach=[Select id,name,(Select id,name from Attachments order by createddate desc) from Applicant__c];
        for(Applicant__c app:lstAttach)
        {
            for(Attachment a:app.Attachments)
            {
                attchNames.add(a.Name);
            }
        }
        System.debug('attchNames:='+attchNames);
        attchNames.sort();
        return attchNames;
    }
}

0 comments:

Post a Comment

If you have any doubts, please let me know.