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
DemintDemint 

How to create a dependent lookup

Hi

 

I am fairly new to Force.com development and have hit an issue trying to build some relationships on a custom object.

 

I have an 'Assessment' custom object with a custom lookup field to a 'Claim' custom object.  I want to build a further custom lookup field on Assessment: 'Claim Contact' that looks up from another custom object, but this object is the detail in a master-detail relationship with the Claim object. 

 

I want to know how to constrain the 'Claim Contact' lookup so that it only shows values that are related to the Claim that has already been looked up on to the Assessment.

 

i.e. if the Assessment is linked to Claim A, which has child Contacts Jack and Jill, then these should be the only contacts I am able to pick using the 'Claim Contact' lookup on the Assessment.  If the assessment is not linked to any Claim then I need the lookup to return no results.

 

Is there a configuration solution to this or if not does anyone know what the best approach is to do this?

 

Many thanks in advance!

Best Answer chosen by Admin (Salesforce Developers) 
SteveBowerSteveBower

Lookups hav no filtering capability at the moment.  So, no, you can't restrict the Assesment lookup to only the Contacts related to the Claim.

 

Of course if you want to write Apex code you could implement an override for the Edit page which would use the standard controller for all aspects of the page except for the lookup.  For the lookup filed the controller could create a picklist of the Contact that are appropriate for the claim and let the user select just from those.  When the user saves the record, the chosen contact would be copied to the lookup field.

 

Alternatively, you could create a validation trigger which fires on a "before update, before insert" and checks to see if the selected Contact is one of the valid ones.  This isn't stopping the user up front from choosing an "illegal" contact, just preventing the record from being saved with that contact.  The user would be returned to the edit screen and the error message displayed could provide the list of valid contacts.

 

So, there are ways around it, if you really need that functionality, but it requires writing some code.

 

Best, Steve.

All Answers

SteveBowerSteveBower

Lookups hav no filtering capability at the moment.  So, no, you can't restrict the Assesment lookup to only the Contacts related to the Claim.

 

Of course if you want to write Apex code you could implement an override for the Edit page which would use the standard controller for all aspects of the page except for the lookup.  For the lookup filed the controller could create a picklist of the Contact that are appropriate for the claim and let the user select just from those.  When the user saves the record, the chosen contact would be copied to the lookup field.

 

Alternatively, you could create a validation trigger which fires on a "before update, before insert" and checks to see if the selected Contact is one of the valid ones.  This isn't stopping the user up front from choosing an "illegal" contact, just preventing the record from being saved with that contact.  The user would be returned to the edit screen and the error message displayed could provide the list of valid contacts.

 

So, there are ways around it, if you really need that functionality, but it requires writing some code.

 

Best, Steve.

This was selected as the best answer
rockchick322004rockchick322004

Filters on lookups is a feature that my team is currently working on.  There is a popular IdeaExchange post for it here: http://ideas.salesforce.com/article/show/25164/Filter_on_Lookup

 

Steve's suggestions will work if you want to write code.  A non-code approach is to:

1) Create a Validation Rule on 'Assessment' to block the save of an invalid 'Claim Contact' value

2) Enable "Enhanced Lookups" for the 'Claim Contact' object

3) Add the 'Claim' field to the 'Claim Contact' Lookup Filter Fields search layout so that end users can populate that filter field themselves to filter their search results to contacts that are related to the claim.

 

This puts the burden of filtering on the end user, but it does help them search and ensures data quality.  I hope to have a much better solution soon.

DemintDemint

Thanks very much Steve and Mscotton

 

I need to prevent the user from picking an unrelated record from the outset so I am going to try out the Apex solution - I'll let you know how I get on

 

DemintDemint

I have managed to get this working now using Apex as per your suggestions - THANKS!! :smileyvery-happy:

 

I now am working on a separate issue - I will raise a separate thread if I can't fix it but in case you have come across...

 

I want to dynamically render some controls in my VF page based on the value in a lookup field (i.e. show or hide them based on this). 

 

The onchange event does not seem to be firing (I have seen some other threads that seems to say that this will not work on Lookup fields) - I can get the dynamic UI working if I use a simple picklist field but it needs to work from a lookup. 

 

SteveBowerSteveBower

Each Field Tag, Panel Tag, Button, etc. has a "rendered=Boolean" parameter.   Create a "getShouldIRender()" function in your customer controller to evaluate to True or False based on your dynamic determination.

 

Then you can use

 

<apex:outputPanel rendered="{!shouldIRender}"> and it will evaluate your function to make the decision.

 

----------------

 

Can you just use the picklist, and then, when saving, put the selected value into the Lookup field yourself?  Otherwise, you need to post code.

 

Best, Steve.

kdmrkdmr

Hi,

 

I am also trying to do the same, that is to create a dependent lookup. The problem I face is that the value from the controlling field is not being passed to the query to get the depending lookup values.

 

It would be great if you could share your trials on the dependent lookup, or give some pointers as to how to use the value from the controlling lookup.

 

thanks

KD

GhilliGhilli

Hi,

 

Could you please tell me how you solved the filtered lookup field issue. Because me also have the same requirement.

 

If possible could you please share the apex code so that i can understand it easily.

 

Thanks.

 

RCJesseRCJesse

Demint,

 

Can you expand on how you got this working using Apex. I can't see doing it without VisualForce.

FatimaFatima

Hi dear,

have a look at www.sarwari.org

Easy lookup filter between lookup relationships.

No matter if standard or custom fields.

cheers

rtr.sudhir_kumar@hotmail.comrtr.sudhir_kumar@hotmail.com

Hi Demint

 

Could you share how you reached to this solution.  Actually I am also facing the same challenge.

Looking forward to getting the reply at the earliest.

thanks. :)

rtr.sudhir_kumar@hotmail.comrtr.sudhir_kumar@hotmail.com

Demint wrote:

I have managed to get this working now using Apex as per your suggestions - THANKS!! :smileyvery-happy:

 

I now am working on a separate issue - I will raise a separate thread if I can't fix it but in case you have come across...

 

I want to dynamically render some controls in my VF page based on the value in a lookup field (i.e. show or hide them based on this). 

 

The onchange event does not seem to be firing (I have seen some other threads that seems to say that this will not work on Lookup fields) - I can get the dynamic UI working if I use a simple picklist field but it needs to work from a lookup. 

 



Hi Demint

 

Could you share how you reached to this solution.  Actually I am also facing the same challenge.

Looking forward to getting the reply at the earliest.

thanks. :)