Я пытаюсь связать связанные записи объекта с вновь созданным. Плагин срабатывает при создании и предварительной работе.
Ошибка возникает при попытке связать коллекцию с новым объектом: «new_ligneContrat With Id = ad630ba6-684e-e111-92e3-00155d151905 не существует»
Вот мой код:
public void Execute(IServiceProvider serviceProvider)
{
// Instanciation des services
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = factory.CreateOrganizationService(null);
Entity target = (Entity)context.InputParameters["Target"];
EntityReference contrats = (EntityReference)target.Attributes["new_contratsid"];
FetchExpression fetch = new FetchExpression(@"
<fetch distinct='false' mapping='logical'>
<entity name='" + context.PrimaryEntityName + "'><link-entity name='new_contrats' alias='nombreligne' from='new_contratsid' to='new_contratsid'><filter type='and'><condition attribute='new_contratsid' value='" + contrats.Id + "' operator='eq'></condition></filter></link-entity></entity></fetch>");
EntityCollection lines = service.RetrieveMultiple(fetch);
// Vérification qu'il y a au moins une ligne de contrat associée
if (lines.Entities.Any())
{
var first = lines.Entities.Last();
if (first.GetAttributeValue<OptionSetValue>("statecode").Value == 1)
{
FetchExpression query = new FetchExpression(@"
<fetch distinct='false' mapping='logical'>
<entity name='incident'><filter type='and'><condition attribute='new_lignecontrat' value='"+first.Id+"' operator='eq'/></filter></entity></fetch>");
EntityCollection incident = service.RetrieveMultiple(query);
if (incident.Entities.Any())
{
foreach (var e in incident.Entities)
{
e.Attributes["new_lignecontrat"] = new EntityReference (target.LogicalName, target.Id);
}
}
}
}
Что случилось???
Заранее спасибо!!
Редактировать 1: ok кажется логичным, поскольку запись еще не существует >‹. Только одно: как я могу изменить значение поля поиска? Каков его тип?
Редактировать 2: у меня нет ошибки при выполнении моего кода, но поля объекта инцидента не обновляются >‹'.... Я проверил свой код с invalidPluginExceptions, и конец кода достигнут... Здесь это код:
Редактировать 3: код обновлен...