Подчистил код плагина. Попробуйте это:
Код:
using System;
using System.Collections.Generic;
using Microsoft.Win32;
using Microsoft.Crm.Sdk.Query;
// Microsoft Dynamics CRM namespaces
using Microsoft.Crm.Sdk;
using Microsoft.Crm.SdkTypeProxy;
using Microsoft.Crm.SdkTypeProxy.Metadata;
using System.Text;
namespace Crm.Plugins
{
public class MyPlugin : IPlugin
{
private string _secureInformation;
private string _unsecureInformation;
public MyPlugin(string unsecureInfo, string secureInfo)
{
_secureInformation = secureInfo;
_unsecureInformation = unsecureInfo;
}
public void Execute(IPluginExecutionContext context)
{
DynamicEntity entity = null;
if (context.InputParameters.Properties.Contains(ParameterName.Target) && context.InputParameters.Properties[ParameterName.Target] is DynamicEntity)
{
entity = (DynamicEntity)context.InputParameters.Properties[ParameterName.Target];
}
else
{
return;
}
try
{
ICrmService crmService = context.CreateCrmService(true);
decimal sum = ((CrmMoney)entity.Properties["new_esum"]).Value;
String new_productname = "";
if (entity.Properties.Contains("productid"))
{
ColumnSet cs = new ColumnSet();
Lookup regarding = (Lookup)entity.Properties["productid"];
cs.Attributes.Add("name");
product _product = (product)crmService.Retrieve(EntityName.product.ToString(), regarding.Value, cs);
if (_product == null || _product.name == null)
{
return;
}
else
{
new_productname = _product.name;
}
}
QueryByAttribute query = new QueryByAttribute();
query.ColumnSet = new AllColumns();
query.EntityName = EntityName.productpricelevel.ToString();
query.Attributes = new string[] { "productidname" };
query.Values = new string[] { new_productname };
BusinessEntityCollection retrieved = crmService.RetrieveMultiple(query);
foreach (DynamicEntity product in retrieved.BusinessEntities)
{
product["amount"] = new CrmDecimal(sum);
crmService.Update(product);
}
}
catch (System.Web.Services.Protocols.SoapException ex)
{
throw new InvalidPluginExecutionException(String.Format("An error occurred in the {0} plug-in.", this.GetType().ToString()), ex);
}
}
}
}