If I select a value on a lookup field on a particular record, is there a way that this selected lookup value gets hidden and does not display in another record? Lets say I have an object Object1__c which has a lookup field (Object2__c). Object2__c has 3 records "A, B and C". Now I create a new record on Object1__c and select value "A" on lookup field. Now when I create a 2nd record on Object1__c , the lookup field should only give me the option to select either B or C. not A. Is this poosible?

4 Answers 4


Lookup fields such as your Object1__c.Object2__c field can have a lookup filter defined. To exclude the already selected Object2__c instances in such a filter, you then need say a checkbox field Object2__c.InUse__c to use in the filter expression. A trigger can populate that checkbox field on insert/update/delete/undelete of Object1__c.

The trigger would look like this in its basic form:

trigger Object2InUse on Object1__c (after insert, after update, after delete, after undelete) {
    Set<Id> o2Ids = new Set<Id>();
    if (Trigger.isInsert || Trigger.isUpdate || Trigger.isUndelete) {
        for(Object1__c o1 : Trigger.new){
            if (o1.Object2__c != null) o2Ids.add(o1.Object2__c);
    if (Trigger.isUpdate || Trigger.isDelete) {
        for(Object1__c o1 : Trigger.old){
            if (o1.Object2__c != null) o2Ids.add(o1.Object2__c);
    if (o2ids.size() > 0) {
        Map<Id, Integer> m = new Map<Id, Integer>();
        for (Id id : o2ids) m.put(id, 0);
        for (AggregateResult ar : [
                select Object2__c i, count(id) c
                from Object1__c
                where Object2__c in :o2ids
                group by Object2__c
                ]) {
            m.put((Id) ar.get('i'), (Integer) ar.get('c'));
        Object2__c[] o2s = new Object2__c[] {};
        for (Id id : m.keySet()) {
            o2s.add(new Object2__c(Id = id, InUse__c = m.get(id) > 0));
        update o2s;

and the filter criteria on the Object1__c.Object2__c field would be:

Object 2: In Use EQUALS False

The answer to your question depends entirely on how you define the look-up relationship. For example, is it a formula? Think of the case where a look-up is to a User. With the exception of where someone is the Owner or the WhoID, It doesn't matter to which User. It's often arbitrary and the same one could be selected many times over.

One could make a similar analogy to a look-up for Contacts when a relationship isn't specified. Unless you define a relationship through a formula in the look-up involving the recordID's, I don't see how it would be possible to do what you ask.

  • Thanks for your inputs. But how to create it. Dec 2, 2014 at 18:54
  • It almost seems as though you need to create some kind of junction object that sits in-between the two to act as a "dynamic filter", where once a record has been used, it becomes no longer available for additional look-ups. I'll have to give it some further thought and think about how that could be implemented.
    – crmprogdev
    Dec 2, 2014 at 19:21

To control what will be displayed in the lookup popup using the standard functionality, you may have a look at Lookup filters:


But I guess you can't do it with that.

Validation Rules are not option, because the do not help to filter the possible options. So it looks like you have to think about a Visualforce page.

  • Thanks for your inputs. VF page is not an option as its just about creating records through Standard Salesforce detail page. Dec 2, 2014 at 18:56
  • Beside Lookup Filters there is no other option to control the available objects in the lookup popup. At least as far as I know. On standard pages you could probably try very hacky things by injecting a custom javascript and bypassing the standard lookup process entirely by something custom - but this would end up in other problems...
    – Uwe Heim
    Dec 2, 2014 at 19:06

You could do this with a Visualforce page and populate the lookup value with an <apex:selectList/> where the <apex:Options/> (List<SelectOption>) come from a SOQL query on Object2__c. You could keep track of what records from Object2__c you've already added in a Set<Id> or List<Id> and then simply update the List<SelectOption> SOQL to not include what you've already added.

As previously mentioned, filters or validation rules wouldn't work for this scenario.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .