locked
My CRM 2011 prevalidation Create plugin does not fire RRS feed

  • Question

  •     <Solution Assembly="acm.mscrm2011.psa.HRBelgium.Plugins.dll" Id="31e17c3b-f322-e411-ade2-005056a03018" IsolationMode="Sandbox" SourceType="Database">
          <PluginTypes>
            <Plugin Description="Plug-in to PreValidateFamilielidCreate" FriendlyName="PreValidateFamilielidCreate" Name="acm.mscrm2011.psa.HRBelgium.Plugins.PreValidateFamilielidCreate" Id="e7b9d859-d526-e411-ade2-005056a03018" TypeName="acm.mscrm2011.psa.HRBelgium.Plugins.PreValidateFamilielidCreate">
              <Steps>
                <clear />
                <Step CustomConfiguration="" Name="PreValidateFamilielidCreate" Description="Pre-Validation of Familielid Create" Id="e8b9d859-d526-e411-ade2-005056a03018" MessageName="Create" Mode="Synchronous" PrimaryEntityName="acm_familielid" Rank="1" SecureConfiguration="" Stage="PreOutsideTransaction" SupportedDeployment="ServerOnly">
                  <Images />
                </Step>
              </Steps>
            </Plugin>
          </PluginTypes>
        </Solution>

    This is the Plugins Solution section of my RegisterFile.crmregister configuration file as it appears in my Visual Studio 2012 project.

    public class PreValidateFamilielidCreate : IPlugin
        {
            /// <summary>
            /// Executes the plug-in.
            /// </summary>
            /// <param name="serviceProvider"/>
            public void Execute(IServiceProvider serviceProvider)
            {
                if (serviceProvider == null)
                {
                    throw new ArgumentNullException("serviceProvider");
                }
    
                IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
                ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
                tracingService.Trace("Entered PreValidation plugin on Create of Familielid.");
                //if (!context.InputParameters.Contains("Target") || !(context.InputParameters["Target"] is Entity))
                //{
                //    tracingService.Trace("Target does not exist or is not an Entity.");
                //    return;
                //}
                Entity familieLid = (Entity)context.InputParameters["Target"];
                //if (!familieLid.Contains("acm_voornaam") || !familieLid.Contains("acm_familienaam"))
                //{
                //    tracingService.Trace("Target does not contain acm_voornaam or acm_familienaam.");
                //    return;
                //}
                string fullName = string.Format("{0} {1}", familieLid.GetAttributeValue<string>("acm_voornaam"), familieLid.GetAttributeValue<string>("acm_familienaam"));
                tracingService.Trace("Autogenerated Name: {0}.", fullName);
                familieLid["acm_name"] = fullName;
                tracingService.Trace("Assigned autogenerated name to familielid.");
                context.InputParameters["Target"] = familieLid;
                tracingService.Trace("Name assigned to familielid.");
            }
        }

    This is the plugin I've written. It serves to automatically assign a correct name to a custom entity based on 2 other names. The entity is part of an extention to assistance HRM.

    This is the registration screen in the plugin Registration tool. The plugin was automatically deployed through a Visual Studio 2012 CRM SDK solution. the plugin and the step both appear in the CRM 2011 solution list.


    The problem I'm faced with is that the plugin just won't fire. To be precise: the Execute does not launch. I've even tried having it throw an exception as the very first line, but it won't launch. I have tried resetting IIS, and I've tried updating it both through the SDK and the Plugin Registration tool. Above are the configuration displays for both of them. I cannot figure out why it's not firing.

    My testing method is to open a new record of the entity, update acm_voornaam and acm_familienaam and then click save. I get the error "You need to enter a value in acm_name". Shouldn't a pre-validation plugin happen before this validation?

    Monday, August 18, 2014 2:24 PM

All replies

  • I am thinking right now that the error message you are getting about the acm_name field is because this field is on the form and is required?  If that is the case the error is coming from the UI before the record is even submitted for save.  Thus your plugin won't be hit.  If the field is on the form as required and you only wish to populate it programmatically, then set the field as 'read-only'.  The UI will not validate against read-only fields.
    Monday, August 18, 2014 3:17 PM
  • I am thinking right now that the error message you are getting about the acm_name field is because this field is on the form and is required?  If that is the case the error is coming from the UI before the record is even submitted for save.  Thus your plugin won't be hit.  If the field is on the form as required and you only wish to populate it programmatically, then set the field as 'read-only'.  The UI will not validate against read-only fields.

    I know that that's not the issue. I've registered the plugin as pre-validation, which means it happens between pressing save and the field validation. I have made similar plugins in the past and I know that it should work. The acm_name field is the primary key name field automatically generated by CRM, so it has to be filled.

    Tuesday, August 19, 2014 6:59 AM
  • I have done some extra testing. Apparently, the plugin (which I registered as pre-validation) decides to happen pre-operation. I don't know why this happens, but it should work pre-validation. I also checked another CRM development organization where I had the same situation (prevalidation plugin to generate a name), but that one works just fine. The only difference is that the failed one has been added through the VS 2012 SDK, while the other one was added through the plugin registration tool. I have no idea why it fails. For now, I've set acm_name to not required.

    edit: I've checked my other CRM organization again. Apparently, the name field there is also set to not required.

    Tuesday, August 19, 2014 11:47 AM
  • Client-side validation (such as required fields) happens before anything is sent to the server, and so it happens before any plugin fires. In this context, 'pre-validation' refers to server-side validation, not client-side validation

    Microsoft CRM MVP - http://mscrmuk.blogspot.com/ http://www.excitation.co.uk

    Tuesday, August 19, 2014 3:14 PM
    Moderator