function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
NickHockingNickHocking 

How to attach a file to a new custom object record in VF page?

Hi All,

I'm trying to create a very simple VF page which will allow the user to fill in their name and upload an attachment. When they click save, it will simply create a new record for that Custom object, and have the file as an attachment.

Is this possible?

So far, I have:
<apex:page standardController="Applicant__c">
      <apex:form >
          <apex:pageBlock title="Submit File">
             <apex:pageMessages />
              <apex:pageBlockButtons >
                  <apex:commandButton value="Save" action="{!save}"/>
              </apex:pageBlockButtons>
              <apex:pageBlockSection >
                  <apex:inputField value="{!Applicant__c.name}"/>
                  <apex:inputFile value="" filename="{}">
              </apex:pageBlockSection>
          </apex:pageBlock>
      </apex:form>
  </apex:page>



But i have no idea what to put in the attributes for the inputFile. The Applicant__c object is a custom object, which currently just has a Name field. Attachments are enabled for this object.

Any advice would be greatly appreciated....many thanks,

Nick
Best Answer chosen by NickHocking
hitesh90hitesh90
Hi Nick,

You have to use Custome controller here to save the attachment and record also.
see below example.

Visualforce Page:
<apex:page standardController="Applicant__c" extensions="extattachfile">
      <apex:form >
          <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:pageBlock>
      </apex:form>
  </apex:page>

Apex Class:
Public class extattachfile{
    Public attachment objAttachment{get; set;}
    Public Applicant__c objApplicant{get; set;}
    Public extattachfile(apexpages.standardcontroller stdCon){
        objAttachment = new Attachment();
        objApplicant = new Applicant__c();
    }
    Public void saveApplicant(){
        insert objApplicant;
       
        objAttachment.ParentId = objApplicant.id;
        insert objAttachment;
    }
}

Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator & Advanced Administrator & Sales cloud consultant
My Blog:- http://mrjavascript.blogspot.in/

All Answers

hitesh90hitesh90
Hi Nick,

You have to use Custome controller here to save the attachment and record also.
see below example.

Visualforce Page:
<apex:page standardController="Applicant__c" extensions="extattachfile">
      <apex:form >
          <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:pageBlock>
      </apex:form>
  </apex:page>

Apex Class:
Public class extattachfile{
    Public attachment objAttachment{get; set;}
    Public Applicant__c objApplicant{get; set;}
    Public extattachfile(apexpages.standardcontroller stdCon){
        objAttachment = new Attachment();
        objApplicant = new Applicant__c();
    }
    Public void saveApplicant(){
        insert objApplicant;
       
        objAttachment.ParentId = objApplicant.id;
        insert objAttachment;
    }
}

Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator & Advanced Administrator & Sales cloud consultant
My Blog:- http://mrjavascript.blogspot.in/
This was selected as the best answer
NickHockingNickHocking
Thanks so much Hitesh! That worked perfectly.

Now i just need to figure out how to add validation, more fields, and embed the form into my website!
Hitesh PatelHitesh Patel

file.Parentid=fu.name;
Replace this line with below.

file.Parentid=fu.Id;