Microsoft.Xrm.Data.PowerShell icon indicating copy to clipboard operation
Microsoft.Xrm.Data.PowerShell copied to clipboard

Get-CrmRecord should also support other options than ID

Open RStadlmair opened this issue 8 years ago • 9 comments

Hi ! When referencing to existing data with the need to emit data out of CRM, the current way seems to be

  1. Get-CRMrecordS,
  2. get an array of arrays of data
  3. Get the first array [0] in the index
  4. get its ID
  5. and then get the record by ID that you want with Get-CRMrecord

What i want is an extension to get-crmrecord to allow to select the same way as in get-CrmrecordS by filterattribute, filteroperator and filtervalue, returning only one object.

Thoughts / comments ?

Regards/Roman

RStadlmair avatar Sep 28 '16 05:09 RStadlmair

nice idea actually. So basically you want to omit CrmRecord[0] part. The thing is for Get-CrmRecord, we assume you already know which id you want to get. However if we return a record only for Get-CrmRecords, it breaks the model, yet you should use Get-CrmRecrods as you are kind of querying the record.

There are two things in my mind.

  1. Alternative Key support: I will add this to v2.5
  2. -SingleRecord or -Single option for Get-CrmRecords which returns a record (maybe just first one if multiple?)

How do you think?

Get-CrmRecords account name eq "AdventureWorks Cycle" -Single

This return just record, same as Get-CrmRecord. Hope to here your opinions.

Mean while use this function.

function Get-CrmRecordWithFilter
{
    [CmdletBinding()]
    PARAM(
        [parameter(Mandatory=$false)]
        [Microsoft.Xrm.Tooling.Connector.CrmServiceClient]$conn,
        [parameter(Mandatory=$true, Position=1)][alias("EntityName")]
        [string]$EntityLogicalName,
        [parameter(Mandatory=$false, Position=2)][alias("FieldName")]
        [string]$FilterAttribute,
        [parameter(Mandatory=$false, Position=3)][alias("Op")]
        [string]$FilterOperator,
        [parameter(Mandatory=$false, Position=4)][alias("Value", "FieldValue")]
        [string]$FilterValue,
        [parameter(Mandatory=$false, Position=5)]
        [string[]]$Fields, 
        [parameter(Mandatory=$false, Position=6)]
        [switch]$AllRows,
        [parameter(Mandatory=$false, Position=7)]
        [int]$TopCount
    )

    if($conn -eq $null)
    {
        $records = Get-CrmRecords -EntityLogicalName $EntityLogicalName -FilterAttribute $FilterAttribute -FilterOperator $FilterOperator -FilterValue $FilterValue -Fields $Fields -TopCount 1        
    }
    else
    {
        $records = Get-CrmRecords -conn $conn -EntityLogicalName $EntityLogicalName -FilterAttribute $FilterAttribute -FilterOperator $FilterOperator -FilterValue $FilterValue -Fields $Fields -TopCount 1
    }
    if($records.Count -eq 1)
    {
        return $records.CrmRecords[0]
    }
    else
    {
        Write-Host "no record"
    }
}

Get-CrmRecordWithFilter account name eq "Adventure Works (sample)"

kenakamu avatar Sep 28 '16 15:09 kenakamu

Hi !

The -Single option is nice, and maybe easiest to implement (maybe call it "-unique")

Thanks for the function !

Regards/Roman

RStadlmair avatar Sep 29 '16 05:09 RStadlmair

Sounds good. Let me though how we want to implement this. Maybe make more sense to add this to get-crmrecord anyway

kenakamu avatar Sep 29 '16 13:09 kenakamu

Hmm. Some thoughts on that. The "Problem" is fetching multiple records and the choice which one to take right ?

Option 1: We get the records, sort them by lastmodified date and take the newest one.

Option 2: We provide an additional Parameter to specify which one to take Example: 3 records emitted and take the second one: Get-Crmrecord -entityname Account -filterattribute Name -filterattribute -like -filterattribute 'Contoso -unique -index 2'

Option 3: we simple let the system choose and hope for the intelligence of the user. If somebody uses this Option he/she must make sure there is only one record

Reading this again i like the mix of 2&3 best. so either specify NO additional parameter to -unique and take the first one the system gets OR specify additionally the -index Parameter.

My 5 Cents ... /Roman

RStadlmair avatar Sep 29 '16 17:09 RStadlmair

it is interesting issue when you have more than two, but as you are querying it, you are supposed to be sure about uniqueness of the record, otherwise I recommend to use Get-CrmRecords.

We don't support Alternative Key yet, so my thoughts are

  1. support alternative key for Get-CrmRecord
  2. Add -FirstRecord option for Get-CrmRecords or Get-CrmRecordsByFetch so that you can get only record data. If you want to manage order as well, ues Fetch.

How do you think?

kenakamu avatar Oct 04 '16 18:10 kenakamu

Nice suggestions, I have thought a feature like this would be useful. I like your -FirstRecord proposal because it can cover cases that aren't satisfied by alternate keys. For instance if I want to get a systemuser by email address or domain name. I know there will only be one result even if I must query it.

ian-moore avatar Oct 05 '16 21:10 ian-moore

Alternative Key: Any Keys in mind ? they may be different for each types, but makes sense i.e. Get-CRMRecord -AltKey(Switch) -EntityLogicalName 'systemuser' -UniqueAttribute "ADusername" -filteroperator 'eq' -Filtervalue 'kenakamu'

Firstrecord - Ok with me as an additional option. Do we know in which order data is emitted ?

Like Alternative Key most !

RStadlmair avatar Oct 09 '16 06:10 RStadlmair

The convention in Powershell is to use singular noun names, so get-crmrecordS does not follow that convention. If you want get-crmrecord to be able to search on things other than the ID, I would suggest that all the various get-crmrecord* commands be collapsed into a single command. Then if you only want 1 record, you can use the -TopCount parameter to limit return results. One complication I see is that the difference in return value types of the various commands now.

kenglover avatar Dec 05 '18 18:12 kenglover

I think this is out of scope for now, would be open to anyone doing some work on this to contribute but given how the sdk works and returns records it would be a pretty major change of approach IMO.

seanmcne avatar Jul 17 '19 01:07 seanmcne