2016-12-05 5 views
-1

Я создал два новых поля с названием «Цена» для предложения и цитирования продукта, и я хочу обновлять второй каждый раз, когда я обновляю первый.Обновление плагина Crm не удалось

Вот мой код:

protected void ExecutePostAccountUpdateContacts(LocalPluginContext localContext) 
{ 
if (localContext == null) 
{ 
    throw new ArgumentNullException("localContext"); 
} 
string oldPrice = ""; 
string newPrice = ""; 

IPluginExecutionContext context = localContext.PluginExecutionContext; 
IOrganizationService service = localContext.OrganizationService; 

var ServiceContext = new OrganizationServiceContext(service); 
ITracingService tracingService = localContext.TracingService; 

if (context.InputParameters.Contains("Target") && 
context.InputParameters["Target"] is Entity) 
{ 
    Entity entity = (Entity)context.InputParameters["Target"]; 

    Entity preImageEntity = (context.PreEntityImages != null && context.PreEntityImages.Contains(this.preImageAlias)) ? context.PreEntityImages[this.preImageAlias] : null; 

    // get the post entity image 
    Entity postImageEntity = (context.PostEntityImages != null && context.PostEntityImages.Contains(this.postImageAlias)) ? context.PostEntityImages[this.postImageAlias] : null; 

    if (preImageEntity.Attributes.Contains("Price")) 
    { 
     oldPrice = (string)preImageEntity.Attributes["Price"]; 
    } 

    if (postImageEntity.Attributes.Contains("Price")) 
    { 
     newPrice = (string)postImageEntity.Attributes["Price"]; 
    } 

    if (newPrice != oldPrice) 
    { 
     try 
     { 
      //Create query to get the related contacts 
      var res = from c in ServiceContext.CreateQuery("Products") 
         where c["parentQuoteid"].Equals(entity.Id) 
         select c; 

      foreach (var c in res) 
      { 
       Entity e = (Entity)c; 
       e["Price"] = newPrice; 

       ServiceContext.UpdateObject(e); 
      } 

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

} 
} 

ответ

0

Хотя вы не задали вопрос, ваш запрос не совсем верно. Поэтому я предполагаю, что ваш плагин не работает при запросе на product с parentquoteid.

Не все операторы linq реализованы, также передают логическое имя объекта в запрос создания как параметр, поэтому вместо Products всего product. Не существует поля из поля parentquoteid, вы не указали свой собственный префикс атрибута?

var res = from c in ServiceContext.CreateQuery("product") 
      where c.GetAttributeValue<Guid>("new_parentquoteid") == entity.Id 
      select c; 

 Смежные вопросы

  • Нет связанных вопросов^_^