Activity Contact Lookup Override

June 26, 2014 Appirio

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 knew I could use javascript for this (I saw Jeff Douglas do it here.) Unfortunately this field on the task object can be a contact or a lead so it acts a little differently than a normal lookup. It is created via javascript after the page is loaded.  The function that creates the lookup is ActivityFunction.initWhoWhatFields.

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

5: ?

6: The name of the filtered object

7: ?

8: The id of the filtered object

9: ?

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.

The javascript to override the lookup dialog now works, but how do we get it onto every page?  There’s an old trick for getting google analytics on every page of your org using a sidebar component, so I figured I could use the same trick here.  We only override lookupPick2 when showing the dialog for our custom field, so it shouldn’t have any impact being on every page (except a little extra js).

You can see the annotated source here,  and a demonstration video here.

Here are the steps to implement this yourself:

1. Create a custom lookup field on the account object with a lookup filter


Override

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:

https://na9.salesforce.com/_ui/common/data/LookupPage?lknm=CF00NE0000005RWDW&lktp=003&lkent=001&lkfield=00NE0000005RWDW&dplp=%5B%22001E000000HIXyq%22%2Cnull%5D&lksrch=

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

5. Replace lookupStringStart with part one and lookupStringEnd in my javascript with your values.

6. Create a left side bar homepage component that is html. Select the show html box and paste in the javascript you have created.

(p.s. ensure that the User Interface setting “Show Custom Sidebar Components on All Pages” is enabled”)

7. Enjoy!

Previous Article
The Dashboard Health Check with Analytics API
The Dashboard Health Check with Analytics API

Contributed by : Asset Library Team Salesforce Dashboards can now be queried using Analytics API. Querying ...

Next Article
Password Protect PDF Docs in Salesforce using Heroku
Password Protect PDF Docs in Salesforce using Heroku

By Ankit Kumar Goyal and Neeraj Gupta One of our clients had a requirement to send monthly invoice statemen...