+ Start a Discussion
Will Jones 18Will Jones 18 

Add lookup field to controller extension

How do I go about adding a a lookup field to a controller extension I have behind a visualforce page? Right now my lookup field is unable to do any searches. I can enter an actual record into the lookup field and it will save, BUT I cannot utilize the lookup search glass at all.
Shashikant SharmaShashikant Sharma
You only need to create an instance of the object that has the lookup field and use instace.CheckBoxFieldAPIName__c .
Could you share your code.
 
Will Jones 18Will Jones 18
Hi Shashikant,

Whenever I do a search using the lookup no records appear. I must type in the exact record name to save the lookup on the visualforce page. Maybe you can see where I am going wrong below:

Here is my controller code (it is an extension):
 
public class UploadAttachmentController {
    
    public String contractType {get;set;}
    public String description {get;set;}
    String AttachmentId;
    public Attachment__c VED {get;set;}
    public 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;
  		SYSTEM.DEBUG('VED Oppty ID is ' + VED.Opportunity__c);
            
    } 
    
    // 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.Vendor_Event_Detail__c;
        //SYSTEM.DEBUG('VED Oppty ID is ' + obj.Vendor_Event_Detail__r.Opportunity__c);
        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) {
            ApexPages.AddMessages(e);
            return null;
        }
        
        return new PageReference('/'+AttachmentId);
    }
    
    public PageReference back() {
        return new PageReference('/'+Opportunity.Id);
    }     
 
}



Here is my page code:
 
<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="Cancel"/>
 </apex:pageBlockButtons>
 <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:pageBlockSectionItem >
      <apex:outputLabel value="Vendor Event Detail" for="VED"/>
      <apex:inputField id="VED" value="{!VED.Vendor_Event_Detail__c}"/>
    </apex:pageBlockSectionItem>
      
    <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:selectList>
    </apex:pageBlockSectionItem>
    
    <apex:pageBlockSectionItem >
      <apex:outputLabel value="Description" for="description"/> 
      <apex:inputTextarea id="description" value="{!description}" rows="4" cols="50"/>
    </apex:pageBlockSectionItem>
    
    <apex:pageBlockSectionItem >
      <apex:outputLabel value="" for="uploadBtn"/> 
      <apex:commandButton id="uploadBtn" value="Attach File" action="{!processUpload}" />
    </apex:pageBlockSectionItem>    
    
  </apex:pageBlockSection>
 
 </apex:pageBlock>
 
 
 </apex:form>
 
</apex:page>

 
Will Jones 18Will Jones 18
Also as you see in my code, I created an instance of the object (Attachment__c) but I still cannot utilize the lookup field correctly. It yields no search results even when I know for a fact that records do exist.
Will Jones 18Will Jones 18
By the way, "obj.Vendor_Event_Detail__c" is the lookup field that isn't working.
Will Jones 18Will Jones 18
I now see that my lookup DOES work if I remove the lookup filter I created for the lookup field. How do I get to work with the filter enabled?