Friday, January 20, 2017

Quick Create and Javascript

With the latest versions of CRM we have got the quick create forms, in which only a number of fields would be displayed. Also the form comes up as a pop up. One thing to note with this is when you click the + sign if the quick create form to pop up the relationship should be a mandatory one between the 2 entities. If this is not the case then when you click on the + sign you would get a sort of existing lookup feature wit 10 records and then a + sign on that.

With quick creates whether to validate a value of a field or stop the record from creating is something a lot of developers would ponder about. This is because the requirements do vary with each client and they don’t want to proceeding saving the record just like from a normal main form in CRM.


The question remains whether this possible using javascript. The answer is yes, this is possible. However you would have to write odata queries to check values of the parent record as it is not easy getting values from the record which has opened the quick create. Using odata queries you definitely can query the parent record or any record you need to do validations. So yes, javascripts are definitely possible with CRM quick create form.

How to prevent a record being saved in CRM

In CRM, we can prevent the save of a record using either a plugin, synchronous workflow or a javascript.

Using a Plugin you can do by throwing an InvalidPluginExecutionException. This would stop the record from being saved. This can be done by a pre plugin.

With the introduction of CRM Synchronous workflows, you can achieve the same result. There is an option within the workflow to say stop workflow with status of Canceled. Here you can give an error message also. The only issue with this would be, you wouldn’t be able to do complex checking here without a custom workflow being written. But this is a very nice feature as you can quickly come up with a workflow for a simple validation without having to write code.
The next is Javascript. You can use this to do complex validations if you know your javascript well. Then you can stop the page from saving. You need select the save event with passing the parameter “Pass execution context as first parameter” for this.

Here, one need to be careful using javascript if you are going to check for the event to stop from saving. As there are events in CRM javascripts you need to choose for which save mode you want to stop this from happening. This MSDN article describes about save mode very nicely. https://msdn.microsoft.com/en-us/library/gg509060.aspx

If you use a code like below it would be very good to use mode 70 which is the auto save mode as is the auto save of the form is enabled if that line is not there the record would be saved. That would fail the requirement. It would be better that you always use that mode if you really want to stop the record from saving.

function preventSave(context) {
    if (context != null && context.getEventArgs() != null) {
        var eventArgs = context.getEventArgs();
        if (eventArgs.getSaveMode() == 1 || eventArgs.getSaveMode() == 70) {
            eventArgs.preventDefault();
        }
    }

}

Retrieving Calendar of a Bookable Resource in Dynamics

There are occasions where we need to retrieve working days and working times of a resource in Dynamics grammatically. This is quite possible...