Restrict Auto Save at entity form level

Auto Save feature was introduced during CRM 2013 version and it is still will us today in Dynamics 365 version.

To activate AutoSave feature navigate to Settings ->Administrator –> System Settings -> Under General Tab -> Select Yes.

Enable Auto Save feature

Some important points about auto save:

  1. It works for update form means where form type = 2
  2. It does not work on create form means where form type = 1
  3. Auto save automatically triggers after 30 seconds.
  4. Once the auto save feature is enabled the Save button would disappear from the ribbon. User can save the form either by using Ctrl+S or by clicking the save icon which would appear on the right hand bottom side.
  5. Auto save feature can be enabled or disabled at organisation level. It cannot be enabled or disabled at entity level.

Considering 5 point in mind there are multiple scenarios where the Auto Save feature becomes a bottleneck for user. Example: If an organization has plug-ins, workflows, or jscripts that run on the OnSave event, they will trigger each time when auto-save occurs. This may potentially cause undesirable results especially if these customizations were not designed to work with auto-save.

For disabling the auto-save feature for a form, we will need a webresource of Javascript type.

Below is code logic which can be used for restricting a form from getting auto save.

function preventAutoSave(econtext) 
{ var eventArgs = econtext.getEventArgs(); 
if (eventArgs.getSaveMode() == 70 || eventArgs.getSaveMode() == 2) 
{ eventArgs.preventDefault(); } 
}

To know more about the Save event arguments(getSaveMode), Click Here.

Value Save ModeEntity
2Save and CloseAll
70Auto SaveAll
getSaveMode (Client API reference)

Let’s continue with the same javascript which we have used in our previous blog, I have added a new function formOnSave for preventing the auto save getting triggered.

var Namespace = window.Namespace || {};
(
function (){
this.formOnLoad = function(executionContext)
{
}

//Prevent form from triggering AutoSave 
this.formOnSave = function(executionContext)
{
var eventArgs = executionContext.getEventArgs();
if (eventArgs.getSaveMode() == 70 || eventArgs.getSaveMode() == 2) {
eventArgs.preventDefault();
}
}

this.validatephonenumber = function(executionContext)
{
var formContext = executionContext.getFormContext();
var phoneNumber = formContext.getAttribute("mobilephone").getValue();

//Using Regular expression
var regexpr = /(((\d{3}) ?)|(\d{3}-))?\d{3}-\d{4}/;

//Uing test for a match in a string. Returns true or false
if(!regexpr.test(phoneNumber))
{
/*Using field notification for displaying the error msg. User won't be able to save the form until the error is cleared up.*/
formContext.getControl("mobilephone").setNotification("Entered Phone number is not in a correct format", "validatephn");

//Using form notification for displaying the msg on the Form.
formContext.ui.setFormNotification("Warning Message", "WARNING", "formwarning");
}
else
{
/*Clear error notfication when the entered phone number is in the correct format. For clearing the notification only uniqueid is required.*/
formContext.getControl("mobilephone").clearNotification("validatephn");

/*Clearing form notification when the criteria is met. For clearing the notification only uniqueid is required.*/
formContext.ui.clearFormNotification("formwarning");
}
}
}
).call(Namespace);

Now, the JScript formOnSave function needs to be called on OnSave event of contact entity form.

formOnSave function has been added on OnSave event of Contact entity.

Hit the OK button. Save and Publish the changes.

Navigate to the Contact entity form, press Ctrl+F5 for clearing cache.

Now the Auto-Save would not trigger on the Contact entity form. User will need to press Ctrl+S or click on the Save button on the right button side of the screen.

Dynamics CRM: Notification for Client API Scripting

We have two types of notification which are useful when we want to populate any message to a user using client-side scripting.

  1. Form Notification
    • Syntax: formContext.ui.setFormNotification(message, level, uniqueid);
  2. Field Notification
    • Syntax: formContext.getControl(“logicalname”).setNotification(message, uniqueid);
  1. Form Notification is used when we want to populate any message to users on the form level. This is not specific to any attribute. It does not stop the user from saving the form,
    • Syntax: formContext.ui.setFormNotification(message, level, uniqueid);
    • message –> Here we need to write the message/alert which would get populated to the user.
    • level –> we have three types of levels. This is used to display an appropriate icon in front of the message.
      • INFO
      • ERROR
      • WARNING
    • uniqueid –> This can be any user define string value.

Field Notification is used when we want to display any error message to a user and restrict the user from saving the form until the error is cleared up.

We will learn how to validate the phone number format entered by the users.
I will use the namespace pattern for writing the Javascript. I have already explained the namespace function in one of my previous blogs. Click here to visit the blog.

Now, let’s continue with the contact entity where we would validate the phone number format for the mobile phone attribute.
Below is the code which can be used for validating the phone number format and notify the user when the entered phone number does not meet the required format criteria.

var Namespace = window.Namespace || {};
(
function (){
this.formOnLoad = function(executionContext)
{
}
this.validatephonenumber = function(executionContext)
{
var formContext = executionContext.getFormContext();
var phoneNumber = formContext.getAttribute("mobilephone").getValue();

//Using Regular expression
var regexpr = /(((\d{3}) ?)|(\d{3}-))?\d{3}-\d{4}/;

//Uing test for a match in a string. Returns true or false
if(!regexpr.test(phoneNumber))
{
/*Using field notification for displaying the error msg. User won't be able to save the form until the error is cleared up.*/
formContext.getControl("mobilephone").setNotification("Entered Phone number is not in a correct format", "validatephn");

//Using form notification for displaying the msg on the Form.
formContext.ui.setFormNotification("Warning Message", "WARNING", "formwarning");
}
else
{
/*Clear error notfication when the entered phone number is in the correct format. For clearing the notification only uniqueid is required.*/
formContext.getControl("mobilephone").clearNotification("validatephn");

/*Clearing form notification when the criteria is met. For clearing the notification only uniqueid is required.*/
formContext.ui.clearFormNotification("formwarning");
}
}
}
).call(Namespace);

Below is the screenshots of field/form notification. I have called the script on Contact entity and it would trigger on mobile phone attribute On Change event.

We can see that once the error(phone number format) is resolved the notification has been automatically cleared up.

Use the link to know more about Regular Expression.

I hope this blog helps in understanding the notifications, their types and when to use form or field notification.

Understanding Client API Objects

Microsoft has provided an API facility for retrieving the data from the form directly using web-resource(JavaScript/HTML etc) which is known as Client API.

Client API helps us in many ways like:

  1. Get or set attribute values.
  2. Show and hide user interface elements.
  3. Reference multiple controls per attribute.
  4. Access multiple forms per entity.
  5. Manipulate form navigation items.
  6. Interact with the business process flow control.

Client API has 4 main objects:

  1. Execution Context
  2. Form Context
  3. Grid Context
  4. XRM object

I will be covering the two most used object in this blog which is Execution Context and Form Context.

Execution Context: executionContext defines the event (OnLoad, OnSave, OnChange) on which your code is executed. It passes the runtime information about the event which took place on the forms or grids like user information, form level information, attribute level information etc. It is used in the function as a object which gets all the necessary information from the CRM to our code . Therefore it is required to pass execution context as a first parameter inside the event handler.

function OnContactLoad(executionContext)
{
}

Form Context: formContext is a part of executionContext. It provides us an access to a form or form items using JavaScript. Before D365 v9.0 we were using XRM.Page which performed the same role as formContext. With the v9+ of CRM when XRM.Page is being deprecated hence it is always recommended to use formContext.

For accessing formContext below is the command:
var formContext = executionContext.getFormContext();

function OnContactLoad(executionContext)
{
var formContext = executionContext.getFormContext();
}

formContext contains two object:
Data object: This used to get the attributes data from the form.
Example: We have a text field on the form and we want to access the field data, then we will be using data object.
formContext.data.entity.attributes
or
formContext.getAttribute(“logicalname”).getValue();

UI object: This is useful when we want to hide any control or disable any control.
formContext.ui.controls
or
formContext.getControl(“logicalname”).getDisabled();
To know more about controls I would recommend to go through Microsoft documents on Controls.

Introduction to an API

What is an API?

API is the acronym for Application Programming Interface, it acts as an intermediary between two application.

Connectivity is a very amazing thing. In today’s world we are very much habituated to instant connectivity across the globe. Ever thought about how does the data travel from one place to another or from a device to an application.
Each time we use any online shopping site or our phone to check weather or Facebook we are using an API.

In a layman’s term an API acts as a messenger, which takes the request from a user and inform the system what a user wants to do, and return the response back to the user.
Example: When you would go to a restaurant you would look into the restaurant menu and place an order to a waiter of the restaurant. The waiter is the person who would take your order from your table to the kitchen where your order would get prepared.

In the above example the waiter acted as a critical link between your order to the kitchen and deliver your food back to your table.
That’s how an API works. It acts as a messenger which takes your request to the system (in our case its kitchen) which tells the system what to do and deliver the response back to you (in our case its the food.)

Now let’s understand the API in a more realistic way. Most of us might have booked a movie ticket using an app or a website. Let’s take an example of BookMyShow app, this app helps us in booking the movie tickets online. Any thought how does the app gets you the movie timing and the tickets fares from different multiplex/malls which are currently running which show. Yes, its the API which play a very important role, the unsung warrior of our connected world.

I would say API acts as an engine who plays a very important role behind the scene, because of API only it is possible to get what we expect and rely upon.

Hope now you are clear with what is an API and its use.