By Bryan Leboff
I recently asked a good friend of mine what was his number one issue with Salesforce and without hesitation he responded with the fact that you cannot filter the contacts on a task that you were creating by the account you were creating it on. “Nonsense!”, I said, “Lookup filters should work here!”
Well as it turns out, he was correct, and of course there is an idea out on the Idea exchange to implement this feature (and its probably a good idea to vote it up).
So I decided to see if I could solve his problem using some Salesforce trickery.
There were a couple restraints I gave myself. For one, I wanted to use the out of the box task edit page, and lookup dialog, as well as to avoid jQuery. This would allow it to be implemented without any dependencies in any org, and maintain the point and click customization of the out of the box page layouts.
Now it was time to design the solution. The lookup dialog we want to see on the task page is exactly what we would expect to see if we had a contact lookup on an account with a lookup filter that restrained the contacts to the current account. I figured I could create this field on the account object and ‘borrow’ the dialog it creates for the lookup on the task page.
After creating the lookup field on account, I needed to figure out how to override the dialog that opens when the user clicks the lookup icon on the task layout.
I was able to hook into this initialization and ensure I overwrote the action of the button after it had been initialized. So instead of opening the lookup dialog as it would normally open, I can make it open the contact lookup dialog from our account page. This override function checks that the “related to” object is an account and contains a value before redirecting to our custom field dialog, so we know which account to filter on.
So now we have the dialog we want to show, and a way to capture the click and redirect the user to our custom field lookup dialog from account. After the user selects an item from the lookup search, we have to make sure the page is populated with the appropriate values. Again we can hook into an existing function in salesforce called lookupPick2, that populates the fields back on the original page.
This function takes nine arguments and I was able to decipher them as follows:
0: The page type
1: The hidden id field in the form
2: The text field in the form
3: The html id of the text field
4: The html name of the text field
6: The name of the filtered object
8: The id of the filtered object
We check that the field that the user is attempting to fill at index two is our custom field, and if it is, we switch out the values for the custom field on the account page and the field on the task page. Otherwise we let it pass through to the original lookupPick2 function.
Here are the steps to implement this yourself:
1. Create a custom lookup field on the account object with a lookup filter
2. Copy the URL that this lookup uses in its lookup dialog
3. Split this url into two parts separated by the accountid value
So for this URL:
part one is: /_ui/common/data/LookupPage?lknm=CF00NE0000005RWDW&lktp=003&lkent=001&lkfield=00NE0000005RWDW&dplp=%5B%22
part two is: %22%2Cnull%5D&lksrch=
We’ll use these in our JS later.
4. Copy the field id we want (lknm value) or CF00NE0000005RWDW in my case
(p.s. ensure that the User Interface setting “Show Custom Sidebar Components on All Pages” is enabled”)