web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Suggested Answer

plugin speed / efficiency

(0) ShareShare
ReportReport
Posted on by

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.

  • Suggested answer
    Bipin D365 Profile Picture
    28,995 Moderator on at

    Hi,

    Can you try using Execute multiple request to delete records

    docs.microsoft.com/.../execute-multiple-requests

    This will reduce the number of service calls and should increase the performance of your plugin.

  • RyanCRM Profile Picture
    on at

    Unfortunately I tried that already, same issue with time out (works on opportunitys will less products), the Microsoft documentation also tells us not to use ExicuteMultiple in plugins -

    docs.microsoft.com/.../avoid-batch-requests-plugin

    Tried splitting the multiple request into smaller batches similar to the above but get lock issues again even though the same item is never accessed multiple times.

    Struggling with this one, don't understand why Microsoft is so slow here.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the August Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Daniyal Khaleel Profile Picture

Daniyal Khaleel 87 Most Valuable Professional

#2
11manish Profile Picture

11manish 67 Super User 2026 Season 2

#3
Travis South Profile Picture

Travis South 35

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans