locked
Cannot restore deleted Record RRS feed

  • Question

  • Hi,

    We are trying to restore a record <dfn class="dictionary-of-numbers">one of our user </dfn>has accidentally deleted.

    I wrote this code, based on some samples I found online

    public static bool RecoverEntity(IOrganizationService organization, Guid auditId)

    {              

           RetrieveAuditDetailsRequestrequest =new RetrieveAuditDetailsRequest();

           request.AuditId = auditId;

           RetrieveAuditDetailsResponse response = (RetrieveAuditDetailsResponse)organization.Execute(request);

           organization.Create(response.AuditDetail.OldValue);

           return true;

    }

    but I cannot access the OldValue property of AuditDetail.

    Am I using some wrong sample?


    Thursday, October 31, 2013 8:49 AM

Answers

  • I was missing a cast, now the code works.

    I will write it here, hoping it will of help to someone else in the future:

    /// <summary>

    /// Restores a record from the Audit history

    /// </summary>

    /// <param name="organization">Organization service to be invoked</param>

    /// <param name="auditId">Id of the audit record to be used during the restore operation</param>

    /// <returns>A bool showing if the method succeded</returns>

    public static bool RecoverEntity(IOrganizationService organization, Guid auditId)

    {

        // create retrieve audit detail request object

        var request =new RetrieveAuditDetailsRequest {AuditId = auditId};

        //execute request and retrieve response

        var response = (RetrieveAuditDetailsResponse)organization.Execute(request);

        //create auditDetail variable and assign its value

        AuditDetail auditDetail = response.AuditDetail;

        //type cast audtitDetail as AttributeAuditDetail

        var attributeAuditDetail = auditDetail as AttributeAuditDetail;

        //create the deleted record

        if (attributeAuditDetail!=null)

            organization.Create(attributeAuditDetail.OldValue);

        else

        {

            return false;

        }

        return true;

    }



    Thursday, October 31, 2013 2:06 PM