+ Start a Discussion
Shawn ReichnerShawn Reichner 

Capturing attachment on Visualforce page

Hoping that one of you awesome Salesforce experts can help me with the following issue I am having. 

I have an force.com Site which has a VisualForce page which should open a custom object record on Salesforce.  I also have a need to have an attachment added to this VF page to then be attached to the new record being created upon the user pressing save.  

Here is what I am seeign so far, if I do not include the Controller extension, the record gets created just fine, but when I have the extension added to the VF page, no record is created. 

I am new to apex, so hoping that someone can look over my code below and provide a solution to my frustration :) 

Thank you

Shawn

VF Page Code - 
 
<apex:page standardcontroller="Backlog__c" extensions="backlogAttachment,PopulateSchedRelease"
showHeader="true">
<img src="{!$resource.AVISPL_Logo2}"></img><b/><b/>
    <apex:form >
    <apex:pageBlock >
<apex:pageBlockSection title="Software Enhancement Request" columns="1" showHeader="True" collapsible="False">
</apex:pageBlockSection>
</apex:pageBlock>
<apex:pageMessages />

        <apex:pageBlock >
        <apex:inputField value="{!objcase.Name}"/>
            <apex:pageBlockSection title="Request/Requestor Information">
                <apex:inputfield value="{!objcase.SER_Requestor__c}" required="True"/>
                <apex:inputfield value="{!objcase.Priority__c}" required="True"/>
                <apex:inputField value="{!objcase.SER_Requestor_email__c}" required="True"/>
                <apex:inputfield value="{!objcase.Stakeholder__c}" required="True"/>
                <apex:inputField value="{!objcase.Requestor_GL_Code__c}"/>
                <apex:inputfield value="{!objcase.Executive_Sponsor__c}"/>
                
                
            </apex:pageBlockSection>

            <apex:pageBlockSection title="Impact of Request">
                <apex:inputField value="{!objcase.SER_Category__c}" required="True"/>
                <apex:inputField value="{!objcase.SER_Sub_Category__c}"/>     
                <apex:inputField value="{!objcase.SER_Designation__c}" required="True"/>
                <apex:inputField value="{!objcase.Number_of_Users_Affected__c}" required="True"/>
                
                <br/>
              
            </apex:pageBlockSection>
            
            <apex:pageBlockSection title="Request Description Information">
                <apex:inputField value="{!objcase.Summary__c}" required="True"/>
                <apex:inputField value="{!objcase.Reason__c}" required="True"/>     
                <apex:inputField value="{!objcase.Description__c}" required="True"/>
                <apex:inputField value="{!objcase.Benefit__c}" required="True"/>
                
                <br/>
              
            </apex:pageBlockSection>
            
            <apex:pageBlockSection title="Deadlines and Project Status">
                <apex:inputField value="{!objcase.Training_Requirements__c}" required="True"/>
                <apex:inputField value="{!objcase.Target_Deadline__c}" required="True"/>
                <apex:inputField value="{!objcase.SOP_Requirements__c}" required="True"/>
                <apex:inputField value="{!objcase.List_All_U_A_T_Participants__c}" required="True"/>     
                <apex:inputField value="{!objcase.SER_Scheduled_Release__c}"/>
                <apex:inputField value="{!objcase.Scheduled_Release__c}" rendered="False"/>
                
                
                <br/>
              
            </apex:pageBlockSection>
            
         <apex:pageBlock title="Upload Attachment">
            <apex:inputFile style="width:100%" id="fileToUpload" value="{!objAttachment.Body}" filename="{!objAttachment.Name}" />
           
       </apex:pageBlock>

           <apex:commandButton value="Submit Request" action="{!saveBacklog}" />
            
        </apex:pageBlock>
    </apex:form>
</apex:page>

First Extention to pass ID of another custom object record and populate a lookup field when record is created.
 
public class PopulateSchedRelease {

  public Backlog__c bl;

  public PopulateSchedRelease(ApexPages.StandardController controller) {
  
  this.bl = (Backlog__c)controller.getRecord();
  Scheduled_Releases__c sr = getReleaseMethod1();
  bl.Scheduled_Release__c = sr.Id;
        
   }

   Scheduled_Releases__c s;

   public Scheduled_Releases__c getReleaseMethod1() {
       if(s == null) s = [SELECT Id, Name FROM Scheduled_Releases__c WHERE Name = 'Software Enhancement Request (Default)' LIMIT 1];
       return s;
   }

}

And lastly, the second controller extension for handling the attachment.
 
public class backlogAttachment {
    
    public blob getfile{get;set;}
    public attachment objAttachment{get;set;}
    public Backlog__c objcase{get;set;}
    public string filename{get;set;}
    
    public backlogAttachment(apexpages.standardcontroller stdCon) {
    
        objcase = new Backlog__c();
        objAttachment = new Attachment();
        
       }
    
    Public PageReference saveBacklog() {
    
        insert objcase;
        objAttachment.body = getfile;
        objAttachment.ParentId = objcase.Id;
        objAttachment.name = filename;
        insert objAttachment;
        return ApexPages.currentPage();
    }


}

PLEASE HELP!!!!
Best Answer chosen by Shawn Reichner
Balayesu ChilakalapudiBalayesu Chilakalapudi
The attachment part of Your vf page must be like
<apex:inputFile style="width:100%" id="fileToUpload" value="{!objcase.getfile}" filename="{!objcase.filename}" />
Note: The maximum file size that can be uploaded via Visualforce is 10 MB.
use a try-catch block to monitor the exception and use the below code.
public with sharing class backlogAttachment {
    
    public blob getfile{get;set;}
    public attachment objAttachment{get;set;}
    public Backlog__c objcase{get;set;}
    public string filename{get;set;}
    
    public backlogAttachment(apexpages.standardcontroller controller) {
    
        objcase = new Backlog__c();
        objAttachment = new Attachment();
        Scheduled_Releases__c sr = getReleaseMethod1();
        objcase.Scheduled_Release__c = sr.Id;
       }
    
    Scheduled_Releases__c s;
    
    public Scheduled_Releases__c getReleaseMethod1(){
    
        if(s == null) s=[SELECT Id, Name FROM Scheduled_Releases__c WHERE Name ='Software Enhancement Request (Default)' LIMIT 1];
        return s;
    }
    Public PageReference saveBacklog() {
    try{
        insert objcase;
    } catch(DMLException e) {
        ApexPages.addMessages(e);
        }
    if(filename != null && fileName.trim().length()>0 && getfile != null) {
        //objAttachment = new Attachment();
        Integer i=0;
        objAttachment.clear();
        objAttachment.body = getfile;
        objAttachment.ParentId = objcase.Id;
        objAttachment.name = this.filename;
        try{
        insert objAttachment;
          }catch(Exception e){
             System.debug(e);
          }
        }
        
        pagereference pr = new pagereference('/'+objcase.id);
        return pr;
    }


}

 

All Answers

Balayesu ChilakalapudiBalayesu Chilakalapudi
Try changing the order of placing the extension names like
extensions="PopulateSchedRelease,backlogAttachment"
Shawn ReichnerShawn Reichner
Bala, thank you for your imput, and doing the different sequence did not provide different results.  No record is still created.  I have edit the controller to attempt to bring both extensions into one extention and now the record creates with no attachment added on the VF page, and also creates a record when an attachment is selecte don the VF page, but the attachment does not get created now.  Can you look over the changes below and see if there is anything incorrect about the way I did this, but it seems as if we are getting closer :)
 
public with sharing class backlogAttachment {
    
    public blob getfile{get;set;}
    public attachment objAttachment{get;set;}
    public Backlog__c objcase{get;set;}
    public string filename{get;set;}
    
    public backlogAttachment(apexpages.standardcontroller controller) {
    
        objcase = new Backlog__c();
        objAttachment = new Attachment();
        Scheduled_Releases__c sr = getReleaseMethod1();
        objcase.Scheduled_Release__c = sr.Id;
       }
    
    Scheduled_Releases__c s;
    
    public Scheduled_Releases__c getReleaseMethod1(){
    
        if(s == null) s=[SELECT Id, Name FROM Scheduled_Releases__c WHERE Name ='Software Enhancement Request (Default)' LIMIT 1];
        return s;
    }
    Public PageReference saveBacklog() {
    try{
        insert objcase;
    } catch(DMLException e) {
        ApexPages.addMessages(e);
        }
    if(filename != null && fileName.trim().length()>0 && getfile != null) {
        objAttachment = new Attachment();
        Integer i=0;
        objAttachment.clear();
        objAttachment.body = getfile;
        objAttachment.ParentId = objcase.Id;
        objAttachment.name = this.filename;
        insert objAttachment;
        }
        
        pagereference pr = new pagereference('/'+objcase.id);
        return pr;
    }


}

 
Balayesu ChilakalapudiBalayesu Chilakalapudi
The attachment part of Your vf page must be like
<apex:inputFile style="width:100%" id="fileToUpload" value="{!objcase.getfile}" filename="{!objcase.filename}" />
Note: The maximum file size that can be uploaded via Visualforce is 10 MB.
use a try-catch block to monitor the exception and use the below code.
public with sharing class backlogAttachment {
    
    public blob getfile{get;set;}
    public attachment objAttachment{get;set;}
    public Backlog__c objcase{get;set;}
    public string filename{get;set;}
    
    public backlogAttachment(apexpages.standardcontroller controller) {
    
        objcase = new Backlog__c();
        objAttachment = new Attachment();
        Scheduled_Releases__c sr = getReleaseMethod1();
        objcase.Scheduled_Release__c = sr.Id;
       }
    
    Scheduled_Releases__c s;
    
    public Scheduled_Releases__c getReleaseMethod1(){
    
        if(s == null) s=[SELECT Id, Name FROM Scheduled_Releases__c WHERE Name ='Software Enhancement Request (Default)' LIMIT 1];
        return s;
    }
    Public PageReference saveBacklog() {
    try{
        insert objcase;
    } catch(DMLException e) {
        ApexPages.addMessages(e);
        }
    if(filename != null && fileName.trim().length()>0 && getfile != null) {
        //objAttachment = new Attachment();
        Integer i=0;
        objAttachment.clear();
        objAttachment.body = getfile;
        objAttachment.ParentId = objcase.Id;
        objAttachment.name = this.filename;
        try{
        insert objAttachment;
          }catch(Exception e){
             System.debug(e);
          }
        }
        
        pagereference pr = new pagereference('/'+objcase.id);
        return pr;
    }


}

 
This was selected as the best answer
Shawn ReichnerShawn Reichner
Bala, you got me so close sir!!!  FO rthe VF page suggestion by placing objcase.filename  or objcase.getfile produced an error that those fields do not exist on the object for which the VF page is creating a record for.  By removing the objcase form each of those, it now works as intended!!!!!  Thank you so much sir.  I will mark the last post from you as the best answer as it was sooo close! 

Thanks again

Shawn