Whenever we are saving a record in CRM 2011 there can be a lot of scenarios where we have to do some validation with respect to business flow for the forms. Such a case is checking for a required field or a business validation.
Required field validation can be implemented by marking the field are required. But this will help while saving the record.
Let's look at an example for lead entity. As per my requirement while saving a lead record, its not mandatory to have a particular field. But when the lead is getting qualified that field is required. Such cases can be implemented by using two functions in the client side itself.
Use the savemode() method to identify whether the save event was initiated as a result of click of qualify button for the lead.
If that is the case, check for the field and make sure the field is having a value.
If the field is not having an entry prevent the save operation using the
ExecutionObj.getEventArgs().preventDefault()
Execution object will be available by checking the check box to pass the execution context as the first parameter to the method. Please check my previous blog for more details.
Below is an example for the above scenario.
function qualify_lead_onclick(context)
{
var saveMode = context.getEventArgs().getSaveMode();
//Lead qualifies.
if (saveMode == 16)
{
if (Xrm.Page.getAttribute('fieldname').getValue() == null)
{
context.getEventArgs().preventDefault();
alert('Field Name cannot be empty while qualifying a lead.');
}
}
}
No comments:
Post a Comment