I have a plugin that I an using to delete opportunity products and its taking forever to run and times out due to the 2min plugin limit.
My Opportunity contains 423 Opportunity Products, is there a better way to delete all products on the opportunity?
I commented out all other transactions in the plugin till only the below remained and was still timing out.
public void DeleteProducts(IOrganizationService dynamicsService, ITracingService tracingService, EntityCollection opp_prod) { tracingService.Trace("DeleteProducts"); foreach (Entity product in opp_prod.Entities) { DeleteRequest deleteReq = new DeleteRequest() { Target = product.ToEntityReference() }; dynamicsService.Execute(deleteReq); } }
I tried to switch to something like the below, the Action in this example calls a plugin (Async) to process each of the batches, then i was hit with the error "1205 Transaction (Process ID %d) was deadlocked on %.*ls resources with another process and has been chosen as the deadlock victim. Rerun the transaction."public void DeleteProducts(IOrganizationService dynamicsService, ITracingService tracingService, EntityCollection opp_prod)
{
tracingService.Trace("DeleteProducts");
OrganizationRequest req = new OrganizationRequest("c1_RecordCreateOrDeleteAction");
req["Action"] = "Delete";
req["Message"] = "Attempted deletion of Opportunity Line Items";
int batchSize = 20;
//split the records into batches of defined size to prevent more time outs
List lstWrapper = new List();
for (int i = 0; i < (opp_prod.Entities.Count / batchSize) 1; i )
{
EntityCollection batchCollection = new EntityCollection();
batchCollection.EntityName = "opportunityproduct";
for (int j = (i * batchSize); (j < (i * batchSize) batchSize) && j < opp_prod.Entities.Count; j )
{
Entity current = opp_prod.Entities.ElementAt(j);
batchCollection.Entities.Add(current);
}
lstWrapper.Add(batchCollection);
}
if (lstWrapper.Any())
{
tracingService.Trace("number of batches to process - " lstWrapper.Count());
foreach (EntityCollection batchToProcess in lstWrapper)
{
req["Records"] = batchToProcess;
OrganizationResponse response = dynamicsService.Execute(req);
}
}
}
Ideally i want the delete to take seconds not minuets or some way to delete a list of opportunity product and create new ones in parallel without locking.