Topics

Knowledge Creation Laboratory by CRM(xRM)

D365 Customization Barriers and How to Overcome Them: ohya's Practical Diary [#004] ~When you open a form, "today's date" is automatically entered!? A smart solution with JavaScript~

Hello, this is ohya.

This time, we will introduce how to automatically enter today's date when opening a form in Dynamics 365.

"Well, that's easy, right?" you might think.
Actually thisThis cannot be done with Power Automate or business rules.That's it...!

At first glance, it may seem like a simple requirement, but it's actually surprisingly complex!

🛠️This time's wall: "I want to have 'Today' pop up the moment I open the form!"

We created a "Contact History" table to record sales activities.
In the "Contact Date" field,I want "today's date" to be automatically entered when the form is opened.There was a request.

But here's the problem...! I couldn't achieve my goal using the usual methods.

❌ Why the usual method didn't work

    • Business Rules → There is no function to set "Today's date"
    • Power Automate → It only works when it is saved
    • When you open the formPower Automate will not respond because no records have been created yet!

In other words, a different approach was needed to enable "insertion the moment the form is opened."

✅ Solution: Real-time input with JavaScript!

Therefore, this time we adopted a method that uses JavaScript to automatically enter "today's date" when the form is displayed .

1. Create a JavaScript web resource

First, include the following code: .js Create the file:

function setTodayDate(executionContext) {
    //Get the form context
    var formContext = executionContext.getFormContext();
    // Get today's date
    var today = new Date ();
    //Get the contact history entry date field
    var contactDateField = formContext.getAttribute("acsj_contact_history_entry_date");
    // If the entry date field is empty
    if (contactDateField.getValue() === null) {
        // Set today's date to the entry date field
        contactDateField.setValue(today);
    }
}

2. Add a web resource to your form

  1. Open the form editor
  2. Add the above JS file to "Form Library"
  3. On the "form load" event setTodayDate Register a function
  4. executionContext Don't forget to set it to pass!

3. Operation check

The moment you open the form, today's date will be automatically entered into the "Contact Date" field!

✅ Summary

  • Requirements that cannot be achieved with business rules or Power Automate can be solved with JavaScript!
  • To "Set values when form is opened"Form Events + JavaScriptis valid
  • With a small ingenuity,Reduces user effort and errorsThat's the fun of customization!

✍️ Conclusion

Even a little auto-fill can be a big help to users.
Like this time,Real-time input assistance using JavaScriptgreatly expands the scope of customization for D365.

Next time, I'll be sharing the knowledge I've gained through hands-on experience, so stay tuned!

Person who wrote this article
ohya

Nice to meet you! I'm a newbie who jumped into the IT industry in March 2025 and am still learning. I started with no experience in programming or IT, but I'm working hard every day, valuing the motto of "study every day, grow every day, and be grateful every day." There's a lot I don't understand, but that's also why I enjoy learning so much! I'm aiming to move forward little by little and become an engineer who can be useful to others. I look forward to working with you!

Articles in the same category