Form save event usage

While working with the Form OnLoad event can help us format and arrange the user interface, working with the Form OnSave opens up a new door towards validation of user input and execution of business process amongst others.

Getting ready

Using the same solution we have worked on in the previous recipe, we will continue to demonstrate a few other aspects of working with the forms in Dynamics CRM 2011. In this recipe the focus is on the handling the Form OnSave event.

How to do it...

First off, in order to kick off this, we might want to verify a set of fields for a condition, or perform a calculation based on a formula. In order to simplify this process, we can just check a simple yes/no condition on a form.

How it works...

Using the previously customized solution, we will be taking advantage of the Contact entity and the fields that we have already customized on that form. If you are starting with this recipe fresh, take the following step before delving into this recipe:

  1. Add a new two-options field, named Is Special Customer (new_IsSpecialCustomer). Leave the default yes/no values.
    How it works...

    Using this field, if the answer is No, we will stop the save process.

  2. In your solution add a new web resource. I have named it new_ch4rcp2. Set its type to JScript.
  3. Enter the following function in your resource:
    function StopSave(context)
    {
      var _isSpecialSelection = null;
      var _isSpecial = Xrm.Page.getAttribute("new_isspecialcustomer");
    
      if(_isSpecial != null)
      {
        _isSpecialSelection = _isSpecial.getValue();
      }
      
      if(_isSpecialSelection == false)
      {
        alert("You cannot save your record while the Customer is not a friend!");
        context.getEventArgs().preventDefault();
      }
    }
  4. The function basically checks for the value in our Is Special Customer. If a value is retrieved, and that value is No, we can bring up an alert and stop the Save and Close event.
  5. Now, back on to the contact's main form, we attach this new function to the form's OnSave event.
  6. Save and Publish your solution.
  7. In order to test this functionality, we will create a new contact, populate all the required fields, and set the Is Special Customer field to No.
    How it works...
  8. Now try to click on Save and Close.
  9. You will get an alert as seen in the following screenshot, and the form will not close nor be saved.
    How it works...
  10. Changing the Is Special Customer selection to Yes and saving the form will now save and close the form.

There's more...

While this recipe only describes in a very simplistic manner the way to stop a form from saving and closing, the possibilities here are immense. Think about what you can do on form save, and what you can achieve if a condition should be met in order to allow the form to be saved.

Starting a process instead of saving the form

Another good use for blocking the save and close form is to take a different path. Let's say we want to kick off a workflow when we block the save form. We can call from the previous function a new function as follows:

function launchWorkflow(dialogID, typeName, recordId) 
{ 
var serverUri = Mscrm.CrmUri.create('/cs/dialog/rundialog.aspx'),
 window.showModalDialog(serverUri + '?DialogId=' + dialogID + '&EntityName=' + typeName + 
'&ObjectId=' + recordId, null, 'width=615,height=480,resizable=1,status=1,scrollbars=1'),
// Reload form 
window.location.reload(true);
}

We pass to this function the following three parameters:

  • GUID of the Workflow or Dialog
  • The type name of the entity
  • The ID of the record

See also

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.149.254.7