+ Start a Discussion
Will Jones 18Will Jones 18 

Visualforce Lookup Field not working

I utilized Jeff Douglas' custom attachment code to create a custom object with a visualforce page.


That worked fine. I added a lookup field to the attachment object. It is a lookup to a custom object that is filtered by the Opportunity ID.

User-added image

It works fine without the visualforce page. Now I need to add the lookup field to the custom controller and visualforce page. Users should be able to associate the attachment with the custom object as their creating the record from the visualforce page. I am having trouble adding it. I was able to add the field (Vendor Event Detail) but its read only and I cant do a lookup at all.

User-added image

Not sure where I should start with figuring out how to get the lookup icon to appear and make sure that the lookup is filtered by the opportunity it is associated with. Below is the code.

Visualforce Page:
<apex:page standardController="Opportunity" tabStyle="Opportunity" extensions="UploadAttachmentController">
 <apex:sectionHeader title="{!Opportunity.Name}" subtitle="Attach File"/>
 <apex:form id="form_Upload">
 <apex:pageBlock >
 <apex:pageBlockButtons >
   <apex:commandButton action="{!back}" value="Back to {!Opportunity.Name}"/>
   <apex:commandButton action="{!back}" value="Cancel"/>
 <apex:pageMessages />
  <apex:pageBlockSection columns="1">
    <apex:pageBlockSectionItem >
      <apex:outputLabel value="File" for="file_File"/>
      <apex:inputFile id="file_File" value="{!fileBody}" filename="{!fileName}"/>
    <apex:pageBlockSectionItem >
      <apex:outputLabel value="Vendor Event Detail" for="VED"/>
      <apex:inputField id="VED" value="{!VED.Id}"/>

    <apex:pageBlockSectionItem >
      <apex:outputLabel value="Contract Type" for="contractType"/>
      <apex:selectList value="{!contractType}" size="1" id="type">
        <apex:selectOption itemvalue=""  itemLabel="--None--"/> 
        <apex:selectOption itemValue="Executed" itemLabel="Executed"/>
        <apex:selectOption itemValue="Final" itemLabel="Final"/>
    <apex:pageBlockSectionItem >
      <apex:outputLabel value="Description" for="description"/> 
      <apex:inputTextarea id="description" value="{!description}" rows="4" cols="50"/>
    <apex:pageBlockSectionItem >
      <apex:outputLabel value="" for="uploadBtn"/> 
      <apex:commandButton id="uploadBtn" value="Attach File" action="{!processUpload}" />


public class UploadAttachmentController {
    public String contractType {get;set;}
    public String description {get;set;}
    String AttachmentId;
    public Attachment__c VED {get;set;}
    private Opportunity Opportunity {get;set;} 
    public String fileName {get;set;}
    public Blob fileBody {get;set;}
    public UploadAttachmentController(ApexPages.StandardController controller) { 
        this.Opportunity = (Opportunity)controller.getRecord();
        VED = new Attachment__c();
        VED.Opportunity__c = this.Opportunity.Id;
    // creates a new Attachment__c record
    private Database.SaveResult saveCustomAttachment() {
        Attachment__c obj = new Attachment__c();
        obj.Opportunity__c = Opportunity.Id;
        obj.description__c = description;
        obj.Contract_Type__c = contractType;
        obj.Vendor_Event_Detail__c = VED.Id;
        return Database.insert(obj);
    // create an actual Attachment record with the Attachment__c as parent
    private Database.SaveResult saveStandardAttachment(Id parentId) {
        Database.SaveResult result;
        Attachment attachment = new Attachment();
        attachment.body = this.fileBody;
        attachment.name = this.fileName;
        attachment.parentId = parentId;
        // insert the attachment
        result = Database.insert(attachment);
        // reset the file for the view state
        fileBody = Blob.valueOf(' ');
        return result;
    * Upload process is:
    *  1. Insert new Attachment__c record
    *  2. Insert new Attachment with the new Attachment__c record as parent
    *  3. Update the Attachment__c record with the ID of the new Attachment
    public PageReference processUpload() {
        try {
            Database.SaveResult customAttachmentResult = saveCustomAttachment();
            if (customAttachmentResult == null || !customAttachmentResult.isSuccess()) {
                ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 
                  'Could not save attachment.'));
                return null;
            Database.SaveResult attachmentResult = saveStandardAttachment(customAttachmentResult.getId());
            if (attachmentResult == null || !attachmentResult.isSuccess()) {
                ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 
                  'Could not save attachment.'));            
                return null;
            } else {
                // update the custom attachment record with some attachment info
                Attachment__c customAttachment = [select id, Vendor_Event_Detail__c from Attachment__c where id = :customAttachmentResult.getId()];
                customAttachment.name = this.fileName;
                customAttachment.Attachment__c = attachmentResult.getId();
                AttachmentId = customAttachmentResult.getId();           
                update customAttachment;
        } catch (Exception e) {
            return null;
        return new PageReference('/'+AttachmentId);
    public PageReference back() {
        return new PageReference('/'+Opportunity.Id);

ShashankShashank (Salesforce Developers) 
Instead of 

<apex:inputField id="VED" value="{!VED.Id}"/>

It should be something like:

<apex:inputField id="VED" value="{!VED.opportunity__c}"/>

provided opportuninty__c is the api name of the lookup field.