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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Plugin for filling quote product lineitemnumber does not start on CRM Online platform

(0) ShareShare
ReportReport
Posted on by

Hi!

I met a strange situation in different CRM Online instances on both versions CRM 2016 and Dynamics 365.

The goal is simple, add lineitemnumber to the quote product when it is greated. It is counting the number of created rows on the quote, increasing the figure by 1 and writing it to the created quote product.

The problem shows himlself in the morning when people are starting to work with CRM and create the first quotes.

Adding quote lines to the first quote takes a lot of time, platform seems like freezing. Some time it is giving time out error messages.

After waiting a 30 seconds or minute, the added quotelines are appearing to the quote form.

Quotes what are created later, are working good, without any delays.

That's a weird situation, seems like lineitem numbering plugin is "sleeping" and needs to "wakeup".

When I disable the plugin, the CRM starts to work normally, such behaviour will disappear.

Do you have any experiences with lineitem numbering with CRM Online?

What can cause such issue in CRM?

With thanks

Peep Palts

*This post is locked for comments

I have the same question (0)
  • a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Hello,

    Can you please provide code of your plugin?

  • Peep Palts Profile Picture
    on at

    Next is a code of that plugin:

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    using System.ServiceModel;

    using Microsoft.Xrm.Sdk;

    using Microsoft.Xrm.Sdk.Query;

    using System.Text.RegularExpressions;

    using Microsoft.Xrm.Sdk.Messages;

    using Microsoft.Crm.Sdk.Messages;

    namespace CreateQuoteDetailPlugIn

    {

       public class Plugin : IPlugin

       {

           public void Execute(IServiceProvider serviceProvider)

           {

               IPluginExecutionContext context = (IPluginExecutionContext)

                   serviceProvider.GetService(typeof(IPluginExecutionContext));

               Entity entity;

               if (context.InputParameters.Contains("Target") &&

                              context.InputParameters["Target"] is Entity)

               {

                   entity = (Entity)context.InputParameters["Target"];

                   if (entity.LogicalName != "quotedetail") { return; }

               }

               else

               {

                   return;

               }

               try

               {

                   IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)

                       serviceProvider.GetService(typeof(IOrganizationServiceFactory));

                   IOrganizationService service = serviceFactory.CreateOrganizationService(

                       context.UserId);

                   IPluginExecutionContext execContext = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

                   var id = context.PrimaryEntityId;

                   Entity quoteDetail = service.Retrieve("quotedetail", id, new ColumnSet("quoteid"));

                   if (quoteDetail != null)

                   {

                       EntityReference quoteId = quoteDetail.GetAttributeValue<EntityReference>("quoteid");

                       string quoteDetailQueryFetch = @"<fetch >

     <entity name='quotedetail' >

       <attribute name='quotedetailid' alias='quotedetailid' />

       <filter>

         <condition attribute='quoteid' operator='eq' value='" + quoteId.Id.ToString() + @"' />

       </filter>

     </entity>

    </fetch>";

                       FetchExpression fetchExpression = new FetchExpression(quoteDetailQueryFetch);

                       EntityCollection quoteDetailsFromCrm = service.RetrieveMultiple(fetchExpression);

                       quoteDetail["lineitemnumber"] = quoteDetailsFromCrm.Entities.Count;

                       service.Update(quoteDetail);

                   }

    }

               catch (Exception ex)

               {

                   throw new InvalidPluginExecutionException(

                       "An error occurred in the plug-in.", ex);

               }

           }

       }

    }

  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Your code looks good but here are suggestions you can implement to speed up execution of code:

    1. Don't retrieve quotedetail record again to get quoteid - use Target itself to get quoteid.

    2. Rewrite your code to use Aggregation count in fetch - msdn.microsoft.com/.../gg309565.aspx

    3. Don't update QuoteDetail again - use pre-validation and just populate target's field.

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans