Tuesday, June 15, 2010

Show no. of open or closed activities (history) on the CRM form.

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;
}
 
 
 
Source: http://mscrmblog.net/2009/05/18/show-no-of-open-closed-activities-history-on-the-crm-form/ 

No comments:

Post a Comment