Attach Files To Records In Salesforce

Create an Attachment:

If your form contains a file upload a file upload field,you can attach the uploaded files to most salesforce objects by creating a dependent Attachment object.
  1. Create the parent object for the Attachment object.
  2. Click the Choose File  button, and select the Attachment object from the list.
  3. You will need to map the ID of the parent object to the Attachment object's Parent ID. To do this,map the Parent ID in the field mapping section.Note that is a required field.
  4. The othe required fields for the Attachment object , "File Name" and "Body," will also appear in the Field Mapping section. You will nedd to map both of these fields to the file upload field in your form.
Here i have created one record of Account object and attached file to that record.
Code:
Apex Class:
public with sharing class AttachmentUploadController 
{
    public String parentId {get;set;}
    public Attachment attach {get;set;}
   public AttachmentUploadController()
    {
        attach = new Attachment();
    }
    //When user clicks upload button on Visualforce Page, perform upload/insert
    //Redirect user to newly inserted document
    public ApexPages.Pagereference upload()
    {
         Account a =new Account();
   a.Name='Sample Attachment ';
    insert a;
        //This shows how to insert an Attachment
        attach.ParentId = a.Id;
        insert attach;
return new ApexPages.Standardcontroller(attach).view();   
    }
}
VisualForce Page:

<apex:page controller="AttachmentUploadController">
    <apex:form >
        <apex:outputText value="Input File:  "/><apex:inputFile value="{!attach.body}" filename="{!attach.name}"/><br/>
        <apex:commandButton value="Upload" action="{!upload}"/>
    </apex:form>
</apex:page>
OutCome:









Comments

  1. mybet youtube login for sportsbet real money
    mybet real money live slot casino game online how to convert youtube to mp3 roulette online slot machine real money. play casino roulette online slots online free games. mybet.

    ReplyDelete

Post a Comment

Popular posts from this blog

Configur Docusign For Salesforce

How To Make DataTable Column Resizable

Lightning:recordForm - Lightning Data Service