locked
Auto Numbering of account number RRS feed

  • Question

  • HI,

    I am trying to auto generate the Account number ,by using the code in sdk,but i am unable to do it,

    i am getting the following error

    "An error Occured.contact system administrator or refer to ms crm sdk for troubleshooting guide"

    i am using following code

    using System;
    using Microsoft.Xrm.Sdk;

    namespace AutogeneratingAccountnumber
    {
        public class Class1:IPlugin
        {
            public void Execute(IServiceProvider serviceprovider)
            {
                //obtain the execution context from serviceprovider
                Microsoft.Xrm.Sdk.IPluginExecutionContext context = (IPluginExecutionContext)serviceprovider.GetService(typeof(IPluginExecutionContext));

                //The input parameters contain all the data passed in the message request
                if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
                {
                    //obtian the target Entity from the input parameters
                    Entity entity = (Entity)context.InputParameters["Target"];
                    {
                        // Verify that the target Entity Represents account
                        //If, not the plugin was not Registered properly
                        if (entity.LogicalName == "account")
                        {
                            IOrganizationServiceFactory servicefactory = (IOrganizationServiceFactory)serviceprovider.GetService(typeof(IOrganizationServiceFactory));
                            IOrganizationService organizatonservice = servicefactory.CreateOrganizationService(context.UserId);

                            if (entity.Attributes.Contains("accountnumber") == true)
                            {
                                Random regen = new Random();
                                entity["accountnumber"] = regen.Next().ToString();
                            }
                            else
                            {
                                throw new InvalidPluginExecutionException("This plug-in is not configured proberly");

                            }
                        }

                    }
                }
               
            }
        }
    }

    I have signed the assembly..

    Please guide me.

    Thursday, May 3, 2012 12:06 PM

Answers

All replies