Design a site like this with WordPress.com
Get started

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.

Advertisement

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.