pointer
pointer

Reputation: 315

Generic Error with MS CRM Plugin

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.

Upvotes: 2

Views: 1385

Answers (2)

pointer
pointer

Reputation: 315

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.

Upvotes: 2

Ranjith
Ranjith

Reputation: 11

Please check it like this

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

some times that doesn't work properly.

Upvotes: 1

Related Questions