function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Scott.MScott.M 

Visual Force Lookup Field

Hi,

This is a simple question, is there a visual force element for lookup fields. I'm looking at the Visual Force component reference but can't find one, maybe I'm blind. Anyone know if I'm looking for something that doesn't exist?

Thanks,

Scott

Best Answer chosen by Admin (Salesforce Developers) 
TehNrdTehNrd
You shouldn't need to create a new field on the task object if in your system somewhere there is already a lookup field to Campaign. I'm not too familiar with the Campaign relationships so I can't think of a lookup field that points to Campaign (maybe CampaignID on CampaignMember) but if you can find one you can use this as a placeholder for you CampaignID. Then since you are using a standard controller you will need to move your Save logic to a Save method in the extension. Here you can apply the CampaignID to the WhatId.

Code:
Extension:

Object idHolder = new Object();
//On this object there is a lookup field to the Campaign object called Campaign__c
//In the page we can do something like this

<apex:inputField value="{!idHolder.Campaign__c}">

//Then in your save method
task.WhatID = idHolder.Campaign__c;

 



Message Edited by TehNrd on 07-16-2008 10:41 AM

All Answers

Scott.MScott.M
I figured out how it works, the <apex:inputField>  always corresponds to a field in an sObject so it displays an input element based on what type the fields is. If it's a lookup it displays an input with the the lookup icon. That's great but doesn't work for what I'm trying to do.

In my situation I have a custom object (CA) that has a related list of an other custom object (CB). When a new CA is created i want to be able to add N of CB to CA in the create interface without having to save the object and add the CB objects from the related list.

Basically what I'm trying to do is have a list of lookup fields with an add button that adds a new field to the list. Very much like adding attachments to an email in Gmail. I'm wondering if there's a way of having a generic lookup field that's not tied to a specific field in the object being created. 
TehNrdTehNrd

Scott.M wrote:
I'm wondering if there's a way of having a generic lookup field that's not tied to a specific field in the object being created. 



Yes. You will need to do this with either a custom controller or an extension. You can create an object in the controller with a lookup field to be a place holder for an Id.

Code:
Controller/Extension:

Opportunity userID = new Opportunity();

Even thought I created an entire object all I really care about is the Owner Id
field on the opportunity as this can be used as an ID "variable" in other places of the controller:

User userRecord = [select Id, Name from User where ID = :userID.OwnerID];


Page:
<apex:inputField value="{!userID.OwnerID}" id="user1"/>



Message Edited by TehNrd on 05-20-2008 01:18 PM
samdsamd
Hi

I'm trying to do the same thing with a lookup to a campaign on a Visual Force page.  However, I can't seem to get the lookup field to appear. I have tried the two scenarios shown below:

Scenario 1

Code:

Extension:

//  Create uninitialised member to store Id of Campaign selected by user
Campaign RelatedToCampaign;

Page:

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

Screenshot shown below - CampaignId is read only (even though inputField apex tag is used)



Scenario 2

Code:

Extension:

//  Create member to store Id of Campaign selected by user and set to arbitrary campaign
Campaign RelatedToCampaign =[select Id, Name from Campaign limit 1][0];

Page:

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

Screenshot shown below - CampaignId is read only (even though inputField apex tag is used)



Anything I'm doing wrong?

Many thanks in advance

Sam
TehNrdTehNrd
You are using the ID field. ID fields are always read only. What you need to use is a field that is of the type Lookup and is related to Campaign.

-Jason


Message Edited by TehNrd on 07-16-2008 10:16 AM
samdsamd
Many thanks for the quick reply.  It sounds like you are suggesting that I create a lookup field on the task, but I don't want to create a new field on the Task object.  I would like to create a lookup to allow the user to select a campaign and then use this Id to set the WhatId of the task (to be done in a customised save() function in the controller extension).  I don't want to create a straight input field for the WhatId (i.e. <apex:inputField value="{!Task.WhatId}" />) because the default lookup that appears can then be used to select other object types (eg. Account etc.) and I want to restrict users to only being able to select a campaign. 

Regards
Sam
TehNrdTehNrd
You shouldn't need to create a new field on the task object if in your system somewhere there is already a lookup field to Campaign. I'm not too familiar with the Campaign relationships so I can't think of a lookup field that points to Campaign (maybe CampaignID on CampaignMember) but if you can find one you can use this as a placeholder for you CampaignID. Then since you are using a standard controller you will need to move your Save logic to a Save method in the extension. Here you can apply the CampaignID to the WhatId.

Code:
Extension:

Object idHolder = new Object();
//On this object there is a lookup field to the Campaign object called Campaign__c
//In the page we can do something like this

<apex:inputField value="{!idHolder.Campaign__c}">

//Then in your save method
task.WhatID = idHolder.Campaign__c;

 



Message Edited by TehNrd on 07-16-2008 10:41 AM
This was selected as the best answer
samdsamd
Brilliant!  It works like a charm.

Many thanks!
Sam
TehNrdTehNrd
Awesome.

One thing to watch out for is that depending on the user field level security still applies to the Lookup field.