Writing reusable JavaScript functions
Fool me once, shame on you; fool me twice, shame on me!
Whenever you see yourself copying and pasting code more than once, consider refactoring your code into reusable functions. In the first two recipes of this chapter, we built single-purpose functions with hardcoded values. We can refactor our code into a collection of reusable functions.
In this recipe, we will refactor our two functions into generic ones by parameterizing them.
Getting ready
As per the previous recipe, you will need an IDE and access to a Dynamics 365 instance with the System Customizer
 or higher security role.
How to do it...
- Navigate to
Settings
|Solutions
|Packt
. - Click on
Web Resources
and double-click onpackt_common.js
. - Click on
Text Edit
and change thepopulateWithTodaysDate
code to the following:
packtNs.common.populateWithTodaysDate = function(attributeToMonitor, dateAttributeToChange) { if (Xrm.Page.getAttribute(attributeToMonitor).getValue() !== null && Xrm...