You need to sign in to do that
Don't have an account?
Will Jones 18
How to add lookup field on Visualforce page?
I have a visualforce page that is used to pull through attachments on the opportunity page. It is based off of the work Jeff Douglas did:
http://blog.jeffdouglas.com/2014/05/30/how-to-customize-salesforce-attachments/
The visualforce page and controller works fine but now I am having trouble adding a lookup field.
The lookup field is based on a related list on the opportunity page. How do I add this field to the controller and page to make sure that a user can do a lookup and choose a "Vendor Event Details" that is associated with the current opportunity?
Controller:
Page:
http://blog.jeffdouglas.com/2014/05/30/how-to-customize-salesforce-attachments/
The visualforce page and controller works fine but now I am having trouble adding a lookup field.
The lookup field is based on a related list on the opportunity page. How do I add this field to the controller and page to make sure that a user can do a lookup and choose a "Vendor Event Details" that is associated with the current opportunity?
Controller:
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) { ApexPages.AddMessages(e); return null; } return new PageReference('/'+AttachmentId); } public PageReference back() { return new PageReference('/'+Opportunity.Id); } }
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: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.Id}"/> </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>
Can you replace line 23 with this line : If this answers your question then hit Like and mark it as solution!
That solved one problem. I now see the lookup field:
However when I do the lookup it does not return any of the related Vendor Event Details. It is always empty. Any suggestions on how to ensure that I am able to search on all of the Vendor Event Details related to the Opportunity?
‹
Have you Vendor Event Detail records or have you any lookup filters?
I changed this line of code from:
to:
Why is my lookup search not working at all?