Skip to main content

ValidateContactAssignmentRelationship

/// <summary>
/// Validates that the plugin context contains a valid tt_Contact_tt_Assignment relationship,
/// and extracts the contact and assignment references.
/// </summary>
public bool ValidateContactAssignmentRelationship(IPluginExecutionContext context, ITracingService tracingService, out EntityReference contact, out EntityReference assignment)
{
contact = null;
assignment = null;

// Check if the relationship is correct
if (!(context.InputParameters["Relationship"] is Relationship rel) ||
!string.Equals(rel.SchemaName, "tt_Contact_tt_Assignment", StringComparison.OrdinalIgnoreCase))
{
return false;
}

// Validate presence of required parameters
if (!(context.InputParameters["Target"] is EntityReference target) ||
!(context.InputParameters["RelatedEntities"] is EntityReferenceCollection related) ||
related.Count == 0)
{
tracingService.Trace("Missing Target or RelatedEntities parameters");
return false;
}

// Ensure correct entity types
if (target.LogicalName != "contact" || related[0].LogicalName != "tt_assignment")
{
tracingService.Trace("Entity type mismatch");
return false;
}

// Assign output values
contact = target;
assignment = related[0];
return true;
}