src/Client/Split-XrmRecords.ps1

<#
    .SYNOPSIS
    Disassociate records in Dataverse.

    .Description
    Remove a link between 1 row (Entity record) and multiple rows in Microsoft Dataverse.

    .PARAMETER XrmClient
    Xrm connector initialized to target instance. Use latest one by default. (Dataverse ServiceClient)

    .PARAMETER RecordReference
    Reference of the row to unlink the others from. (EntityReference)

    .PARAMETER RecordReferences
    Rows / Records references to split to Record. (EntityReference array)

    .PARAMETER RelationShipName
    RelationShip Logical name involve between these records.

    .PARAMETER RelationShipRole
    Role of RecordReference when the relationship links a table to itself: Referencing or Referenced. (Default: Referencing)

    .OUTPUTS
    System.Void.

    .EXAMPLE
    $account = Get-XrmRecord -LogicalName "account" -Id $accountId;
    $contactRefs = @(New-XrmEntityReference -LogicalName "contact" -Id $contactId);
    Split-XrmRecords -RecordReference $account.Reference -RecordReferences $contactRefs -RelationShipName "contact_customer_accounts";

    .LINK
    https://github.com/AymericM78/PowerDataOps/blob/main/documentation/commands/Split-XrmRecords.md
#>

function Split-XrmRecords {
    [CmdletBinding(SupportsShouldProcess)]
    [OutputType([System.Void])]
    param
    (    
        [Parameter(Mandatory = $false, ValueFromPipeline)]
        [Microsoft.PowerPlatform.Dataverse.Client.ServiceClient]
        $XrmClient = $Global:XrmClient,

        [Parameter(Mandatory = $true)]
        [Microsoft.Xrm.Sdk.EntityReference]
        $RecordReference,

        [Parameter(Mandatory = $true)]
        [Microsoft.Xrm.Sdk.EntityReference[]]
        $RecordReferences,

        [Parameter(Mandatory = $true)]
        [string]
        $RelationShipName,        

        [Parameter(Mandatory = $false)]
        [Microsoft.Xrm.Sdk.EntityRole]
        $RelationShipRole = [Microsoft.Xrm.Sdk.EntityRole]::Referencing
    )
    begin {   
        $StopWatch = [System.Diagnostics.Stopwatch]::StartNew(); 
        Trace-XrmFunction -Name $MyInvocation.MyCommand.Name -Stage Start -Parameters ($MyInvocation.MyCommand.Parameters);
    }    
    process {

        $relationShip = New-Object -TypeName "Microsoft.Xrm.Sdk.Relationship" -ArgumentList $RelationShipName;
        $relationShip.PrimaryEntityRole = $RelationShipRole;
        
        $recordReferenceCollection = New-XrmEntityReferenceCollection -EntityReferences @($RecordReferences);
        
        $disassociateRequest = [Microsoft.Xrm.Sdk.Messages.DisassociateRequest]::new();
        $disassociateRequest.Target = $RecordReference;
        $disassociateRequest.Relationship = $relationShip;
        $disassociateRequest.RelatedEntities = $recordReferenceCollection;
        $XrmClient | Invoke-XrmRequest -Request $disassociateRequest | Out-Null;
    }
    end {
        $StopWatch.Stop();
        Trace-XrmFunction -Name $MyInvocation.MyCommand.Name -Stage Stop -StopWatch $StopWatch;
    }    
}

Export-ModuleMember -Function Split-XrmRecords -Alias *;