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)

Correct plugin event

(0) ShareShare
ReportReport
Posted on by 437

Hi Friends, 

I have a  requirement to create  a plugin.

In a opportunity Entity there is a out of box Field "Success" of option type Having items(10%, 20%,30%).

Req- When a user changes e.g. 10% to 20% I want to capture 10%  and then i will do calculation a and store its value in a another field.

So on which event (prevalidation , preoperation or post operation) I should register the plugin ?

Thanks

  

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Michel van den Brink Profile Picture
    4,697 on at

    Hello,

    1. If the field, in which you want to store the calculation result, is in the same entity then PreOperation
      1. This saves some internal latency because you don't have to update any other records and it also prevents you from accidentally updating the same record multiple times.
    2. If you want to store the calculation result in another entity, then PostOperation
      1. Because you want to make sure the commit to the database has finished, so you can continue with your action in a separate transaction

      

    Check out this article on the differences between the different pipeline stages:
    https://docs.microsoft.com/en-us/dynamics365/customer-engagement/developer/event-execution-pipeline

      

    Hope this helps you get started. If you have any other questions, please let me know!

    If you found my answer helpful, please help the community by marking it as verified :-)

  • windyMill Profile Picture
    437 on at

    Thanks for your reply . Let me implement this then I will tell you.

  • windyMill Profile Picture
    437 on at

    Also should I register my plugin on operation - update

  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at

    Hi ,

    You should go pre operation if the field is in same entity otherwise go post operation after committing the target entity value.

    Hope this helps.

  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at

    Hi ,

    Yes if you are updating the data then you should registered update event.

  • Suggested answer
    Michel van den Brink Profile Picture
    4,697 on at

    For your scenario you should register it on Create and on Update, so your plug-in triggers when a new record is created and when one is updated.

    Make sure that for Update you only trigger you plug-in when the 'Success' field has been changed, to avoid unnecessary execution of your code.

  • windyMill Profile Picture
    437 on at

    Hi Michel,

    I am getting error  I am  using following code.

    I registered it in preoperation  event

    Message - update

    Filter attribute - success

    So when ever success option set change i want to do some calculation by taking its previous value.

    Means it if it 10%  and user change 10% to 20%  then i want to fetch 10% and then i will do some calculation

    and then i will update a another field.

    Kindly suggest

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    using System.Threading.Tasks;

    using System.Text.RegularExpressions;

    using Microsoft.Xrm.Sdk;

    using System.ServiceModel;

    namespace PreopAgingChanceofSuccess

    {

        public  classSuccess :IPlugin

       {

           public void Execute(IServiceProvider serviceProvider)

           {

               //Extract the tracing service for use in debugging sandboxed plug-ins.

               ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));

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

               tracingService.Trace("Plugin enetering in update");

               try

               {

                   int statusCode = 0;

                   if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)

                   {

                       if (context.Depth > 1) { return; }

                       //obtain the target  entity from input parameters

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

                               if (entity.Attributes.Contains("kkk_chanceofsuccess"))  // getting error in this line

                               {

                                   string optionsetScuccess = entity.FormattedValues["kkk_chanceofsuccess"].ToString();

                                   optionsetChanceofScuccess = Regex.Replace(optionsetChanceofScuccess, "[^0-9]+", string.Empty);

                                   throw new InvalidPluginExecutionException(optionsetScuccess);

                               }

                   }

               }

               catch (FaultException<OrganizationServiceFault> ex)

               {

                   throw new InvalidPluginExecutionException(ex.Message.ToString());

               }

               catch (Exception ex)

               {

                   tracingService.Trace(ex.ToString());

                   throw;

               }

               }

           }

       }

    error -

    Unexpected exception from plug-in (Execute): PreopSuccess.PreOperationSuccess: System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary. If you contact support, please provide the technical details.

  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at

    As you mentioned in filter attribute you have put - success , you should put  name of the attribute not value of the attribute.

    I can see your attribute name is kkk_chanceofsuccess so you need to put kkk_chanceofsuccess in the filtering attribute instead plugin registration tool instead of success.

    Hope this helps.

  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at

    As you mentioned in filter attribute you have put - success , you should put  name of the attribute not value of the attribute.

    I can see your attribute name is kkk_chanceofsuccess so you need to put kkk_chanceofsuccess in the filtering attribute instead plugin registration tool instead of success.

    Hope this helps.

  • Suggested answer
    RaviKashyap Profile Picture
    55,410 Moderator on at

    Hi,

    I don't think the error is thrown on the line  if (entity.Attributes.Contains("kkk_chanceofsuccess")) as here we are just checking if it exists or not. The error "key not found" means the key we are using in the collection doesn't exists in the collection and we are using the key is just this statement "entity.FormattedValues["kkk_chanceofsuccess"].ToString();" which could throw "key not found" exception.

    It appears that you are trying to get the text value of your option set. If that's true then you can try the below link to retrieve the metadata.

    community.dynamics.com/.../retrieve-option-set-metadata-in-c

    You can also hardoce the the values insyead of retrieving the text value i.e.

    if(value == 10000000)

    {

    text = 10;

    }

    else if(value == 10000001)

    {

    text = 20;

    }

    else

    {

    text = 30;

    }

    Hope this helps.

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