6

I have a Custom Lookup field for Account and Contact on visualforce page

After Selecting Account from 1st lookup field when i click the 2nd lookup field for Selecting Contact it should display those contacts only related to the Account Selected .

If No account is being selected it should display all the Contacts ..

I have added lookup filter on contact to show only contacts that are related to contact ..but it is not working.

Contact: Account ID equals Reserve:AccountId

I have enabled the lookup filter as well

1 Answer 1

5

you might need a DML update (i.e. Save the record) between selecting the 1st and 2nd lookups (since the 2nd lookup filter depends on the 1st lookup's value). You have an issue because you are trying to do both at the same time, and the 2nd Lookup Filter doesn't know the 1st Lookup's Value because you haven't saved the record yet.


@Andy Hitchings on 2017-02-06

is there another way around this, do you see? JavaScript could be used here I suppose?

Please disregard the previous answer.

Yes, there are many ways. Idea is to decide how you will write your query for Contacts once you know whether or not an Account is selected. You will need code to decide how to look up.

Apex

public Id accountId {get;set;}

public Contact[] getContacts() {
    return (Contact[]) Database.query(String.join(new String[]{
        'SELECT Id, AccountId, FirstName, LastName',
        'FROM Contact',
        (accountId == null ? '' : 'WHERE AccountId = :accountId')
    }, ' '));
}

When the Account Lookup changes, do an apex:actionSupport, etc.

@RemoteAction

Same as above, but get Contacts via @RemoteAction in JavaScript. You can also get Accounts via Remote Action and construct the <select> manually in JavaScript.

@RemoteAction
public static Contact[] getContacts(Id accountId) {
    return (Contact[]) Database.query(String.join(new String[]{
        'SELECT Id, AccountId, FirstName, LastName',
        'FROM Contact',
        (accountId == null ? '' : 'WHERE AccountId = :accountId')
    }, ' '));
}

apex:remoteObjects

Or you can use Remote Objects: https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_compref_remoteObjects.htm

Other Ideas

Or you can do the whole thing in JavaScript. Have Controller Methods to return all your data in JSON, and parse the JSON (after escaping single quotes), and construct your <select>s in JavaScript.

You could query all Contacts and then decide which to show / enable / disable in Contacts' <select> based on Accounts' selected choice. Watch out for querying too many records and other limitations.

5
  • 1
    It sounds so obvious when you say it that way, but when creating records through the standard page layouts, the look-ups work fine without having to save in between.
    – martin
    Commented Jun 27, 2016 at 6:30
  • I agree - this is frustrating. Commented Feb 6, 2017 at 15:12
  • @Scott Pelak - is there another way around this, do you see? JavaScript could be used here I suppose? Commented Feb 6, 2017 at 15:24
  • @AndyHitchings updating answer... Commented Feb 6, 2017 at 15:33
  • @AndyHitchings ...answer updated. Commented Feb 6, 2017 at 15:51

You must log in to answer this question.

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