You need to sign in to do that
Don't have an account?
Contact lookup on VF page for custom object
Hello.
I have a custom object called Expense that has a lookup field to the Contact object. I'm attempting to make a custom Visualforce edit page for the object, and I'd like to have the Contact field show up the same way it does on standard edit pages (ie with the autocomplete and the icon to click for a lookup).
I can't just use the standard controller for the Expense object, due to other logic I need in the controller, so I've created an extension. But, I cannot figure out how to get the contact field to show up. I had found some other references on the b boards to similar issues and have tried them (see the note about "phantom" record in the controller below), but have had no success.
My shortened page code is below:
<apex:page standardController="Expense__c" extensions="CtrlExpenseAddNew" > <apex:form > <apex:pageBlock title="Expense Edit" id="thePageBlock" mode="edit"> <apex:pageMessages /> <apex:pageBlockButtons > <apex:commandButton value="Save" action="{!saveExpense}"/> </apex:pageBlockButtons> <apex:pageBlockSection title="Expense Details" columns="2"> <apex:pageBlockSectionItem > <apex:outputLabel for="expCategory" value="Category"/> <apex:selectList id="expCategory" value="{!expCategory}" size="1"> <apex:selectOptions value="{!expCategoryOptions}"/> </apex:selectList> </apex:pageBlockSectionItem> <apex:pageBlockSectionItem > <apex:outputLabel for="expContact" value="Contact"/> <apex:inputField id="expContact" value="{!expContact.Id}"/> </apex:pageBlockSectionItem> <apex:pageBlockSectionItem > <apex:outputLabel for="expDescription" value="Description"/> <apex:inputText value="{!expDescription}"/> </apex:pageBlockSectionItem> </apex:pageBlockSection> </apex:pageBlock> </apex:form> </apex:page>
My shortened controller code is below (unnecessary stuff removed for clarity):
public with sharing class CtrlExpenseAddNew { public String expType {get;set;} public String expDate {get;set;} public String expDescription {get;set;} public Decimal expMileage {get;set;} public String expCategory {get;set;} public String expAmex {get;set;} public Decimal expAmount{get;set;} public String expLocation {get;set;} public Contact expContact {get;set;} // populate the list of expense types public List<SelectOption> getExpTypeOptions() { // Obtain the picklist values for the Type__c field on the expense object Schema.DescribeFieldResult fieldResult = Expense__c.Type__c.getDescribe(); List<Schema.PicklistEntry> picklistEntries = fieldResult.getPicklistValues(); List<SelectOption> options = new List<SelectOption>(); options.add(new SelectOption('','')); for (Schema.PicklistEntry entry : picklistEntries) { options.add(new SelectOption(entry.getLabel(), entry.getValue())); } return options; } // populate the list of expense categories public List<SelectOption> getExpCategoryOptions() { // Obtain the picklist values for the Expense_Type__c field on the expense object Schema.DescribeFieldResult fieldResult = Expense__c.Expense_Type__c.getDescribe(); List<Schema.PicklistEntry> picklistEntries = fieldResult.getPicklistValues(); List<SelectOption> options = new List<SelectOption>(); options.add(new SelectOption('','')); for (Schema.PicklistEntry entry : picklistEntries) { options.add(new SelectOption(entry.getLabel(), entry.getValue())); } return options; } public CtrlExpenseAddNew(ApexPages.StandardController stdController) { // Read parameter values (if being created from an event or event/expense calendar) String paramDate = ApexPages.currentPage().getParameters().get('date'); if (paramDate <> null) {expDate = paramDate;} String paramDesc = ApexPages.currentPage().getParameters().get('desc'); if (paramDesc <> null) {expDescription = String.escapeSingleQuotes(paramDesc);} String paramType = ApexPages.currentPage().getParameters().get('type'); if (paramType <> null) {expType = String.escapeSingleQuotes(paramType);} String paramCat = ApexPages.currentPage().getParameters().get('cat'); if (paramCat <> null) {expCategory = String.escapeSingleQuotes(paramCat);} // Need to create "phantom" contact and lead objects for lookups to function properly // ref: http://boards.developerforce.com/t5/Visualforce-Development/lookup-inputField-causing-errors-w-standard-controller-extension/m-p/158894 expContact = new Contact(); } public pageReference saveExpense() { // Stuff removed } }
Any assistance on this would be greatly appreciated. Thank you.
-Greg
What is the name of the Contact lookup field?
Hello.
The contact lookup field is simply called "Contact" so the API name is "Contact__c".
-Greg
Any insight into how this can be accomplished? Thanks.
-Greg
Based on the lack of response, is it not possible to implement what I am trying to achieve?
-Greg
Was out of country, so couldn't reply. I am not sure you found a work around already.
Why don't you use <apex:inputField value="{!Expense__c.Contact__c}" /> for that contact lookup?
And in constructor, just mention Expense__c exp = (Expense__c) con.getRecord(); , like the following:
In save method you can make use of 'exp' and insert it. Doe this help you?