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
kyleRochekyleRoche 

inputFile to Account attachment

How do you link a inputFile upload to an Account record?
jwetzlerjwetzler
You need to use inputFile on the attachment object (value="{!attachment.body}") where attachment is a new Attachment().  Before you save it you have to set attachment.parentId to your account id to associate it with that account.
kyleRochekyleRoche
Hi Jill,

Thanks for the reply. So, here's what I put together. I tried to match your sample. I'm missing something. Can you help please?

Thanks,
Kyle.


Code:
<apex:page standardController="Account" extensions="InputFileControllerExtension"> 
    <apex:messages /> 
    <apex:form id="theForm"> 
        <apex:pageBlock > 
            <apex:pageBlockSection > 
                <!-- <apex:inputFile value="{!document.body}" filename="{!document.name}"/>  -->
                <apex:inputFile value="{!attachment.body}" filename="{!attachment.name}"/>
                <apex:commandButton value="save" action="{!save}"/> 
            </apex:pageBlockSection> 
        </apex:pageBlock> 
    </apex:form> 
</apex:page>

 
Code:
public class InputFileControllerExtension {
 
   private final Account acct; 
   
   public Attachment attachment
   {
     get
     {
      if (attachment != null)
      {
       attachment = new Attachment();
       attachment.parentId = acct.id;
       return attachment;
      }
      return attachment;
     }
     set;
   }
   
   public Document document
 {
  get 
  {
   if (document != null)
   {
    document = new Document();
    document.folderid = acct.id;
    return document;
   }
   return document;
  }
  set;
 }
 
 public InputFileControllerExtension(ApexPages.StandardController stdController)
 { 
  this.acct = (Account)stdController.getRecord(); 
 } 
}

 

 
jwetzlerjwetzler
You're almost there but you're getting confused because your standard controller is on Account instead of Attachment.

Code:
public class InputFileControllerExtension {
 
   private final Account acct; 
   
   public Attachment attachment {get;set;}
   
   public PageReference save() {
     attachment.parentid = acct.id;
     insert attachment;
     return stdController.save();
   }
 
 public InputFileControllerExtension(ApexPages.StandardController stdController)
 { 
  attachment = new Attachment();
  this.acct = (Account)stdController.getRecord(); 
  this.stdController = stdController;
 } 
 
 ApexPages.StandardController stdController;
}

 When Save is called by the standard controller it's going to save the Account, because that's the object you have your standard controller bound to.  That means you have to insert the attachment yourself, before calling through to stdController.save(), which will then perform the regular save functionality on the account.

Make sense?
kyleRochekyleRoche
Ah.. makes sense. Thanks Jill!
rlreynosorlreynoso

I have followed this code to build the same process for a custom object however i keep getting an Authorization Required page/error after save.  I dont see anywere guest user profile to allow insert attachment permission.  What am i doing wrong? 

rlreynosorlreynoso

It turns out that the "Authorization Required" page appears for other reasons such as an error while inserting.  I created a debug log for the Site Guest User and found that the problem was my code.  The Parent ID was not populating as i assumed it was.  When i hard coded a parent ID it work like a charm.  I just need to find out why my code is not taking the newly created record ID and assigning it to the Attachment Parent ID. 

JoanaJoana

Hi,

 

Is it possible to use <apex:inputFile > in a visualforce page and not use the Attachment or Document standard controller?

 

I have a Visualforce pages that it is used for uploading files but it can be accessed trough custom buttons in Cases or in other custom objects.  The purpose of this page is for the user to upload a file, associate it to Case or any other object he desires and also give a description of the file. The document will only be saved in salesforce if the users decides to press a custom button "Finish" (he may decide that after all he doesn't want to upload the file he has choosen).

 

The problem is that my variables "file" and "fileName" are always null.

 

Here's part of my code:

 

<apex:page controller="CC_TipificarDocumentosController" >

    <apex:form >

          <apex:outputText value="Procurar Ficheiros" />

           <apex:outputPanel layout="block" styleClass="bd">

                   <apex:inputFile title="Procurar" value="{!file}" filename="{!fileName}" id="inpFile"/>

                   <apex:commandButton value="Finish" action="{!criarDoc}" />        
          </apex:outputPanel>

      </apex:form>
</apex:page>

 

 

public void criarDoc(){

System.debug('\n\n\n############# fileName:' + fileName);

}

 

 

 

Can someone give me a hand?

Thank you very much!