I want to delete records between two dates. I use a Fetchxml expression to get the GUID of each record between the two dates, then I use service.Delete
. Here is my code:
string fetchXml = @" <fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
<entity name='new_units'>
<link-entity name='new_alterunitorder' from ='new_orderlineid' to = 'new_unitsid' >
<attribute name='new_alterunitorderid' />
<filter type='and'>
<condition attribute='new_orderdate' operator='on-or-after' value='" + startDate.ToShortDateString() + @"' />
<condition attribute='new_orderdate' operator='on-or-before' value='" + endDate.ToShortDateString() + @"' />
<condition attribute='new_orderlineid' operator='eq' uiname='" + uiName + @"' uitype='new_units' value='" + unitOrderId + @"' />
</filter>
</link-entity>
</entity>
</fetch>";
EntityCollection result = service.RetrieveMultiple(new FetchExpression(fetchXml));
//Delete the collection
foreach (var id in result.Entities)
{
var entRef = id.GetAttributeValue<EntityReference>("new_alterunitorderid");
var guid = (Guid)entRef.Id;
service.Delete("new_alterunits", guid);
}
FetchXML has a limitation of 5000 records, how can I bypass that?! Also I read that looping through using service.Delete
is not the recommended way of bulk delete. Please help me find the best way to achieve what I want to do!