
Paste this code into a new snippet SControl called Format_Utilities (the name is important).
<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
function formatCurrency(num, includeCents) {
num = num.toString().replace(/\$|\,/g,'');
if(isNaN(num)) {
num = "0";
}
sign = (num == (num = Math.abs(num)));
num = Math.floor(num*100+0.50000000001);
cents = num%100;
num = Math.floor(num/100).toString();
if(cents<10) {
cents = "0" + cents;
}
for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++) {
num = num.substring(0,num.length-(4*i+3))+','+ num.substring(num.length-(4*i+3));
}
if (includeCents == true) {
return (((sign)?'':'-') + '$' + num + '.' + cents);
} else {
return (((sign)?'':'-') + '$' + num);
}
}
// End -->
</script>
Paste this code into a new SControl:
<html>
<head>
<script type="text/javascript" src="/js/functions.js"></script>
<script src="/soap/ajax/8.0/connection.js"></script>
<script type="text/javascript" src="http://api.maps.yahoo.com/ajaxymap?v=3.0&appid=YahooDemo"></script>
<script src="/soap/ajax/8.0/apex.js" type="text/javascript"></script>
{!INCLUDE($SControl.Format_Utilities)}
<script>
var contactId = "{!Contact.Id}";
var contactMailingAddress = "{!Contact.MailingStreet},{!Contact.MailingCity},{!Contact.MailingState}";
// Set up map type in the constructor
var map = null;
var marker = null;
function setup() {
map = new YMap(document.getElementById('mapContainer'));
// Display the map centered on a lattitude and longitude
map.drawZoomAndCenter(contactMailingAddress, 4);
// Create a marker positioned at a lat/lon
marker = new YMarker(contactMailingAddress);
// Add auto expand
var _txt = '<div><b>{!Contact.Name}</b></div>';
marker.addAutoExpand(_txt);
// Call onSmartWinEvent when the user clicks on the marker
YEvent.Capture(marker, EventsList.MouseClick, onSmartWinEvent);
// Display the marker
map.addOverlay(marker);
}
// Capture the user mouse-click and expand the SmartWindow
function onSmartWinEvent() {
marker.openSmartWindow(getSmartWindowContent());
}
function getSmartWindowContent() {
//Put the opportunities into the smartWindow
//Get the opportunities for this contacts account
var qr = sforce.connection.query("Select (Select Id, Name, Amount From Opportunities Order By Amount Desc Limit 5) From Account Where Id = '{!Account.Id}'");
var html = "";
if (qr.size == 0) {
//Not opptys found
html = "<div><b>This Account has<br>no opportunities</b></div>";
} else {
html += "<div class='optyStyle'>"
html += "<table width='200px' cellpadding='0' cellspacing='0' border='0'>";
var qrOpportunity = qr.getArray("records")[0].Opportunities;
var opportunities = qrOpportunity.getArray("records");
for (var i=0;i<opportunities.length;i++) {
//Add a row for each oppty
html += "<tr>";
var style = "optyStyle" + ((i%2) ? " odd" : " even");
html += "<td class='" + style + "'>";
html += "<a href='/" + opportunities[i].Id + "' target='_top'>";
html += opportunities[i].Name;
html += "</a>";
html += "</td>";
var amount = opportunities[i].Amount ? formatCurrency(opportunities[i].Amount, false) : "$0";
html += "<td class='" + style + "'>" + amount + "</td>";
html += "</tr>";
}
//Add the link to execute Apex Code
html += "<tr><td colspan='2'> </td></tr>";
html += "<tr><td class='optyStyle' colspan='2'>";
html += "<a href='javascript: findNearBy()'>Opportunities Nearby</a>";
html += "</td></tr>";
html += "</table>";
html += "</div>";
}
return html;
}
function findNearBy() {
var cId = "{!Contact.Id}";
var nearbyContacts = sforce.apex.execute(
"NearbyUtil",
"findNearBy",
{currentContactId: cId },
mapNearByContacts);
}
function mapNearByContacts(nearbyContacts) {
for (var i=0;i<nearbyContacts.length;i++) {
var nearbyContact = nearbyContacts[i];
nearbyContact.address = nearbyContact.MailingStreet + "," + nearbyContact.MailingCity + "," + nearbyContact.MailingState;
var newMarker = new YMarker(nearbyContact.address);
// Add a label to the marker
newMarker.addLabel("N");
// Add auto expand
var _txt = '<div><b>' + nearbyContact.FirstName + " " + nearbyContact.LastName + '</b></div>';
newMarker.addAutoExpand(_txt);
YEvent.Capture(newMarker, EventsList.MouseClick,
function() {
this.openSmartWindow("<div><a class='optyStyle' href='/" + nearbyContact.Id + "' target='_top'>Go to Details</a></div>");
});
// Display the marker
map.addOverlay(newMarker);
marker.closeSmartWindow();
}
}
</script>
<style type="text/css">
#mapContainer
{
height: 300px;
width: 600px;
}
.optyStyle, .optyStyle {
font-size: 12px;
font-family: arial;
}
.even {
background-color: #eedddd;
}
</style>
</head>
<body onload="setup();">
<div id="mapContainer"></div>
</body>
</html>
Paste this code into a new Apex Code Package named NearbyUtil (the name is important)
package NearbyUtil{
//Look for leads in the contact city whose opportunity probability is > 50%,
webService Contact[] findNearBy(String currentContactId) {
Contact[] nearContacts = new Contact[] {};
Contact currentContact = [Select Id, FirstName, LastName,
AccountId, MailingCity, MailingStreet,
MailingState From Contact Where Id = :currentContactId];
//Look for contacts that are in the same city
for (Contact contact : [Select Id, FirstName, LastName, AccountId, MailingCity,
MailingStreet, MailingState From Contact
Where MailingCity = :currentContact.MailingCity and
AccountId != :currentContact.AccountId]) {
//Find opportunities for this Account that are over 50% probability
for (Opportunity opp : [Select Id, Name, Amount From Opportunity
Where AccountId = :contact.AccountId and Probability > 50]) {
nearContacts.add(contact);
}
}
if (nearContacts.size() > 0) {
return nearContacts;
} else {
return null;
}
}
}