Looking for PowerObjects? Don’t worry, you’re in the right place! We’ve been part of HCL for several years, and we’ve now taken the final step in our acquisition journey: moving our website to the HCL domain. Nothing else is changing – we are still fanatically focused on Microsoft Business Applications!

PowerObjects Blog 

for Microsoft Business Applications

|

Populating Due Dates with Javascript

Post Author: Joe D365 |

If you spend a lot of time creating activities in Microsoft Dynamics CRM and would like a simple way to auto-populate a date field, here’s how:

First, decide what field you’d like to populate.  In this example, we focus on the due date field of the phone call entity.  Second, determine when you would like the field to populate.  Will it only be when creating new entities, modifying existing ones or both?  In this case, we’ll set the due date field on the creation of the phone call entity only.

The following JavaScript function is added as a web resource and then the function will be called on the OnLoad event of the form.

function SetDueDateField()
{
if(Xrm.Page.ui.getFormType() == 1)
{
var dueDate = Xrm.Page.data.entity.attributes.get("scheduledend");
var now = new Date();
var endDate = new Date().setDate(now.getDate()+7);
dueDate.setValue(endDate);
}
}

Above, you’ll notice that our whole function is wrapped in an if statement that is comparing the FormType.  This is how we determine if the form is for a new entity or the modification of an existing one.  On the next line, you’ll notice us getting a reference to our due date field (in this case, its schema name is scheduledend).  Later, we’ll use this to set the date we create.

The next two lines are where we create a Date (now) that matches the current date and time and then create a new date that exists exactly seven days in the future.

Finally, we assign our end date to the due date field we referenced earlier.  There you have it, a simple and quick way to remove a tedious task and effected standardization in your process.

Happy CRM'ing!

Joe CRM
By Joe D365
Joe D365 is a Microsoft Dynamics 365 superhero who runs on pure Dynamics adrenaline. As the face of PowerObjects, Joe D365’s mission is to reveal innovative ways to use Dynamics 365 and bring the application to more businesses and organizations around the world.

PowerObjects Recommends