Mainly I had this requirement where I had to delete audit log to shrink the database size so that it come back within log capacity threshold.
Below is the code for the DeleteRecordChangeHistoryRequest() method usage.Here I am looping through the records to delete the changed history for a particular entity in my case it is "Customer Asset".
static void Main(string[] args)
{
try
{
IOrganizationService _service;
ClientCredentials credentials = new ClientCredentials();
credentials.UserName.UserName = "username";
credentials.UserName.Password = "pwd";
Uri serviceUri = new Uri(@"soapurl");
OrganizationServiceProxy proxy = new OrganizationServiceProxy(serviceUri, null, credentials, null);
proxy.EnableProxyTypes();
_service = (IOrganizationService)proxy;
//fetchXML for getting entity
string fetchXML = @"<fetch mapping='logical'>
<entity name='msdyn_customerasset'>
<attribute name='msdyn_name'/>
<attribute name='msdyn_customerassetid'/>
</entity>
</fetch>
";
EntityCollection EntityList = _service.RetrieveMultiple(new FetchExpression(fetchXML));
Console.WriteLine("COUNT RESULTS: {0}", EntityList.Entities.Count);
foreach (var record in EntityList.Entities)
{
var EntityLogReference = new EntityReference(record.LogicalName, record.Id);
Console.WriteLine("Entity Name: " + EntityLogReference.LogicalName);
if (EntityLogReference.LogicalName != "contact" || EntityLogReference.LogicalName != "account")
{
var DeleteAudit = new DeleteRecordChangeHistoryRequest();
//-- Set entity to delete - workaround as CRM does not allow to delete Audit directly
DeleteAudit.Target = new EntityReference(record.LogicalName, record.Id);
if (EntityLogReference.Id != Guid.Empty)
{
_service.Execute(DeleteAudit);
Console.WriteLine("Delete Successful for Entity " + record.LogicalName + "Record " + record.Id);
}
}
else
{
Console.WriteLine("Skipped delete...");
}
}
}
catch (Exception ex)
{
Console.Write(ex);
}
Comments
Post a Comment