The original problem can be viewed here: https://community.dynamics.com/crm/f/117/t/179391
I now have a plug-in that works properly on a 7 field test form.
I created a new plug-in that is designed to work the same way on my production form, but that plug in doesn't work, which was unexpected given the success of the original plug-in.
The differences between the test and the production forms are:
- Test Form - has 7 fields, 6 are textboxes and one is a two option set, no JavaScript.
- On Test, All 7 fields are involved in the context parameters area of the code
- The data types are the same, field names and entity names are different
- Production Form - has over 30 fields of many different types and a ton of JavaScript.
- On production, I only put the same 6 or 7 fields in the context parameters area of the new plug-in since that's all I am worried about interacting with.
- The data types are the same, field names and entity names are different.
Do I need to include all the fields in the context parameters area, or only the fields I am working with and manipulating?
What could I zero in on to find the problem?
If I disable JavaScript, the form wont work properly and it will become a problem interacting with it so ideally if I can focus on something specific, that would be the best route for me to take.
Here is the plug-in code form the test form:
namespace AloyeLicenseGen.Plugins
{
using System;
using System.ServiceModel;
using Microsoft.Xrm.Sdk;
/// <summary>
/// PostTestLicenseCreate Plugin.
/// </summary>
public class PostTestLicenseCreate: Plugin
{
/// <summary>
/// Initializes a new instance of the <see cref="PostTestLicenseCreate"/> class.
/// </summary>
public PostTestLicenseCreate()
: base(typeof(PostTestLicenseCreate))
{
base.RegisteredEvents.Add(new Tuple<int, string, string, Action<LocalPluginContext>>(40, "Create", "new_s_testlicense", new Action<LocalPluginContext>(ExecutePostTestLicenseCreate)));
// Note : you can register for more events here if this plugin is not specific to an individual entity and message combination.
// You may also need to update your RegisterFile.crmregister plug-in registration file to reflect any change.
}
/// <summary>
/// Executes the plug-in.
/// </summary>
/// <param name="localContext">The <see cref="LocalPluginContext"/> which contains the
/// <see cref="IPluginExecutionContext"/>,
/// <see cref="IOrganizationService"/>
/// and <see cref="ITracingService"/>
/// </param>
/// <remarks>
/// For improved performance, Microsoft Dynamics CRM caches plug-in instances.
/// The plug-in's Execute method should be written to be stateless as the constructor
/// is not called for every invocation of the plug-in. Also, multiple system threads
/// could execute the plug-in at the same time. All per invocation state information
/// is stored in the context. This means that you should not use global variables in plug-ins.
/// </remarks>
protected void ExecutePostTestLicenseCreate(LocalPluginContext localContext)
{
if (localContext == null)
{
throw new ArgumentNullException("localContext");
}
// TODO: Implement your custom Plug-in business logic.
//Get Execution Context from service provider
IPluginExecutionContext context = localContext.PluginExecutionContext;
IOrganizationService service = localContext.OrganizationService;
//Input paramaters connection contains all data passed in the message request
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
//Obtain all the input paramaters
Entity new_s_testlicense = (Entity)context.InputParameters["Target"];
try
{
if (new_s_testlicense.Contains("new_field1") && new_s_testlicense.Contains("new_field2") && new_s_testlicense.Contains("new_field3") && new_s_testlicense.Contains("new_autonumber") && new_s_testlicense.Contains("new_testlicenseid") && new_s_testlicense.Contains("new_fieldx"))
if ((new_s_testlicense["new_field1"].ToString() == "G") || (new_s_testlicense["new_field1"].ToString() == "B"))
{
try
{
var f1 = new_s_testlicense["new_field1"].ToString();
var f2 = new_s_testlicense["new_field2"].ToString();
var f3 = new_s_testlicense["new_field3"].ToString();
var fx = new_s_testlicense["new_fieldx"].ToString();
var fa = new_s_testlicense["new_autonumber"].ToString();
var result = f1 + "-" + f2 + "-" + fa + "-" + f3;
new_s_testlicense["new_testlicenseid"] = result;
service.Update(new_s_testlicense);
}
catch (Exception ex) { throw new Exception("Error in PreValidate Plug-In: " + ex.Message); }
}
}
catch (Exception e1) { throw new Exception("Error in PreValidate Plug-In Entity Context Acquisition: " + e1.Message); }
}
}
}
}
Here is the plug-in code from the production form:
namespace SetLicenseIDBasedOnPreReqData.Plugins
{
using System;
using System.ServiceModel;
using Microsoft.Xrm.Sdk;
/// <summary>
/// GenerateLicenseIDPostOP Plugin.
/// </summary>
public class GenerateLicenseIDPostOP: Plugin
{
/// <summary>
/// Initializes a new instance of the <see cref="GenerateLicenseIDPostOP"/> class.
/// </summary>
public GenerateLicenseIDPostOP()
: base(typeof(GenerateLicenseIDPostOP))
{
base.RegisteredEvents.Add(new Tuple<int, string, string, Action<LocalPluginContext>>(40, "Create", "new_s_licenseinformation", new Action<LocalPluginContext>(ExecuteGenerateLicenseIDPostOP)));
// Note : you can register for more events here if this plugin is not specific to an individual entity and message combination.
// You may also need to update your RegisterFile.crmregister plug-in registration file to reflect any change.
}
/// <summary>
/// Executes the plug-in.
/// </summary>
/// <param name="localContext">The <see cref="LocalPluginContext"/> which contains the
/// <see cref="IPluginExecutionContext"/>,
/// <see cref="IOrganizationService"/>
/// and <see cref="ITracingService"/>
/// </param>
/// <remarks>
/// For improved performance, Microsoft Dynamics CRM caches plug-in instances.
/// The plug-in's Execute method should be written to be stateless as the constructor
/// is not called for every invocation of the plug-in. Also, multiple system threads
/// could execute the plug-in at the same time. All per invocation state information
/// is stored in the context. This means that you should not use global variables in plug-ins.
/// </remarks>
protected void ExecuteGenerateLicenseIDPostOP(LocalPluginContext localContext)
{
if (localContext == null)
{
throw new ArgumentNullException("localContext");
}
// TODO: Implement your custom Plug-in business logic.
//Get Execution Context from service provider
IPluginExecutionContext context = localContext.PluginExecutionContext;
IOrganizationService service = localContext.OrganizationService;
//Input paramaters connection contains all data passed in the message request
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
//Obtain all the input paramaters
Entity new_s_licenseinformation = (Entity)context.InputParameters["Target"];
try {
if (new_s_licenseinformation.Contains("new_licensenumbgtype") && new_s_licenseinformation.Contains("new_licenseyear") && new_s_licenseinformation.Contains("new_licensidcode") && new_s_licenseinformation.Contains("new_licensenumtype") && (new_s_licenseinformation.Contains("new_licenseinfoid")))
{
try
{
//get the current values of the licnese fields in the form
var MainLicenseID = new_s_licenseinformation["new_licenseinfoid"].ToString();
var LicenseLType = new_s_licenseinformation["new_licensenumbgtype"].ToString();
var LicenseYear = new_s_licenseinformation["new_licenseyear"].ToString();
var LicenseAutoGenNum = new_s_licenseinformation["new_licenseidcode"].ToString();
var LicenseLNType = new_s_licenseinformation["new_licensenumtype"].ToString();
var result = LicenseLType + "-" + LicenseYear + "-" + "-" + LicenseAutoGenNum + "-" + LicenseLNType;
new_s_licenseinformation["new_licenseinfoid"] = result;
service.Update(new_s_licenseinformation);
}
catch (Exception ex) { throw new Exception("Error in Post-Operation Plug-In: " + ex.Message); }
}
}
catch (Exception ex) { throw new Exception("Error in Post-Operation Plug-In: " + ex.Message); }
}
}
}
}
Any help would be greatly appreciated!