Skip to main content

Notifications

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.

  • RyanCRM Profile Picture
    RyanCRM on at
    RE: plugin speed / efficiency

    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.

  • Suggested answer
    Bipin D365 Profile Picture
    Bipin D365 28,964 Super User 2024 Season 1 on at
    RE: plugin speed / efficiency

    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.

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

News and Announcements

Announcing Category Subscriptions!

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Verified Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,359 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,370 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans