Customer Engagement & Dynamics CRM Forum

 View Only

Upsert with Date Only/DateTime, EntityReference Key Attribute Field

  • 1.  Upsert with Date Only/DateTime, EntityReference Key Attribute Field

    Posted Oct 27, 2021 03:22 PM
    How do I get the key attributes to match what's in Dynamics 365 for DateTime, DateOnly and/or EntityReference?

    I am attempting an "Upsert" to a Dynamics 365 Cloud GCC site. Most have been successful.

    Now I have a requirement that needs to use a Date Only field and an EntityReference field. Strings, whole numbers and decimals have worked best in the past. However the "Upsert" now routes to the Insert branch because the key attributes (KeyAttributes, KeyAttributeCollection) may be formatted wrong.

    Here is a current code sample in Powershell:

    $dateTime = Get-Date
    $dateOnly = $dateTime.Date 
    $row = New-Object Microsoft.Xrm.Sdk.Entity( "corp_entity" )
    $row.Attributes[ "corp_name" ] = "Test Name"
    $row.KeyAttributes.Add( "corp_datetime", $dateTime )
    $row.KeyAttributes.Add( "corp_dateonly", $dateOnly )
    $row.KeyAttributes.Add( "corp_lookup", $row.ToEntityReference() )
    $req = New-Object Microsoft.Xrm.Sdk.OrganizationRequest
    $req.RequestName = "Upsert"
    $params = New-Object Microsoft.Xrm.Sdk.ParameterCollection 
    $params.Add( "Target", $row )
    $req.Parameters = $params​
    From what I understood, DateTime and EntityReference were recently available for key attribute values. I may have missed something though.

    Any suggestions would be greatly appreciated.

    Rodrick Hales
    Software Consultant

If you've found this thread useful, dive deeper into User Group community content by role