In Dynamics CRM, the Sales team wants to know the last activity done
for a lead, contact, or account.
We
have an out-of-the-box field called Modified On that displays the date when a record was last updated.
Modified On does not keeps a track of various activities like tasks, emails, phone calls that are associated with the actual
records. We
need a way to track when was the last activity completed for any record.
Demo Screenshots
Leads Date of Last Activity got updated as to when you create or update any associated activity.
Here is a solution that consists of a custom field and a few lines of JavaScript code.
1. Create a custom field
: 'Date of Last Activity'
We need to create a field on every parent entity (like Account, Contact, Lead) where we wish to know when was the most recent or last activity performed.
Note : Field name must be the same for all respective entities where the Last Date of Activity needs to be updated or change the script accordingly.
new_dateoflastactivity
2. Create a custom JavaScript
Webresource
Jscript Content
function onSaveUpdateDateOfLastActivity(context)
{var serverUrl = Xrm.Page.context.getClientUrl() + "/api/data/v9.1/";
var formContext = context.getFormContext();
var regardingObj = formContext.getAttribute("regardingobjectid");
if (regardingObj != null)
{
var regardingObjValue = regardingObj.getValue();
if (regardingObjValue != null)
{
var entityTypeName = regardingObjValue[0].entityType;
var entityId = regardingObjValue[0].id.replace("{", "").replace("}", "");
var entity = {};
entity.new_dateoflastactivity = new Date();
var req = new
XMLHttpRequest();
req.open("PATCH",serverUrl + entityTypeName + "s(" + entityId + ")", false);
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
=== 204)
{
//Success - No
Return Data - Do Nothing
} else
{
// You can remove the below alert if you do not wish the customer to face
such error.
Xrm.Utility.alertDialog(this.statusText
+ "\n DateOfLastActivity field not found for an entity :
" + entityTypeName );
}
}
};
req.send(JSON.stringify(entity));
}
}
}
3. Register OnSave Event
on each Activity Type (tasks, emails, phone
calls)
Remember
to tick for Pass execution context as the first parameter
I hope
you find this solution worthwhile, please let me know if you have any such
business scenario and you are looking for any technical solution for it.
Thanks.
Vipin Jaiswal
Vipinjaiswal12@gmail.com
1 comment:
Thanks for the information. Really help me a lot.
Post a Comment