+ Start a Discussion

S-Controls and AJAX: Showing a pull-down list of related contacts

I had a bit of fun building an S-Control using AJAX that shows Contacts with similar email addresses.

I've posted the code on the wiki if anyone is interested. Feedback/suggestions welcome.

John Rotenstein
Sydney, Australia

The 'Show All' option doesn't seem to show in mine though... Could using Safari affect that?
Either way, thanks for the inspiration.

Hey, thanks for noticing that!

I've inserted a missing line at the end, so it works now.

Thanks to JohanLiljegren!
Even sweeter! Works like a charm, John!
Thanks for the quick update.


Hello John,

Great example code. You have really opened my eyes to some ideas that will bring greater functionality to my users. Thanks for posting it.

One question / idea: I would like to use this mini tool to help my sales reps see leads and contacts that have the same domain name but exclude the most common email domain names like gmail.com, yahoo.com, btinternet.com, hotmail.com etc . . . By doing this the mini tool would become more effective in helping reps seeing related leads or contacts within in companies like greenworks.com, hatchertech.com, toldosperez.es, etc . . .

Since we do not market directly to businesses and only market to leads that find our site this would help know form the very beginning that others in the same business are intersted in our products. This could prove very helpful in our B2C business although it would be even more helpful in a B2B business.

Could another scontrol snippet or admin field somewhere in SF be used to create a list of omitted email domian names? This would be called upon each time the code is loaded and would prevent our SF from showing 10,231 gmail.com emails lol!

How could I program the call out to the list of omitted domain names?

Thanks again!!


Good idea!

Here's a revised version of the code.

You'll need to create a new Custom Object called "Unwanted Domain". The create a record for each domain you want to exclude, with the domain stored in the Name field.

I just added one lookup, but also put the whole routine into a function so that I could exit if an Unwanted Domain is found.

<script src="/soap/ajax/12.0/connection.js" type="text/javascript"></script>

<select id="contacts" onchange="related_contact(this)">


function related_contact(contact)
  // If a user is selected, show them!
  top.location.href = "/" + contact.options[contact.selectedIndex].value;

function show_related()
// Extract the domain
var domain = "{!Contact.EmailAddr__c}".split("@")[1];

// ------------------ Start NEW BIT ---------------------

// Exit if this is an Unwanted Domain (use LIKE to make it case-insensitive)
var unwanted_domains = sforce.connection.query("select Name from Unwanted_Domain__c where Name like '" + domain + "'");
if (unwanted_domains.size > 0)

// ------------------ End NEW BIT -----------------------

// Find Contacts with same email domain
var related_contacts = sforce.connection.query("select Name, EmailAddr__c, Id from Contact where EmailAddr__c like '%" + domain + "' and Id != '{!Contact.Id}' ");

// Show them
var list = document.getElementById('contacts');
if (related_contacts.size == 0)
// Show them in a pull-down list
var option = document.createElement('option');
option.text = related_contacts.size + " Contact" + (related_contacts.size> 1 — "s" : "") + " with similar email address:";
try { list.add(option, null) } catch (ex) { list.add(option) } // For IE

// Look through each Contact and add the to the list
records = related_contacts.getArray("records");
for (var i=0; i < records.length; i++)
  option = document.createElement('option');
  option.value = records[i].Id;
  option.text = records[i].Name + " ("+ records[i].EmailAddr__c + ")";
  try { list.add(option, null) } catch (ex) { list.add(option) } // For IE

// Finally, add an option to go to the Search screen and look for the email domain
option = document.createElement('option');
option.value = "search/SearchResults–sen=003&sbstr=" + domain;
option.text = "Show All";

} // show_related

// Run it


Or, to exclude particular domains where the domains are hard-coded:

// Exit if this is an Unwanted Domain
var unwanted_domains = new Array ('msn.com', 'gmail.com');
if (unwanted_domains.indexOf(domain) != -1)
  list.style.display = 'none';


Hi John

I am not a developer but thought your drop down idea was a good one. Unfortunately am having problems loading it up, and wondered whether there was a step I am missing? The drop down box appears but there is no data populating it? I am using safari on mac

I copied the code went to DEVELOP set up a new HTML S COntrol and pasted code in. gave it the related_list name and then saved, in CONTACTS page set up added to a field section and saved...

Any elp you can provide wld be helpful....
Maybe sort of a stupid question, but anyway: Does the Contact you're viewing have the same email domain as any other Contacts in your system?


In the <body> tag of the S-control/html, it looks nice to put SFDC's background color in:

<body bgcolor=#f3f3ec>

Cosmetic, but may appease some users.
It can also be nice to name the S-Control something useful and to check the "Show label" checkbox on the S-control options popup (from the Contact page layout edit screen).
Great job with this--very useful and easily adapted to other functions.

Hi John,

I am also encountering the same issue as Mikecredent although I have IE 7.0.  There are related contacts with the same domain in the system, however the picklist pulldown contains no data.  Any ideas?

Thanks for your help.

Mikecredent & chrissy2007,

I have tested the code in IE7 on Windows (plus Safari and Firefox on Macintosh) and it works fine for me.

Here's the steps I took:
  • Created new S-Control
  • Pasted the code directly from the wiki page
  • Save
  • Went to the Contact Page Layout
  • Dragged the new S-Control onto the layout
  • Double-clicked, set height to 22 and ticked "Show label"
  • Went to a Contact that shared an e-mail address with another Contact (eg fred@gmail.com and jim@gmail.com)
  • The pull-down menu appeared
So, if that's what you did and it isn't working for you, it'll need some debugging.

User Profile
Here's one thing to check if you're not logged in as an Administrator:
  • Take a look at the User record of the login you're using to test the control
  • See what Profile that user has
  • Check the Profile and make sure that API Enabled it turned ON
The S-Control uses the API to call-back to Salesforce, so the user's profile requires this permission.

Another thing you can do is check if there are any errors being generated within the browser (look at the bottom-left of the window).

And finally... you could always switch to Firefox! :)

:smileyvery-happy: Outstanding!  Thank you so much.  It looks to be that my original HTML code was outdated/flawed, but when I recopied the code from the WIki, all works well in both Internet Explorer AND Mozilla.

This s-control will greatly improve processes since a count of similar domain "siblings" are now flagged directly in front of account managers.

Fantastic code, I can't say enough!