Hello Readers,
its been so many days, I was waiting to write something very commonly required yet useful feature.
I have been asked so many times by my customers, How can i see the time spent on an activity, if we have entered the ‘Start Time’ & ‘Finish Time’. Yes, we can, its easy and simple. This time calculation can also be used for calculating, how much total we spent on a case resolution if we replace ‘Start Time’ by ‘Created on’ & ‘Finish Time’ by ;Actual Resolution on’ ; so on and so forth there can be multiple usage of this javascript based on your time calculation requirements. In this scenario, i am taking up a custom activity called ‘Repair Activity’; following is a screenshot:
Please note that the time spent field is a Deciaml Field
We need to follow the following steps:
Step 1: write the Javascript code as shown below-
function time()
{
var stdate=Xrm.Page.getAttribute(“new_starttime”).getValue();
var endate=Xrm.Page.getAttribute(“new_finishtime”).getValue();
if(stdate==null && endate==null)
{
alert(“Please enter the Start Time / Finish Time…”);
return false;
}
if(stdate==null)
{
alert(“Please enter the Start Time !”);
return false;
}
if(endate==null)
{
alert(“Please enter the Finish Time !”);
return false;
}
var st=stdate.getTime();
var ed=endate.getTime();
var sdiff=(ed-st);
var h=(sdiff/60/1000);
var p=parseInt(h);
var tm=p.toString();
Xrm.Page.getAttribute(“new_timespent”).setValue(tm);
}
Step 2: Add a webresource in CRM
Add a new webresource by typing in a name, Display Name, Language & Type>>Save & Publish>>URL will be genrated
Step 3: Add this webresource to form Libraries
Go to ‘Repair Activity’ Form Editor>> Form Properties>>Form Libraries>>Add
And also manage the ‘Event Handler’ by setting the event when you want this Javascript to be executed, so, i will set it on ‘On Save’ & set control to ‘Form’ & click OK
Once done save the form & publish it
Result : That’s it, here you go,now we can see the time spent as soon as we save the form; this javascript also, gives us an alert if we have not provided any value in Start & Finish time
Happy CRMing !!
It’s interesting the way you calculate the spent time in activities, my question is, how can I do to calculate the spent time in a case, when I resolve the case the activities are closed. Can you help me with that? Greetings
LikeLike
Hi, I just tried your script but im getting this error:
One of the scripts for this record has caused an error. For more details, download the log file.
ReferenceError: time is not defined at eval (eval at RunHandlerInternal (https://aquaflowservices.crm4.dynamics.com/form/ClientApiWrapper.aspx?ver=-2079214127:158:32), :1:1)
Could you please help 🙂
LikeLike