title: Salesforce custom object and attachments
body: <p>Ok, I've been tasked with creating a custom salesforce object that saves some data. That's the easy part.</p>
  
  <p>Where I'm struggling is adding a piece to upload files associated with the record created in the custom object.</p>
  
  <p>The custom object is essentially a form with fields on it. But I also need to upload relevant files.</p>
  
  <p>So I built the form part of the custom object. I used an Apex class and a VisualForce page for the "upload" part.</p>
  
  <p>Unfortunately, in the course of testing, I keep getting errors on the VisualForce/Apex piece.</p>
  
  <p>The errors I get are the following:</p>
  
  <p>Parent: Parent ID: id value of incorrect type: 00590000000yBOmAAM</p>
  
  <p>And my Apex class looks like this:</p>
  
  <pre><code>    public with sharing class FileToDocument {
      public Attachment attachment{
          get{
              if(attachment == null)
                  attachment = new Attachment();
              return attachment;
          }
          set;
      }           
  
      public PageReference upload(){
          attachment.OwnerId = UserInfo.getUserId();
          attachment.ParentId = '00590000000yBOm';
          attachment.IsPrivate = false;
  
          try{
              insert attachment;
          }
          catch(DMLException e){
              ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR, 'Error Uploading Attachment'));
              return null;
          }
          finally{
              attachment = new Attachment();
          }
  
          ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO, 'Attachment Uploaded Successfully'));
  
          return null;
      }
  }
  </code></pre>
  
  <p>I am very much a novice at SalesForce. I think the problem is the ParentID line, but I don't know exactly how to fix it.</p>
  
  <p>What I'm trying to do is to be able to upload/attach documents to a record, while I'm filling out the form for the record.</p>
  
  <p>My VisualForce looks like this:</p>
  
  <pre><code>&lt;apex:page controller="FileToDocument"&gt;
      &lt;apex:form enctype="multipart/form-data"&gt;
          &lt;apex:pageMessages /&gt;
          &lt;apex:pageblock &gt;
              &lt;apex:pageBlockButtons &gt;
                  &lt;apex:commandButton action="{!upload}" value="Attach"/&gt;
              &lt;/apex:pageBlockButtons&gt;
  
              &lt;apex:pageBlockSection showHeader="false" columns="2" id="block1"&gt;
                  &lt;apex:pageBlockSectionItem &gt;
                      &lt;apex:outputLabel value="File Name" for="fileName"/&gt;
                      &lt;apex:inputText value="{!attachment.name}" id="fileName"/&gt;
                  &lt;/apex:pageBlockSectionItem&gt;
  
                  &lt;apex:pageBlockSectionItem &gt;
                      &lt;apex:outputLabel value="File" for="file"/&gt;
                      &lt;apex:inputFile value="{!attachment.body}" filename="{!attachment.name}" id="file"/&gt;
                  &lt;/apex:pageBlockSectionItem&gt;
              &lt;/apex:pageBlockSection&gt;
          &lt;/apex:pageblock&gt;
      &lt;/apex:form&gt;
  &lt;/apex:page&gt;
  </code></pre>
  
  <p>So what could I be doing wrong? My other thought is that there's simply no way to upload appropriate files until after the record has been created. Even then, I think I'd still need to know how to associate it with the Parent ID.</p>
  
  <p>Any help is greatly appreciated, as always.</p>