Dynamics 365 WEB API · Dynamics CRM Javascript · Javascript · Uncategorized

{Dynamics CRM Web API} Calculate Total time spent on Case

First of all let me give you some background, When we resolve case from CRM case form using Resolve ribbon button you see a dialog which has Total time and Billable time auto poulated by the system.

The Total Time field gets updated only when the Child Activities with duration have been marked as completed. The Billable time automatically take the time of Total time, but it can be modified at any point of time.

The Total Time only calculates the completed activities ONLY.


Now coming to my original requirement, We need to calculate total time spent on case for Reporting/BI purpose. So CRM OOB has a function Microsoft.Dynamics.CRM.CalculateTotalTimeIncident() which can be used to calculate total time spent on a case.

Web API Code to get total time spent on a case

var parameters = {};
var entity = {};
entity.id = "ca9e62a8-90df-e311-9565-a45d36fc5fe8";
entity.entityType = "incident";
parameters.entity = entity;

var calculateTotalTimeIncidentRequest = {
    entity: parameters.entity,

    getMetadata: function() {
        return {
            boundParameter: "entity",
            parameterTypes: {
                "entity": {
                    "typeName": "mscrm.incident",
                    "structuralProperty": 5
            operationType: 1,
            operationName: "CalculateTotalTimeIncident"

    function success(result) {
        if (result.ok) {
            var results = JSON.parse(result.responseText);
    function(error) {



Xmlhttprequest code to calculate total time spent on a case

var req = new XMLHttpRequest();
req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v9.1/incidents(f69305a1-c1f5-4fca-9079-ad398caf9c83)/Microsoft.Dynamics.CRM.CalculateTotalTimeIncident()", true);
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.onreadystatechange = function() {
    if (this.readyState === 4) {
        req.onreadystatechange = null;
        if (this.status === 200) {
            var results = JSON.parse(this.response);
        } else {



Key Points –

  1. When we resolve a case in CRM it creates incident resolution entity record which stores all details such as Remark, Total Time, Billable Time and other field details which is present on Resolve Case Dialog
  2. When we reactivate a case system changes the status on Incident Resolution entity record as cancelled
  3. CalculateTotalTimeIncidentResponse is not just returning the field value from latest IncidentResoultion entity record.
  4. To prove that I closed incident which has one phone call with 30 mins duration, I execute this function and in return I got result as 30. Now I added one more phone call on closed case with 30 minuts duration. Now when i executed this function I got 60 in response.

Hope this helps!


2 thoughts on “{Dynamics CRM Web API} Calculate Total time spent on Case

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s