Blocking events

One of the most common requirements that you will eventually encounter is to block a form from getting saved if a certain condition is not met.

Getting ready

Use any of the previously created solutions, or if starting at this recipe create a new solution for this exercise. Add the Contact entity to your solution.

How to do it...

To block events, perform the following steps:

  1. Within your solution, navigate to Entities | Contact | Forms and open the Contact main form for editing. Add a new field of type Two Options. Name it Can Save (new_cansave) and leave the default Yes and No values.
  2. Add the field to your form.
  3. Save and publish the form.
  4. Add a new web resource named JS Event Blocking (new_JSEventBlocking).
  5. Insert the following code:
    function BlockSave(context)
    {
      var _canSave = Xrm.Page.getAttribute("new_cansave").getSelectedOption().text;
      if(_canSave == "No")
      {
        Xrm.Page.context.getEventArgs().preventDefault();
      }
    }
  6. Save and publish the resource.
  7. Associate the BlockSave function to the form OnSave event. Make sure you check the checkbox for Pass execution context as first parameter.
  8. Save and publish your solution.
  9. Open a contact or create a new one. Set the Can Save field to No, and click on Save or Save and Close. Your form will not get saved anymore.
  10. Return the Can Save to Yes, and click on Save or Save and Close. Now your form gets saved as expected.

    Note

    It's usually a good idea to notify the user that you've blocked the form save, and provide a reason why. Also, for usability, consider setting the focus back on the field where the input value is not within the expected parameters.

How it works...

In this example the rule is as simplistic as it can get. We read the value of Can Save, and if the value is No, then we block the form from getting saved. In a real-life situation your condition will definitely be more complex than that. It could be driven by a set of fields, a complex calculation, or any other business rule. For simplicity, you can always create a hidden field similar to our Can Save field, and based on the business rules set the value accordingly. Then your script to prevent a save is kept clear of your main logic.

There's more...

The opposite of stopping a form from getting saved has a few variations. We can look at either the simple straightforward Save, Save and New, or Save and Close.

Forcing a Save

In order to force a save in JavaScript, incorporate the following line of code in a function that you can call when needed:

Xrm.Page.data.entity.save();

Save and New

To execute the same functionality from the Save and New button, incorporate the following line in your function:

Xrm.Page.data.entity.save('saveandnew'),

Save and Close

Just like Save and New, we can call the same line of code, and pass a different parameter in order to achieve the Save and Close functionality:

Xrm.Page.data.entity.save('saveandclose'),

See also

In Update Rollup 8 for Dynamics CRM 2011, an issue was fixed that was preventing using scripting to cancel the OnSave event in a recurring appointment. This is documented with the release notes. Additional details are found on the Update Rollup 8 page at http://support.microsoft.com/kb/2600644.

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

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