<!-- Set the session ID for the AJAX toolkit to use automatically -->
<script type="text/javascript">
var __sfdcSessionId = '{!GETSESSIONID()}';
</script>
<!-- Include the AJAX Toolkit -->
<script src="/soap/ajax/25.0/connection.js" type="text/javascript" javascript"></>script>
<script src="/soap/ajax/25.0/apex.js" type="text/javascript" />></script>
<script type="text/javascript">
// Execute the Apex web service.
sforce.apex.execute( '<namespace>/<class>',
'<method name>',
{ownerId: '<ownerId>'},
{onSuccess: displayCount, onFailure: errorHandler, timeout: 300000 //timeout in ms}
);
function displayCount( result )
{
// Add your code here to update/display your count.
}
function errorHandler( error )
{
// Handle errors.
}
</script>
Information on the webService
annotation can be found here: http://www.salesforce.com/us/developer/docs/apexcode/index_Left.htm#StartTopic=Content/apex_web_services_methods_considerations.htm#kanchor587
Here's a simpler method for querying the count directly via the AJAX Toolkit without relying on a webService
method.
<apex:page >
<!-- Set the session ID for the AJAX toolkit to use automatically -->
<script type="text/javascript">
var __sfdcSessionId = '{!GETSESSIONID()}';
</script>
<!-- Include the AJAX Toolkit -->
<script src="/soap/ajax/25.0/connection.js" type="text/javascript"></script>
<script src="/soap/ajax/25.0/apex.js" type="text/javascript"></script>
<!-- jQuery -->
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
<script type="text/javascript">
$().ready( function() {
queryCount();
});
function queryCount()
{
// Execute the Apex web service.
sforce.connection.query(
"SELECT Count() FROM Lead",
{
onSuccess: handleCount,
onFailure: queryFailed,
timeout: 300000 // Set timeout to 5 minutes
});
}
function handleCount( result )
{
// Add your code here to update/display your count.
$( '#output' ).append( result.size );
}
function queryFailed( error )
{
// Handle errors.
$( '#output' ).append( error );
}
</script>
<div id="output">
</div>
</apex:page>