Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to delete record using alternate key #205

Closed
LarsBauer opened this issue Nov 19, 2021 · 3 comments · Fixed by #225
Closed

Unable to delete record using alternate key #205

LarsBauer opened this issue Nov 19, 2021 · 3 comments · Fixed by #225
Labels
bug Something isn't working

Comments

@LarsBauer
Copy link

LarsBauer commented Nov 19, 2021

Hi,

Unfortunately when trying to delete a record using alternate key I receive the following exception.

My code snippet

var request = new DeleteRequest
{
    Target = new EntityReference(Account.EntityLogicalName)
    {
        KeyAttributes = new KeyAttributeCollection
        {
            { Account.LogicalNames.MyAlternateKey, key}
        }
    }
};

_context.Execute(request); /* OrganizationServiceContext */

Exception

Microsoft.PowerPlatform.Dataverse.Client.Utils.DataverseOperationException: Entity Reference cannot have Id and Key Attributes empty.
 ---> Microsoft.Rest.HttpOperationException: Operation returned an invalid status code 'BadRequest'
   at Microsoft.PowerPlatform.Dataverse.Client.ConnectionService.ExecuteHttpRequestAsync(String uri, HttpMethod method, String body, Dictionary`2 customHeaders, CancellationToken cancellationToken, DataverseTraceLogger logSink, Nullable`1 requestTrackingId, String contentType, Nullable`1 sessionTrackingId, HttpClient providedHttpClient)
   at Microsoft.PowerPlatform.Dataverse.Client.ConnectionService.Command_WebExecuteAsync(String queryString, String body, HttpMethod method, Dictionary`2 customHeaders, String contentType, String errorStringCheck, Guid callerId, Boolean disableConnectionLocking, Int32 maxRetryCount, TimeSpan retryPauseTime, Uri uriOfInstance, Guid requestTrackingId, CancellationToken cancellationToken)
   --- End of inner exception stack trace ---
   at Microsoft.PowerPlatform.Dataverse.Client.ServiceClient.Execute(OrganizationRequest request)
   at Microsoft.Xrm.Sdk.Client.OrganizationServiceContext.Execute(OrganizationRequest request)

I investigated further and have found the root cause of this issue here:

else if (req.Parameters.ContainsKey("Target") && req.Parameters["Target"] is EntityReference entRef) // this should cover things that have targets.
{
cReq = new Entity(entRef.LogicalName, entRef.Id);
}

As you can see the problem is that the KeyAttributes won't get evaluated when the Target parameter is of type EntityReference and so the request url will be invalid: accounts(00000000-0000-0000-0000-000000000000)

Let me know if I should provide a fix via PR. Thanks in advance!

@MattB-msft
Copy link
Member

We have accepted your changes, but cannot commit the PR here as it will be overwritten by the master, we will link the Update to this issue to close it when we push it live.

Thanks for your help @LarsBauer !

@MattB-msft MattB-msft added the bug Something isn't working label Nov 23, 2021
@LarsBauer
Copy link
Author

Hi @MattB-msft
sorry for the late reply...
These are great news! So should I close my pending PR or will it be merged once your internal repo is mirrored?

@MattB-msft
Copy link
Member

We will take care of it when the publish happens.
Thanks !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
2 participants