web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Plug-In Not Working as Expected - Any suggestions to diagnose or see into what might be stopping it?

(0) ShareShare
ReportReport
Posted on by 1,589

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!

*This post is locked for comments

I have the same question (0)
  • Aiden Kaskela Profile Picture
    19,696 on at

    Hi Jim,

    Are you getting an error when you run it, or does it just not work the way you expect?

    Thanks,

     Aiden

  • ACECORP Profile Picture
    1,589 on at

    No error pops up on the screen. I am trying to find something in a trace log now. As for the behavior, it looks like the new plug-in didn't execute. This new plug-in, coded in its own project, for the new_s_licenseinformation entity, didn't do what a similar plug-in that was coded for a test entity did, even though the code is the same (except for form and entity specific changes).

  • Suggested answer
    Aiden Kaskela Profile Picture
    19,696 on at

    Hi Jim,

    Everything looks good in your code, the only thing I'm concerned about is that it looks like you're creating an infinite loop (though it's the same in the test environment so I'm not sure why it wouldn't be a problem there). In your posted code, you should compare the values to make sure there's an actual change before saving the value like this:

    if (!new_s_testlicense.Contains("new_testlicenseid") || 
        new_s_testlicense["new_testlicenseid"].ToString() != result)
    {
        new_s_testlicense["new_testlicenseid"] = result;
        service.Update(new_s_testlicense);
    }
    


    Besides that I don't see anything different in the code. Did you double check the plugin is registered with all the same properties on both environments?

    Thanks,

      Aiden

  • Aiden Kaskela Profile Picture
    19,696 on at

    Jim,

    I think the most likely culprits are either that the plugin isn't firing at all, or it's running but  it doesn't have the properties you're expecting. You might want to add logging throughout your plugin to see the logical path it's following (one easy/short term way is to crate Task records with the details in the subject or description).

    Thanks,

     Aiden

  • ACECORP Profile Picture
    1,589 on at

    The register file contains the same type of property info between the two:

    PRODUCTION PLUGIN / PRODUCTION FORM
    
    <Step CustomConfiguration="" Name="GenerateLicenseIDPostOP" Description="GenerateLicenseIDPostOP" Id="714e4237-2978-e511-a47d-000c29d70c71" MessageName="Create" Mode="Synchronous" PrimaryEntityName="new_s_licenseinformation" Rank="999" SecureConfiguration="" Stage="PostOutsideTransaction" SupportedDeployment="ServerOnly">
             
    TEST PLUGIN / TEST FORM
    
    <Step CustomConfiguration="" Name="PostTestLicenseCreate" Description="Post-Operation of TestLicense Create" Id="39332a94-1378-e511-a47d-000c29d70c71" MessageName="Create" Mode="Synchronous" PrimaryEntityName="new_s_testlicense" Rank="999" SecureConfiguration="" Stage="PostOutsideTransaction" SupportedDeployment="ServerOnly">
    


    I am still going through trace logs. I'll post what I find.


  • ACECORP Profile Picture
    1,589 on at

    Here is something out of the sandbox host log-- unable to load plugin assembly --

    >----- dump exception/fault tree (TryConvertToFaultExceptionInternal: before) -----
    >>>>>> EXCEPTION: 0
    >Type: Microsoft.Crm.CrmException
    >ErrorCode: 0x80044191: ErrorCodes.UnableToLoadPluginAssembly
    >Message (27): Assembly should be provided
    >StackTrace: present: 46AF7B3E
    >no PluginTrace
    >InnerException <null>
    ><<<<< EXCEPTION: 0
    >
    [2015-10-21 17:11:23.381] Process:Microsoft.Crm.Sandbox.HostService |Organization:00000000-0000-0000-0000-000000000000 |Thread:  133 |Category: Sandbox |User: 00000000-0000-0000-0000-000000000000 |Level: Info |ReqId:  | ExceptionConverter.ConvertToFault  ilOffset = 0x57
     at ExceptionConverter.ConvertToFault(Exception exception)  ilOffset = 0x57
     at Excep

  • Aiden Kaskela Profile Picture
    19,696 on at

    Thanks for the update Jim.

    I didn't realize you were only running this plugin on create of a record, which is why there's no infinite loop.

  • Aiden Kaskela Profile Picture
    19,696 on at

    Have you tried unregistering the assembly and steps and re-registering them?

  • ACECORP Profile Picture
    1,589 on at

    Yes, but I didn't do that quite right. Apparently I need to clear the GUID out of the registerfile.cmregister so it can re-register. I have to run out to a meeting but will go down that road when I return. I have a feeling that's the direction towards a fix.

  • Aiden Kaskela Profile Picture
    19,696 on at

    Sorry i can't help too much with this, I'm not sure all the steps to take when you're registering through a file (I usually use the MS plugin registration util for managing plugin steps).

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans