It was time.
Time to finally start mastering the JavaScript syntax changes to Dynamics 365 v9.0.
I’ve been using JavaScript forever in CRM forms since version 4.0. Instead of continuing to use now deprecated code, it was time to bite the bullet. Sorry Xrm.Page, you’ve been great. I hope you enjoy retirement with plenty of fruity, boozy drinks with umbrellas.
We’re using Dynamics 365 as a custom project and task management system (among other things). One of the things we track is billable time for our clients and there’s been plenty of times when we forget to fill it in.
Then it’s time for me to do billing for the month and I get really grumpy when the total time is 0 hours.
On the Task form there is a subgrid called Time Log where the users enter the time spent on a project:
There are two other yes/no fields we added to Task: Is Billable? and Ready for Review?
All of the billable tasks are first reviewed by the Chief Many Things Officer (that’s you Sean) and when completed go to me so I can bill this time to our clients:
When the task is ready to be reviewed, the user changes the Ready for Review? field to “Yes”:
Now that the history is out of the way, how to prevent the Time Log from being blank? With some JavaScript of course! That’s the whole point of this post!
Thank you to both Carl de Souza and Ritchey Hazeu — you were both my direction for the JavaScript changes.
If the Is Billable? field is “Yes” and the user marks the Task ready to review, there is a check to see if the grid is empty. If it is, this prompt appears:
Neat, right? And it is the Power Apps confirmation prompt, not the yucky JavaScript alert prompt.
Once the user clicks OK, the Ready to Review? field is automatically set back to “No”. After the Time Log is filled in, the user can successfully mark the task ready for review. Plus I am not grumpy anymore!
Here’s the JavaScript:
function ReadyForReviewChange(executionContext) { var formContext = executionContext.getFormContext(); // get the form context var gridContext = formContext.getControl("Subgrid_TimeLog");// get the grid context var isBillable = formContext.getControl("jcma_isbillable").getValue(); if (isBillable == "Yes") { var count = formContext.getControl("Subgrid_TimeLog").getGrid().getTotalRecordCount(); if (count == 0) { var alertStrings = { confirmButtonLabel:"OK", text:"There is no time entered in the Time Log.", title:"Oops!", subtitle:"This is a billable task" }; var alertOptions = { height: 120, width: 260 }; Xrm.Navigation.openAlertDialog(alertStrings, alertOptions).then( function (success) { formContext.getAttribute("jcma_readyforreview").setValue(false); }, function (error) { formContext.getAttribute("jcma_readyforreview").setValue(false); }); } } }
Then all I needed to do was add my function name to the On Change event handler on the Ready to Review? field and publish all the customizations!
Absolute magic.