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.