23 February 2008

Show how many activities/history associated with a record


Sometimes we want to see how many activities/history associated with a record, it's nice to have a number just like the Outlook Inbox.

Here's the code, it works fine on both CRM 3.0 and 4.0 (slightly different). Please notice that you have to reload the record to get the current number after you add/close an activity.


var buXml = GetRegardingActivity();

if(buXml != null)
{
var buNodes = buXml.selectNodes("//BusinessEntity/statecode"); // CRM 3.0

//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\">" +
" <soap:Body>" +
" <query xmlns:q1=\"http://schemas.microsoft.com/crm/2006/Query\" xsi:type=\"q1:QueryExpression\" xmlns=\"http://schemas.microsoft.com/crm/2006/WebServices\">" +
" <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>" +
" </soap:Body>" +
"</soap:Envelope>" +
"";

var xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
xmlHttpRequest.Open("POST", "/mscrmservices/2006/CrmService.asmx", false);
xmlHttpRequest.setRequestHeader("SOAPAction","http://schemas.microsoft.com/crm/2006/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;
}

21 comments:

  1. Anonymous5:07 pm

    Great and useful script. Put this one to my demo scripts. Just one little thing. This script doesn´t work for activities which are in history. It works fine if you´re closing an activity, but not with the history activities you´ve closed or canceled in the past.

    ReplyDelete
  2. Anonymous6:05 pm

    ...just my fault. This script is only looking for the objectid in the regarding field. This means: it works fine if you´re using it for cases, but it doesn´t work if you´re using it within the account form. Because the regarding field can be the customer guid, but of course it can be the cases guids as well. So I have to modify your soap expression. Then it will show all the elements.

    ReplyDelete
  3. Thanks Carsten. I think I tested it which works for Account, Contact, Lead, Case, etc and Customer entities.

    One thing I forgot was the statecodename:Scheduled, it has been modified now.

    But it doesn't count the Campaign Response which uses a different SQL table. Plus the Campaign Response only works for Account/Contact/Lead, so I didn't cover it.

    ReplyDelete
  4. Thanks for your email. Yes, the number will only count if the activity regarding field was connected with the current record. It doesn't count the related activities.

    ReplyDelete
  5. Hi Jim. What would it take to modify this to work with custom entities? Thanks, Tim

    ReplyDelete
  6. Tim,

    You don't need to change anything to work with custom entity, as soon as the custom entity has activity/history related.

    Jim

    ReplyDelete
  7. When I open a form I get an error - the download of the specified resource failed.

    I presume this goes in the onLoad of the form?

    I am running CRM 4.0, and the only change I made was to swap the commenting for the 3/4 lines.

    ReplyDelete
  8. Jim,

    I need to update my previous posting, it appears that when I was testing it I was tyring to use it over the Internet, it doesn't work there.

    ReplyDelete
  9. Hi Jim, is this something that could be adapted to be used on the Queues within CRM to show the number of items within each queue?

    Thanks, Matt

    ReplyDelete
  10. Anonymous4:03 am

    Great script ... thanks.
    Just had another idea. What do I need to change to get the count of related entities rather than the count of related activites. For example the number of contacts (or custom entity) in the Account form.
    thanks a lot.

    ReplyDelete
  11. Jim,

    Is this an onLoad script? How does this get deployed?

    ReplyDelete
  12. Would there be much work involved in converting this to work the same but against queues. We have a requirement to see how many items in a queue without having to go into the queue.

    ReplyDelete
  13. Hi Jim,

    Thanks for your article.
    I have recently posted an updated version on my blog based upon this example to count related records.


    http://boudewijnsnoeren.blogspot.com/2009/04/count-related-items-javascript.html

    ReplyDelete
  14. Hi...

    Did anyone find a way of doing this for queues? Thx

    ReplyDelete
  15. Anonymous5:16 pm

    Hi Jim!

    Thanks for the post!

    One issue that I'm running into with your script: I'm getting prompted for User Name and Password each time that I access a record with this script on it. If I click "OK" 3 times, it makes it past and runs correctly, but won't run without the prompt? Using windows 7 and ie 8

    ReplyDelete
  16. Anonymous5:17 pm

    This comment has been removed by the author.

    ReplyDelete
  17. I love this code - brilliant! I am having a similar issue where my users will get prompted for a login/password. What is the solution to this? I can recreate this on the server using the CRM Administrator account.

    Cheers!
    Craig

    ReplyDelete
  18. Hello, your blog is really good, I like it very much!By the way, if you like nike shoes you can come here to have a look!
    Three icons converge in Nike Shoes Us, a bold design that combines the best style and cushioning features from legends of Nike Air Max Shoes. With the lacing system of the '95, "Nike air max 2011the silhouette of the '97, and the full-length Max Air inspired by the Nike Air Max 2009, this shoe delivers unbeatable comfort 24 hours a day, seven days a week.

    Nike Air Max 1
    Air Max Griffey 1
    Nike Air Max 180
    Nike Air Max 2009
    Nike Air Max 2010
    Nike Air Max 24 7
    Nike Air Max 87
    Nike Air Max 2011
    Nike Air Max 91
    Nike Air Max BW
    Nike Air Max Plus TN
    Nike Air Max LTD
    Nike Air Max Boots
    Nike Air Max Presto
    Nike Air Max Skyline
    Nike Air Max Turbulence
    Nike Air Max Fitsole
    Nike Air Max Goadome
    Nike Air Max Zenyth
    Nike Air Max Wildwood Supreme
    Nike Air Max Zoom Kobe
    Nike Air Max Shoes.

    ReplyDelete