Добрый день!
У меня возникла схожая проблема при работе плагина для CRM 2011 Online.
Плагин зарегистрирован для message = create, entity = salesorder, execution mode = synchronous, post-operation.
Когда вручную создается новый salesorder, то плагин успешно отрабатывает, но если salesorder создается из sales opportunity c помощью стандартного функционала, то возникает ошибка "Incorrect atribute value Type.Int32".
Путем отладки удалось установить, что ошибка возникает на последней строке кода
Код:
service.Update(updateAccount);
С чем это может быть связано?
Код плагина:
Код:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
// Microsoft Dynamics CRM namespace(s)
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using System.ServiceModel;
//using Microsoft.Xrm.Sdk.Client;
namespace Microsoft.Crm.Sdk.Samples
{
public class AccountAmounts : IPlugin
{
//<snippetAccountNumberPlugin2>
public void Execute(IServiceProvider serviceProvider)
{
//IPluginExecutionContext context2 = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
ITracingService tracer = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
if (tracer == null)
throw new InvalidPluginExecutionException("Failed to retrieve the tracing service.");
try
{
// Obtain the execution context from the service provider.
Microsoft.Xrm.Sdk.IPluginExecutionContext context = (Microsoft.Xrm.Sdk.IPluginExecutionContext)
serviceProvider.GetService(typeof(Microsoft.Xrm.Sdk.IPluginExecutionContext));
// The InputParameters collection contains all the data passed in the message request.
if (context.InputParameters.Contains("Target") &&
context.InputParameters["Target"] is Entity)
{
IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = factory.CreateOrganizationService(context.UserId);
// Obtain the target entity from the input parmameters.
Entity entity = (Entity)context.InputParameters["Target"];
//</snippetAccountNumberPlugin2>
// Verify that the target entity represents an account.
// If not, this plug-in was not registered correctly.
if (entity.LogicalName == "salesorder")
{
ColumnSet cols = new ColumnSet();
cols.AddColumns(new string[] { "salesorderid", "customerid", "steer_completed", "statuscode", "totalamount", "steer_budgetsum" });
Entity _order = service.Retrieve(entity.LogicalName, entity.Id, cols);
if (_order.Contains("totalamount") || _order.Contains("steer_budgetsum"))
{
EntityReference _customer_ref = (EntityReference)_order.Attributes["customerid"];
// Query using ConditionExpression
ConditionExpression condition1 = new ConditionExpression();
condition1.AttributeName = "customerid";
condition1.Operator = ConditionOperator.Equal;
condition1.Values.Add(_customer_ref.Id);
ConditionExpression condition2 = new ConditionExpression();
condition2.AttributeName = "statuscode";
condition2.Operator = ConditionOperator.Equal;
condition2.Values.Add(100001); //fullført
ConditionExpression condition3 = new ConditionExpression();
condition3.AttributeName = "statuscode";
condition3.Operator = ConditionOperator.Equal;
condition3.Values.Add(100002); //fullført
ConditionExpression condition4 = new ConditionExpression();
condition4.AttributeName = "steer_startdato";
condition4.Operator = ConditionOperator.LessEqual;
condition4.Values.Add(DateTime.Now);
ConditionExpression condition5 = new ConditionExpression();
condition5.AttributeName = "steer_forfalsdato";
condition5.Operator = ConditionOperator.GreaterEqual;
condition5.Values.Add(DateTime.Now);
FilterExpression filter1 = new FilterExpression();
filter1.Conditions.Add(condition1);
FilterExpression filter2 = new FilterExpression();
filter2.Conditions.Add(condition2);
//filter2.Conditions.Add(condition3);
FilterExpression filter3 = new FilterExpression();
filter3.Conditions.Add(condition4);
FilterExpression filter4 = new FilterExpression();
filter4.Conditions.Add(condition5);
QueryExpression query = new QueryExpression("salesorder");
query.ColumnSet.AddColumns(new string[] { "salesorderid", "totalamount", "steer_budgetsum" });
query.Criteria.AddFilter(filter1);
query.Criteria.AddFilter(filter2);
query.Criteria.AddFilter(filter3);
query.Criteria.AddFilter(filter4);
EntityCollection result1 = service.RetrieveMultiple(query);
decimal totalamount = 0;
decimal totalbudgetamount = 0;
foreach (Entity a in result1.Entities)
{
if (a.Contains("totalamount"))
{
totalamount += ((Money)a.Attributes["totalamount"]).Value;
}
if (a.Contains("steer_budgetsum"))
{
totalbudgetamount += ((Money)a.Attributes["steer_budgetsum"]).Value;
}
}
//Записываем полученные значения в поля таблицы Account
Entity updateAccount = new Entity("account");
EntityReference _customer_ref2 = (EntityReference)_order.Attributes["customerid"];
updateAccount.Attributes.Add("accountid", _customer_ref2.Id);
if (totalamount != 0)
{
updateAccount.Attributes.Add("steer_totalpurchase", new Money(totalamount));
if (totalamount >= 10000000)
{
updateAccount.Attributes.Add("steer_customertype", 548850000);
}
if (totalamount < 1000000 && totalamount >= 2500000)
{
updateAccount.Attributes.Add("steer_customertype", 548850001);
}
if (totalamount <= 1000001 && totalamount >= 2500000)
{
updateAccount.Attributes.Add("steer_customertype", 548850002);
}
if (totalamount <= 500001 && totalamount >= 1000000)
{
updateAccount.Attributes.Add("steer_customertype", 548850003);
}
if (totalamount <= 50001 && totalamount >= 500000)
{
updateAccount.Attributes.Add("steer_customertype", 548850004);
}
if (totalamount <= 50000)
{
updateAccount.Attributes.Add("steer_customertype", 548850005);
}
}
if (totalbudgetamount != 0)
{
updateAccount.Attributes.Add("steer_totalbudgetamount", new Money(totalbudgetamount));
}
service.Update(updateAccount);
}
}
}
}
catch (Exception e)
{
tracer.Trace("Received exception: {0}", e);
throw new InvalidPluginExecutionException(e.Message);
}
}
}
}