locked
Debugging a plug in RRS feed

  • Question

  • I am still trying to get my autonumber plugin going with out much luckSad

    I have downloaded the new sdk & using the improved plugin registration tool.

    I have also pointed the solution to the new sdk.

    I have also reviewed the debugging a plugin but thats not telling an amateur like me.

     

    I have managed to attach to the w3p service & create a lead, problem is it just saves the lead without adding the next number.

     

    The only thing that is appearing in the debugger is "The thread 0x79c has exited with code 0 (0x0)."

     

    Ive also noticed error message in the event log on the crm server

     

    Web Service Plug-in failed in OrganizationId: <guid>; SdkMessageProcessingStepId: <guid>; ; EntityName: lead; Stage: 50; MessageName: Create; AssemblyName: AutoNumber.clsAutoNumber, AutoNumber, Version=1.0.0.0, Culture=neutral, PublicKeyToken=xxxxxxxx; ClassName: AutoNumber.clsAutoNumber; Exception: Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object.
       at AutoNumber.clsAutoNumber.Execute(IPluginExecutionContext context) in E:\CRM\LeadAutoNumber\AutoNumber\clsAutoNumber.cs:line 66
       at Microsoft.Crm.Extensibility.PluginStep.Execute(PipelineExecutionContext context)

     

    this line in the code is

     

    entity.Properties.Add(LeadNumber);

     

    Thanks in advance for any help

    Saturday, March 29, 2008 5:24 AM

Answers

  • Hi,

     

    I went through your code and the only odd thing that I've seen it's that this line " context.InputParameters.Properties["Target"] = entity; " that I think it's not necessary as the entity you are using is already a reference to that Property of the InputParameters.

     

    It's really odd. Are you sure you are registering this plugin as pre plugin? Anyway, in the sdk folders you have a example of a pre pllugin in the account entity if you want to have some code references. I will try to use your code in my test VPC as soon as I have some spare time and see if I discover something.

     

    BTW. If your are looking to an autonumbering in the lead entities you should know that this code has a little problem caused by the fact that the CRM Platform by default makes some kind of pagination in the querys. Look at the properties of BusinessEntityCollection and you will find "HasMoreRecords" and "PaginCookie", that are using to retrieve the next page of results. So, If your reach the max number of results with your leads this code will start to assign the same number for all the leads. In CRM 3.0 this can be deactived/configured using a registry key http://msdn2.microsoft.com/en-us/library/aa682811.aspx that is not created by default, but in CRM 4.0 this documentation is not available yet even I guess that it will be similar Smile

     

    There is also another main concern you should take into acount about this code, is the possibility of getting duplicate numbers by concurrence problems as two create lead operations can take place at the same time. Yous should think on how you can avoid this controlling the concurrency with .Net techniques. Maybe create a numbering web service and controlling there the concurrence as in the plugins code could be more difficult... it's only an idea Smile

     

    Hope it helps,

    Marco

     

    Tuesday, April 1, 2008 11:11 AM
    Moderator

All replies

  • Hi,

     

    I think that you need to review your code to find why that sentence produces a Null Pointer exception. The chances are that the entity variable or his property "Properties" are null.

     

    Have you try the setp by step guide in the sdk to debug your plugin? What problem are you facing to debug your plugin? Basically you have to compile the code, then register the plugin and copy the .pdb to the /bin/assembly directory and attach to the w3wp process(es) running in the server.

     

    Hope it helps,

     

    Marco

    Saturday, March 29, 2008 9:27 PM
    Moderator
  • Thanks Marco,

     

    Ive done all that but i cant work out what the problem is.

    Sunday, March 30, 2008 10:37 AM
  • Here is the code

     

    using System;
    using System.Collections.Generic;
    using System.Text;
    using Microsoft.Crm.Sdk;
    using Microsoft.Crm.SdkTypeProxy;
    using Microsoft.Crm.Sdk.Query;
    using System.Web.Services;   

    namespace AutoNumber
    {
        public class clsAutoNumber : IPlugin
        {
            public void Execute(IPluginExecutionContext context)
            {
                // Do something here.
                DynamicEntity entity = null;

                // Check whether the input parameters property bag contains a target
                // of the create operation and that target is of type DynamicEntity.
                if (context.InputParameters.Properties.Contains("Target") &&
                   context.InputParameters.Properties["Target"] is DynamicEntity)
                {
                    // Obtain the target business entity from the input parmameters.
                    entity = (DynamicEntity)context.InputParameters.Properties["Target"];
                    // Verify that the entity represents an account.
                    if (entity.Name != EntityName.lead.ToString())
                    {
                        return;
                    }
                }//if
                else
                {
                    return;
                }//else
                try
                {
                    CrmNumber crmNumber = new CrmNumber();
                    crmNumber.Value = 0;
                    //Set new_leadreference to Next Number
                    //Retrive the last Id from DB
                    /////////////////////////////////////////////////
                    ICrmService service = (ICrmService)context.CreateCrmService(true);
                    QueryExpression query = new QueryExpression();
                    query.EntityName = EntityName.lead.ToString();

                    AllColumns cols = new AllColumns();
                    query.ColumnSet = cols;

                    BusinessEntityCollection reterive = service.RetrieveMultiple(query);
                    if (reterive != null)
                    {
                        if (reterive.BusinessEntities.Count > 0)
                        {
                            crmNumber.Value = reterive.BusinessEntities.Count + 231031;                       
                        }
                        else
                        {
                            crmNumber.Value = 231031;
                        }
                    }
                    /////////////////////////////////////////////////
                    if (crmNumber.Value > 0)
                    {
                        //new_leadreference
                        CrmNumberProperty LeadNumber = new CrmNumberProperty("new_leadreference", crmNumber);
                        entity.Properties.Add(LeadNumber);
                        context.InputParameters.Properties["Target"] = entity;
                    }
                }//try
                catch(System.Web.Services.Protocols.SoapException ex)
                {
                    throw new InvalidPluginExecutionException(
                      "An error occurred in the Lead auto number plug-in.", ex);           
                }//catch
            }//Execute
        }//clsAutoNumber
    }

    Sunday, March 30, 2008 10:48 AM
  • In the debugger i have some breakpoints & they are showing the value that should be inserted just above //new_leadreference.

    in the debugger window it just says The thread 0x1690 has exited with code 0 (0x0).

    Sunday, March 30, 2008 10:59 AM
  • Ive attached to the process & can get all the way to the line

    //new_leadreference

    the output is showing the correct info counting the number leads etc

     

    but its just not inserting into the lead. its just blank (no error messages)

     

    any help is greatly appreciated

    Tuesday, April 1, 2008 9:54 AM
  • Hi,

     

    I went through your code and the only odd thing that I've seen it's that this line " context.InputParameters.Properties["Target"] = entity; " that I think it's not necessary as the entity you are using is already a reference to that Property of the InputParameters.

     

    It's really odd. Are you sure you are registering this plugin as pre plugin? Anyway, in the sdk folders you have a example of a pre pllugin in the account entity if you want to have some code references. I will try to use your code in my test VPC as soon as I have some spare time and see if I discover something.

     

    BTW. If your are looking to an autonumbering in the lead entities you should know that this code has a little problem caused by the fact that the CRM Platform by default makes some kind of pagination in the querys. Look at the properties of BusinessEntityCollection and you will find "HasMoreRecords" and "PaginCookie", that are using to retrieve the next page of results. So, If your reach the max number of results with your leads this code will start to assign the same number for all the leads. In CRM 3.0 this can be deactived/configured using a registry key http://msdn2.microsoft.com/en-us/library/aa682811.aspx that is not created by default, but in CRM 4.0 this documentation is not available yet even I guess that it will be similar Smile

     

    There is also another main concern you should take into acount about this code, is the possibility of getting duplicate numbers by concurrence problems as two create lead operations can take place at the same time. Yous should think on how you can avoid this controlling the concurrency with .Net techniques. Maybe create a numbering web service and controlling there the concurrence as in the plugins code could be more difficult... it's only an idea Smile

     

    Hope it helps,

    Marco

     

    Tuesday, April 1, 2008 11:11 AM
    Moderator
  • Thanks Marco

     

    Appreciate it

    Tuesday, April 1, 2008 8:37 PM
  • I was registering at the post stage

     

    now working beautifully

     

    thank you so much Marco & all contributors

    Tuesday, April 1, 2008 8:59 PM
  • I have a similar problem.. I also observed a message like yours in the Event Viewer. The thing is this: although mu plugin is correctly set, I get an error after it runs.. that full of information error saying "An error has occured. Try again.. " etc..

    I debug my code successfully and there are no errors in it. It's just that, after the code from plugin runs, the crm blows that error in my face. I have no idea what is its problem..

    I would really appreciate some help on this, as I'm clueless..

    Thank you,
    Florin


    Edit:
     Turns out that I was using a dynamic entity so I didn't see the correct type of a field I was updating, i.e. I was using a CrmDecimalProperty for a CrmMoney field.. my bad.


    Thursday, May 8, 2008 4:13 PM