Show no. of open or closed activities (history) on the CRM form.
May 18th, 2009
Hello all,
Have you ever wanted to see the total no. of outstanding/completed activities on a Case, Account, Opportunity, Contact etc..?
Well now you can, simply add the following JavaScript code to the onLoad event of the entity, it will query the webservice and it will give you the exact count of open/closed activities.
Update: 9th March 2010 - I have updated the code below and tested.
// this code is to get the number of activities and historical activities.
var buXml = GetRegardingActivity();
if(buXml != null)
{
var buNodes = buXml.selectNodes("//BusinessEntity/q1:statecode"); // CRM 4.0
var iActivity = 0;
var iHistory = 0;
if(buNodes != null )
{
/*get values*/
for( i = 0; i < buNodes.length; i++)
{
switch(buNodes[i].text)
{
case "Open" : iActivity++; break;
case "Scheduled" : iActivity++; break;
case "Completed" : iHistory++; break;
case "Canceled" : iHistory++; break;
}
}
if(document.getElementById('navActivities') != null)
{
document.getElementById('navActivities').getElementsByTagName('NOBR')[0].innerText = document.getElementById('navActivities').getElementsByTagName('NOBR')[0].innerText + " (" + iActivity + ")";
}
if(document.getElementById('navActivityHistory') != null)
{
document.getElementById('navActivityHistory').getElementsByTagName('NOBR')[0].innerText = document.getElementById('navActivityHistory').getElementsByTagName('NOBR')[0].innerText + " (" + iHistory + ")";
}
}
}
function GetRegardingActivity()
{
var xml = "" +
"<?xml version="1.0" encoding="utf-8"?>" +
"<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">" +
GenerateAuthenticationHeader() +
" <soap:Body>" +
"<RetrieveMultiple xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>"+
"<query xmlns:q1='http://schemas.microsoft.com/crm/2006/Query'"+
" xsi:type='q1:QueryExpression'>"+
" <q1:EntityName>activitypointer</q1:EntityName>" +
" <q1:ColumnSet xsi:type="q1:ColumnSet">" +
" <q1:Attributes>" +
" <q1:Attribute>statecode</q1:Attribute>" +
" </q1:Attributes>" +
" </q1:ColumnSet>" +
" <q1:Distinct>false</q1:Distinct>" +
" <q1:Criteria>" +
" <q1:FilterOperator>And</q1:FilterOperator>" +
" <q1:Conditions>" +
" <q1:Condition>" +
" <q1:AttributeName>regardingobjectid</q1:AttributeName>" +
" <q1:Operator>Equal</q1:Operator>" +
" <q1:Values>" +
" <q1:Value xsi:type="xsd:string">" + crmForm.ObjectId + "</q1:Value>" +
" </q1:Values>" +
" </q1:Condition>" +
" </q1:Conditions>" +
" </q1:Criteria>" +
" </query>" +
"</RetrieveMultiple>"+
" </soap:Body>" +
"</soap:Envelope>" +
"";
var xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
xmlHttpRequest.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);
xmlHttpRequest.setRequestHeader("SOAPAction","http://schemas.microsoft.com/crm/2007/WebServices/RetrieveMultiple");
xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
xmlHttpRequest.setRequestHeader("Content-Length", xml.length);
xmlHttpRequest.send(xml);
var resultXml = xmlHttpRequest.responseXML;
return resultXml;
}
Any comments or suggestions are welcome!

Hi, I would like to use this but I can’t get it to work (CRM 4). I just get an authentication dialog three times then no activities display. Do you have any suggestions?
is your CRM deployed onPremise or IFD?
this is screwed - where’s soap:body and all that stuff
i am also getting an authentication error and my CRM Deployment is onPremise. what should i do?
Hi Ibrahim
I really like this idea, but am having some trouble getting it to run. I keep getting a javascript error when I add the code from the page.
I added the code to the onLoad of my Incident entity.
I was just wondering if the xml string is complete ? Do I have to do anything else to get it to jam with the Incident?
best regards
Arnar
Apologies for the delay in replying.. i have fixed up the code.
I have uploaded a working version at: http://mscrmblog.net/downloads/getCountOfOpenAndClosedActivities.txt
Tested it and seems to work.