Friday, September 28, 2012

How to Restrict Saving of a Record in CRM 2011

First make sure you have the 'Pass execution context as first parameter' checkbox ticked when attaching the onSave Event Handler



In the ValidateData method make sure you have a parameter defined to receive the Execution Object.

function ValidateData(ExecutionObj){
     //Place all your data validation here
     //In case data is not valid call the below method
     // The getEventArgs() method returns an object with methods to manage the Save event.
     // The preventDefault() method cancels the save operation
     ExecutionObj.getEventArgs().preventDefault();
}

Refer this link for more details ...

Tuesday, September 4, 2012

Debugging a Plugin in CRM 2011


  1. Build and register your Plugin as usual.
    Register Plugin
  2. Inside Plugin Registration Tool, you’ll find a button which says Install Profiler, Click it.

    Install Profiler
  3. Once it is done, you will find an entry for Plug-in Profiler beneath your assemblies.
  4. Now, go-to the step for which you want to debug your plugin and press the Profile button.
    Start Profiling
  5. Once done, you will see a tag (Profiled) adjacent to the step.
    Profiled
  6. Now go ahead and execute the plugin i.e. perform the steps in CRM that will trigger this plugin.
  7. If the code has any errors, then CRM will provide you a log file to download; go ahead and download the file. (see screenshot below)
    Error Log
  8. Now, inside Visual Studio, insert Breakpoints wherever you feel and use Attach to Process button and attach the code to Plugin Registration Tool exe.
    Note: Initially you will see that VS cannot find symbols so breakpoints will not be hit.
    Attach to Process
  9. Now, go back to Plugin Registration Tool and Select the Step which you profiled earlier hit Debug button.
    Debug
  10. A new Dialog-box will be presented to you, refer to the necessary files and hit Start Plug-in Execution button.
    Start Plug-in Execution
  11. Wait for a few seconds and you will start to see the following in Plug-in Traces window from Debug screen.
    Plug-in Trace
  12. Now, switch to Visual Studio and you will see that your first Breakpoint has been hit.
    Breakpoint