*** NOTE: ALL INFORMATION IS ACCURATE AT DATE OF PUBLISHING ***

I was asked recently by a client as to how a date field could be populated with TODAY’s date on a D365 Marketing Form. Easy I thought, we can just set a default value, just like you can with an Optionset/Choice field, or populating some default text on other fields. Should be easy. Is it though? Well, not quite that simple, so let’s take a look at how this can be achieved. Credit goes to Igor Bazarny from the D365 Marketing team at Microsoft for pointing me in the right direction!

First, create your marketing form field and map it to the correct Date field on your Lead or Contact record. You must have the format correct. So if you have created a date field that is Date only, you must select that format for it to work and vice versa with a date field that is Date and time. In this example, we will have a date field on a form for selecting a date for a demo, which will be pre-populated for today. This is just an example to show how you can achieve this. More on populating a day in the future later.

Click to view in detail

Next, from the top of the browser in the URL, go to the end of the string and get the id that is right at the end. Save this somewhere as you will need it later.

Click to view in detail

Next, add your field to the form. Although you can set a default value from here, you have to actually pick a specific date, which isn’t going to meet the requirement.

Click to view in detail

What we need to do instead, is add a little bit of javascript to the same web page that the form is embedded on. This should be placed directly below the main script for the form. Replace the code below that starts fd0c6ded with the full id of the marketing form field you added.

<script>
MsCrmMkt.MsCrmFormLoader.on('afterFormLoad', function(event) {
     document.getElementById("fd0c6ded-10e6-eb11-bacb-000d3a4af96f").value = new Date().toISOString().substr(0, 10)
});                
</script>

Now when the form is loaded, the script populates today’s date directly into the date field with the same id stated in the script. Pretty cool!

Click to view in detail

What about setting a date in the future? For example, I want to set the Estimated Close Date on a Lead to 4 weeks from the date the form was submitted, so 4 weeks from todays date. Here we can add a bit more to the script to figure out what that date should be. Again, we will need to set the marketing form field id for the specific date field on the form.

<script>
MsCrmMkt.MsCrmFormLoader.on('afterFormLoad', function(event) {
let d = new Date();
let FourWeeksFromNow = d.setDate(d.getDate() + 28);
document.getElementById("b6a0cd51-8ff3-eb11-94ef-0022481a8b2c").value = new Date(FourWeeksFromNow).toISOString().substr(0, 10)
});                
</script>

This then populates our specified date field accordingly.

Click to view in detail

To fit those two things together, we can put together something like this, setting todays date in one of the date fields, and 4 weeks from now in the other one.

<script>
MsCrmMkt.MsCrmFormLoader.on('afterFormLoad', function(event) {
let d = new Date();
let FourWeeksFromNow = d.setDate(d.getDate() + 28);
document.getElementById("0b9e2757-a8eb-eb11-bacb-0022481a42b3").value = new Date().toISOString().substr(0, 10);
document.getElementById("b6a0cd51-8ff3-eb11-94ef-0022481a8b2c").value = new Date(FourWeeksFromNow).toISOString().substr(0, 10)
});                
</script>

This is great to see what is possible, but what if I don’t want that second date displayed?

Click to view in detail

This is where we can go in to the HTML of the Marketing Form in D365, and find the date field we want to hide. Where it shows Field-date, change it to Field-checkbox, then go back to the Designer tab.

Click to view in detail

This will allow the Hide field option to be accesible for the field. This means we can turn this on so that the form will be populated with the required date but will not be displayed to the person submitting the form. Nice one!

Click to view in detail


Check out the latest post:
Removing Issues With Mobile Phone Numbers On Realtime Forms


D365 Marketing Weekly
Have you seen the D365 Marketing Weekly newsletter yet?
A weekly issue covering features, functionality and news on the topic of Marketing, specifically covering Dynamics 365 Marketing and other interesting tools and tips for anyone interested in the subject.
Subscribe Here
This is just 1 of 447 articles. You can browse through all of them by going to the main blog page, or navigate through different categories to find more content you are interested in. You can also subscribe and get new blog posts emailed to you directly.