'How to get error details client-side when a save operation fails in Dynamics CRM 2013?

When saving an entity form programmatically it is possible to register an error callback, like this:

Xrm.Page.data.save().then(successCallback, errorCallback);

Per the MSDN docs, the errorCallback will receive an object containing the properties errorCode and message. Sometimes it would be convenient (or even necessary) to get hold of more details about the error, such as any "inner exceptions" etc.

In my specific case, I'm getting an error when saving a record with changes that makes the record inaccessible to my user. The error message just says "Server is busy" and the error code is not documented, but if I click at the "Download error log" button in the error dialog that pops up I can see that there is indeed an inner fault message that makes more sense (access denied). For reference, this is the error log contents:

Unhandled Exception: System.ServiceModel.FaultException`1[[Microsoft.Xrm.Sdk.OrganizationServiceFault, Microsoft.Xrm.Sdk, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]: Server was unable to process request.Detail:
<OrganizationServiceFault xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/xrm/2011/Contracts">
  <ErrorCode>-2140991214</ErrorCode>
  <ErrorDetails xmlns:d2p1="http://schemas.datacontract.org/2004/07/System.Collections.Generic" />
  <Message>Server was unable to process request.</Message>
  <Timestamp>2014-06-06T15:15:56.16Z</Timestamp>
  <InnerFault>
    <ErrorCode>-2147187962</ErrorCode>
    <ErrorDetails xmlns:d3p1="http://schemas.datacontract.org/2004/07/System.Collections.Generic" />
    <Message>SecLib::AccessCheckEx failed. Returned hr = -2147187962, ObjectID: 10349363-8ded-e311-8c3a-001c42fc191a, OwnerId: f0cd03fc-18d5-e311-bb51-001c42fc191a,  OwnerIdType: 8 and CallingUser: 105e53ed-18d5-e311-bb51-001c42fc191a. ObjectTypeCode: 4, objectBusinessUnitId: ccd87e0a-dbec-e311-8c3a-001c42fc191a, AccessRights: ReadAccess </Message>
    <Timestamp>2014-06-06T15:15:56.16Z</Timestamp>
    <InnerFault i:nil="true" />
    <TraceText i:nil="true" />
  </InnerFault>
  <TraceText i:nil="true" />
</OrganizationServiceFault>

Are any of you aware of a way (supported or not) to get hold of these error details in the scope of the errorCallback function?



Solution 1:[1]

One ("unsupported") solution would be to rewrite the Mscrm.SaveErrorResponse "class" to include the serialized exception details. Good enough if you only want to search for a substring, for example. Something like this did the trick for me:

Mscrm.SaveErrorResponse = function (jsonData) {
  this.errorCode=jsonData.ErrorCode;
  this.message=jsonData.DisplayText;
  this.debugMessage=jsonData.Description;

  // Below is the only difference from the standard function definiton:
  if (jsonData.SerializedException) {
    this.serializedException = jsonData.SerializedException;
  }
};

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Anders Fjeldstad