Hello, Holly, and thank you very much for your reply.
I followed your advice and modified the coding so that it first retrieves the associated records and then detaches them.
Here's the code.
foreach ( var relationship in relaciones )
{
tracingService.Trace( $"Procesando relación: {relationship.NombreRelacion}; Tabla: {relationship.NombreTabla}" );
tracingService.Trace( $"Id del registro a buscar: {entityRef.Id}" );
// Obtener los registros relacionados
var query = new QueryExpression
{
EntityName = relationship.NombreTabla, // Nombre de la tabla relación N:N
ColumnSet = new ColumnSet( false ),
Criteria = new FilterExpression
{
Conditions =
{
new ConditionExpression(entityRef.LogicalName + "id", ConditionOperator.Equal, entityRef.Id)
}
}
};
EntityCollection relatedEntities = service.RetrieveMultiple( query );
tracingService.Trace( $"Ejecutada retrievemultiple para '{relationship.NombreRelacion}'" );
if ( relatedEntities.Entities.Count > 0 )
{
//tracingService.Trace( $"Id del registro de la tabla intermedia: {relatedEntities.Entities[0].Id}" );
var relatedEntityRefs = relatedEntities.Entities.Select( e => e.ToEntityReference( ) ).ToList( );
tracingService.Trace( $"Se encontraron {relatedEntityRefs.Count} registros relacionados en {relationship.NombreRelacion}" );
var disassociateRequest = new DisassociateRequest
{
Target = entityRef,
Relationship = new Relationship( relationship.NombreRelacion ),
RelatedEntities = new EntityReferenceCollection( relatedEntityRefs )
};
var respuesta = service.Execute( disassociateRequest );
tracingService.Trace( $"Relación {relationship.NombreRelacion} eliminada correctamente." );
}
else
{
tracingService.Trace( $"No hay registros en {relationship.NombreRelacion} para eliminar." );
}
}
The problem persists. Of the three tables, two detach correctly, but the third retains its data. I've looked at flows, actions, etc., and haven't seen any problems. I specifically created this plugin because there's no "before" delete event in either JavaScript or Power Automate.