src/Converters/ConvertTo-XrmObject.ps1
|
<# .SYNOPSIS Transform Entity to custom object. .DESCRIPTION Represent Entity object to custom object. .PARAMETER Record Entity record / table row (Entity). .OUTPUTS PSCustomObject. Row with Id, LogicalName, Record (the Entity), Reference (EntityReference) and one property per column: the formatted value when there is one (the raw value is then in a second property suffixed with _Value), otherwise the value. .EXAMPLE $entity = Get-XrmRecord -XrmClient $xrmClient -LogicalName "account" -Id $accountId -Columns "name", "industrycode" -AsEntity; $account = $entity | ConvertTo-XrmObject; $industryLabel = $account.industrycode; $industryValue = $account.industrycode_Value; .LINK https://github.com/AymericM78/PowerDataOps/blob/main/documentation/commands/ConvertTo-XrmObject.md #> function ConvertTo-XrmObject { [CmdletBinding()] [OutputType([PSCustomObject])] param ( [Parameter(Mandatory, ValueFromPipeline)] [Microsoft.Xrm.Sdk.Entity] $Record ) begin { $StopWatch = [System.Diagnostics.Stopwatch]::StartNew(); Trace-XrmFunction -Name $MyInvocation.MyCommand.Name -Stage Start -Parameters ($MyInvocation.MyCommand.Parameters); } process { $hash = [ordered]@{ "Id" = $Record.Id "LogicalName" = $Record.LogicalName "Record" = $Record "Reference" = $Record.ToEntityReference() }; $attributes = $Record.Attributes | Sort-Object -Property Key; foreach ($attribute in $attributes) { $value = ""; if ($Record.FormattedValues.ContainsKey($attribute.Key)) { $value = $Record.FormattedValues[$attribute.Key]; $hash["$($attribute.Key)_Value"] = $Record[$attribute.Key]; } else { $value = $Record[$attribute.Key]; if ($value) { if ($value.GetType().Name -eq "AliasedValue") { $value = $value.Value; } } } $hash[$attribute.Key] = $value; } $object = [pscustomobject]$hash; $object; } end { $StopWatch.Stop(); Trace-XrmFunction -Name $MyInvocation.MyCommand.Name -Stage Stop -StopWatch $StopWatch; } } Export-ModuleMember -Function ConvertTo-XrmObject -Alias *; |