2
votes

I am trying to run a sample plugin for MS CRM. but I am getting following error:

An error occurred. Contact a system administrator or refer to the Microsoft Dynamics CRM SDK troubleshooting guide.

here is the code:

public void Execute(IServiceProvider serviceProvider)
    {

        // Obtain the execution context from the service provider.
        IPluginExecutionContext context = (IPluginExecutionContext)
        serviceProvider.GetService(typeof(IPluginExecutionContext));


        // The InputParameters collection contains all the data passed in the message request.
        if (context.InputParameters.Contains("account") &&
        context.InputParameters["account"] is Entity)
        {
            // Obtain the target entity from the input parmameters.
            Entity entity = (Entity)context.InputParameters["account"];



            try
            {
                //check if the account number exist

                if (entity.Attributes.Contains("account number") == false)
                {

                    //create a task
                    Entity task = new Entity("task");
                    task["subject"] = "Account number is missing";
                    task["regardingobjectid"] = new EntityReference("account", new Guid(context.OutputParameters["id"].ToString()));

                    //adding attribute using the add function
                    // task["description"] = "Account number is missng for the following account. Please enter the account number";
                    task.Attributes.Add("description", "Account number is missng for the following account. Please enter the account number");



                    // Obtain the organization service reference.
                    IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                    IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);


                    // Create the task in Microsoft Dynamics CRM.
                    service.Create(task);


                }
            }

            catch (FaultException ex)
            {
                throw new InvalidPluginExecutionException("An error occurred in the plug-in.", ex);
            }

        }

    }
}//end class

This is an example code, and I have verified that all the entities and fields which are utilized by this plugin are defined and are at there places. but I am continuously getting this Business Error.

2
there are several logic errors in your code (for example you need to look for Target property inside the InputParameters, not account) but because of these errors your current code will execute nothing, my best guess is that your target framework is 4.5 instead of 4.0. In addition, you can try to run this sample: msdn.microsoft.com/en-us/library/gg594416.aspxGuido Preite

2 Answers

2
votes

I found the Solution: Instead of explicitly mentioning "account", we have to use:

Entity entity = (Entity)context.InputParameters["Target"];

The second reason for error was the restriction inside CRM which was not allowing creation of new accounts. Works fine when used to create new "contact."

Thanks a lot everyone for help.

1
votes

Please check it like this

Entity entity = context.InputParameters["account"] as Entity;

some times that doesn't work properly.