0
votes

I have a plugin registered for when an account is created or updated, this is registered for the post stage.

The plugin works fine when a user creates or updates an account through the CRM interface, however when an account is created uging the API the plugin fails with the ever helpful 'server was unable to process the request' message. if an account is updated through the api the plugin also works correctly.

anyone have any ideas why?

UPDATE:

here is the create code

  account = new CrmService.account();

                account.ownerid = new CrmService.Owner();
                account.ownerid.Value = new Guid("37087BC2-F2F0-DC11-A856-001E0B617486");
                account.ownerid.type = CrmService.EntityName.systemuser.ToString();

                account.name = model.CompanyName;
                account.address1_line1 = model.Address1;
                account.address1_line2 = model.Address2;
                account.address1_stateorprovince = model.County;
                account.address1_country = model.Country;
                account.address1_city = model.TownCity;
                account.address1_postalcode = model.PostCode;
                account.new_companytype = new CrmService.Picklist();

                switch (model.SmeType)
                {
                    case SmeType.Micro:
                        account.new_companytype.Value = 1;
                        break;
                    case SmeType.Small:
                        account.new_companytype.Value = 2;
                        break;
                    case SmeType.Medium:
                        account.new_companytype.Value = 3;
                        break;
                    default:
                        break;
                }

                account.new_balancesheettotal = new CrmService.CrmMoney();
                account.new_balancesheettotal.Value = preQualModel.BalanceSheetGBP;
                account.revenue = new CrmService.CrmMoney();
                account.revenue.Value = preQualModel.SalesTurnoverGBP;
                if (model.Website != null)
                {
                    account.websiteurl = model.Website.ToString();
                }
                account.numberofemployees = new CrmService.CrmNumber();
                account.numberofemployees.Value = (int)preQualModel.NumEmployees;


                accountGuid = svc.Create(account);
                account.accountid = new CrmService.Key();
                account.accountid.Value = accountGuid;

Here is the plugin code:

public void Execute(IPluginExecutionContext context)
    {
        DynamicEntity entity = null;

        // Check if the InputParameters property bag contains a target
        // of the current operation and that target is of type DynamicEntity.
        if (context.InputParameters.Properties.Contains(ParameterName.Target) &&
           context.InputParameters.Properties[ParameterName.Target] is DynamicEntity)
        {
            // Obtain the target business entity from the input parmameters.
            entity = (DynamicEntity)context.InputParameters.Properties[ParameterName.Target];

            // TODO Test for an entity type and message supported by your plug-in.
            if (entity.Name != EntityName.account.ToString()) { return; }
            // if (context.MessageName != MessageName.Create.ToString()) { return; }

        }
        else
        {
            return;
        }

        if (entity!=null && !entity.Properties.Contains("address1_postalcode"))
        {
            return;
        }

        if (context.Depth > 2)
        {
            return;
        }

        try
        {
            // Create a Microsoft Dynamics CRM Web service proxy.
            // TODO Uncomment or comment out the appropriate statement.

            // For a plug-in running in the child pipeline, use this statement.
            // CrmService crmService = CreateCrmService(context, true);

            // For a plug-in running in the parent pipeline, use this statement.
            ICrmService crmService = context.CreateCrmService(true);

            #region get erdf area from database

            string postCode = entity.Properties["address1_postalcode"].ToString();
            postCode = postCode.Replace(" ", ""); //remove spaces, db stores pcodes with no spaces, users usually enter them, e.g b4 7xg -> b47xg
            string erdfArea = "";

            SqlConnection myConnection = new SqlConnection(@"REDACTED");

            try
            {
                myConnection.Open();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }

            try
            {
                SqlDataReader myReader = null;
                SqlCommand myCommand = new SqlCommand("select ErdfAreaType from dim.Locality WHERE PostCode = '" + postCode+"'",
                                                         myConnection);
                myReader = myCommand.ExecuteReader();
                while (myReader.Read())
                {
                    erdfArea = myReader["ErdfAreaType"].ToString();                        
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }

            try
            {
                myConnection.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }

            #endregion

            entity.Properties["new_erdfarea"] = erdfArea;                

            crmService.Update(entity);

        }
        catch (System.Web.Services.Protocols.SoapException ex)
        {
            throw new InvalidPluginExecutionException(
                String.Format("An error occurred in the {0} plug-in.",
                   this.GetType().ToString()),
                ex);
        }
    }
2
I assume you expect data which is not present during the create with the API. Catch the SoapException which is thrown and take a look at it's Detail property. Also post some of your create and plugin codeccellar

2 Answers

1
votes

Sometimes it can be difficult to see the actual source of an error in plugins. In moments like this tracing is your friend. You can use this tool to enable tracing. When you have the trace files, try to search them for the error you got in your exception. This should tell you more details about what is failing.

0
votes

Turns out this was due to me expecting data that was not there due to some weird behaviour in CRM.

I was taking the dynamicEntity passed to the plugin like so

entity = (DynamicEntity)context.InputParameters.Properties[ParameterName.Target];

But this was missing critical things like accountid. fixed it by using the PostEntityImage entity instead, which had all of the expected data like so

entity = (DynamicEntity)context.PostEntityImages[ParameterName.Target];