Untitled
unknown
plain_text
2 years ago
3.6 kB
12
Indexable
public class AddParticipantsPlugin : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
try
{
if (context.MessageName.ToLower() == "update" && context.Stage == 40) // PostUpdate
{
// Recupera i dati aggiornati dell'entità Visit Report
Entity updatedVisitReport = (Entity)context.InputParameters["Target"];
// Verifica se il campo Appuntamento su Visit Report è stato modificato
if (updatedVisitReport.Attributes.Contains("new_appuntamento"))
{
// Ottieni l'ID dell'appuntamento dal campo lookup
EntityReference appointmentRef = (EntityReference)updatedVisitReport.Attributes["new_appuntamento"];
Guid appointmentId = appointmentRef.Id;
// Ottieni tutti i partecipanti (obbligatori e opzionali) dell'appuntamento
Entity appointment = service.Retrieve("appointment", appointmentId, new ColumnSet("requiredattendees", "optionalattendees"));
// Aggiungi i partecipanti alla griglia dei contatti dell'entità Visit Report
AddParticipantsToGrid(service, updatedVisitReport, appointment);
}
}
}
catch (Exception ex)
{
throw new InvalidPluginExecutionException($"Errore nell'esecuzione del plugin: {ex.Message}");
}
}
private void AddParticipantsToGrid(IOrganizationService service, Entity visitReport, Entity appointment)
{
// Recupera i contatti associati all'appuntamento (obbligatori e opzionali)
EntityCollection participants = new EntityCollection();
participants.Entities.AddRange(((EntityCollection)appointment.Attributes["requiredattendees"]).Entities);
participants.Entities.AddRange(((EntityCollection)appointment.Attributes["optionalattendees"]).Entities);
// Itera sui partecipanti
foreach (Entity participant in participants.Entities)
{
// Recupera il contatto associato al partecipante (SystemUser o Account)
Entity contact = RetrieveContactFromParticipant(service, participant);
// Aggiungi il contatto alla griglia dei contatti dell'entità Visit Report
AddContactToGrid(service, visitReport, contact);
}
}
private Entity RetrieveContactFromParticipant(IOrganizationService service, Entity participant)
{
// Implementa la logica per recuperare il contatto dal partecipante (SystemUser o Account)
// Utilizza il campo "Related Contact" sull'entità "SystemUser" e il campo "PrimaryContact" sull'entità "Account"
// Restituisci l'entità contatto recuperata
}
private void AddContactToGrid(IOrganizationService service, Entity visitReport, Entity contact)
{
// Implementa la logica per aggiungere il contatto alla griglia dell'entità Visit Report
// Assicurati di gestire la relazione 1:N tra "Visit Report" e "Contact"
}
}Editor is loading...
Leave a Comment