Showing posts with label delete. Show all posts
Showing posts with label delete. Show all posts

Wednesday, August 14, 2013

Re-create deleted records using Audit History

Many a times we come across situations where we accidentally, delete a particular record and wish to recover that record back in CRM. This is possible only if you have “Auditing” enabled for that respective entity. So from the audit history we can re-create deleted records in CRM.

Let us take an example, supposing some records were deleted today and needs to be recovered.

//fetch XML for retrieving auditing records which were deleted today

                string fetchXML = "<fetch version='1.0' output-format='xml-platform' mapping='logical' no-lock='true' distinct='false'> " +
    "<entity name='audit'>  " +
    "<attribute name='auditid' /> " +
     "<attribute name='operation' /> " +
    "<filter type='and'> " +
    "<condition attribute='createdon' operator='today' /> " +
   "<condition attribute='operation' operator='eq' value='3' /> " +
    "</filter>   " +
    "</entity> " +
    "</fetch>";

                //retrieve the audit records from fetchxml
                var auditrecords = service.RetrieveMultiple(new FetchExpression(fetchXML));

                //loop through each audit entity record found and create deleted records
                foreach (Entity audit in auditrecords.Entities)
                {
                    // create retrieve audit detail request object
                    RetrieveAuditDetailsRequest auditDetailsRequest = new RetrieveAuditDetailsRequest();

                    //assign audit id value
                    auditDetailsRequest.AuditId = audit.Id;

                    //execute request and retrieve response
                    RetrieveAuditDetailsResponse auditDetailsResponse =
                        (RetrieveAuditDetailsResponse)_service.Execute(auditDetailsRequest);
                 
                    //create auditDetail variable and assign its value
                    AuditDetail auditDetail = auditDetailsResponse.AuditDetail;

                    //type cast audtitDetail as AttributeAuditDetail
                     AttributeAuditDetail attributeAuditDetail = auditDetail as AttributeAuditDetail;

                    //create the deleted record
                     service.Create(attributeAuditDetail.OldValue);
                }


In above code first we have retrieved audit entity records which were created “today” and operation was “Delete”.

“Operation” field is an “Option Set” field and values are

-              Create = 1
-              Update = 2
-              Delete = 3
-              Assign = 4





Hope this post helps!


Tuesday, November 30, 2010

Workflows - Behind the scene

We were recently working on designing a tool that deletes workflow job. We thought it would be easy to just filter out all job that related to a workflow by the workflow id and then delete that’s it.

But like always we were in for a surprise. Just thought putting it down would help others in resolving this mystery.

Each Workflow has a unique id just like any other entity. But if you search for workflow jobs using this id to look for all jobs related to this workflow, you would be in for a surprise. It would not return all of the jobs that you are expecting. The reason being each time you revise a workflow i.e unpublish and publish the workflow once again, it creates a new workflow entry in the tables. If you want to look for the workflow jobs related to a particular workflow then you need to look for the workflow id assigned to each of its versions.

For workflow, you would find the following definitions available in the tables.


There will be only 1 definition record in CRM for a workflow no matter how many times you revise it.


But each time you publish a workflow it will create a new activation record.


The activation workflow record will have its parent workflowid set to the workflow definition id.

The workflow definition record will store the id of the currently active version of the workflow.

If you unpublish a worklfow and publish it again, it will add another row to the table and the status would now be as follows:


Note the activeworkflowid of the Definition record now refers the Guid of the latest version of the workflow that is published.

Now if you want to look for system jobs for this workflow, you will need to search for jobs that have their workflowactivationid set to any of the workflowid listed above in the table.

Hope this helps you find your way through the maze of workflows!!!