locked
Plugin help needed RRS feed

  • Question

  • Hi All, I have a business scenario where I have to calculate the service quantities an account has then discount that by any incidents created. For customer loyalty sake, if one of the products is faulty and an incident is raised - the customer gets a discount of the bill. I need to create a plugin but this is the first time I'm attempting this. My requirements are:

    The first part is:

    I have on my Account entity with 3 decimal fields and 1 money field, new_ServiceA, new_ServiceB, new_ServiceC and new_Total which is a money field.

    I want to calculate the value of the 3 services and divide by 80, then store it in field "new_Total" on my Account entity. I want to do this on Post Create Operation.

    The second part that I need to do is:

    In the Incident form, there is a money field called "New_Discountprice" I need to also take that from the Incident entity and then use that value to subtract from the Total above. 

    Can anyone assist with this?


    • Edited by Sebd.DD Friday, November 29, 2013 12:59 AM
    Wednesday, November 27, 2013 9:15 PM

Answers

  • Hi Sanjeeb,

    Here is the video as promised: 

    Click here for Video

    The source code is here:

    Souce Code

    Part 2 is coming with the second part of your question.

    Kind regards,

    Steve

    • Marked as answer by Sebd.DD Saturday, November 30, 2013 7:40 AM
    Friday, November 29, 2013 10:18 PM
  • Hi Sanjeeb,

    Here is Part 2 explaining how to query the associated Incidents using Query Expressions and LINQ.

    Part 2

    • Marked as answer by Sebd.DD Saturday, November 30, 2013 7:41 AM
    Saturday, November 30, 2013 12:23 AM

All replies

  • Following code should be ok for you:

    IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
    Entity record = context.InputParameters["Target"] as Entity;
    
    if (!record.Contains("new_total"))
    	retrurn;
    
    int total = record.GetAttributeValue<int>("new_total");
    total = total/80;


    Dynamics CRM MVP/ Technical Evangelist at SlickData LLC
    My blog

    • Proposed as answer by _Vikram Thursday, November 28, 2013 1:23 AM
    Wednesday, November 27, 2013 9:48 PM
    Moderator
  • Hi I followed the example but it's giving me an error on ServiceProvider. I am not sure how to proceed with it.
    Wednesday, November 27, 2013 11:30 PM
  • Have you developed plugins before? I believe that you have not. Recheck following article - http://msdn.microsoft.com/en-us/library/gg328263.aspx

    Dynamics CRM MVP/ Technical Evangelist at SlickData LLC
    My blog

    Thursday, November 28, 2013 12:51 AM
    Moderator
  • Apologies, I'm fairly new and I am trying to create the plugin in post-operation. I've created plugins before but only with the class mentioned below. I started dev about a month ago so catching up. I've changed my idea a little; what I want to do is essentially take a value "new_cost" which is a decimal, divide that by 60 and set a new field called "new_total" which is a whole number. If anyone can help I would appreciate it immensely. 

    public class PostTimeRegisterUpdate: Plugin

            {

            public PostTimeRegisterUpdate()

                : base(typeof(PostTimeRegisterUpdate))

                {

                base.RegisteredEvents.Add(new Tuple<int, string, string, Action<LocalPluginContext>>(40, "Update", "new_timeregister", new Action<LocalPluginContext>(ExecutePostTimeRegisterUpdate)));

                }

            protected void ExecutePostTimeRegisterUpdate(LocalPluginContext localContext)

                {

                if (localContext == null)

                    {

                    throw new ArgumentNullException("localContext");

                    }

                }

            }

        }

    Thursday, November 28, 2013 8:19 AM
  • Ok. Seems that you develop your plugins used Developer Toolkit for CRM. So you can use following snippet of code:

        public class PostTimeRegisterUpdate : Plugin
        {
            public PostTimeRegisterUpdate()
                : base(typeof(PostTimeRegisterUpdate))
            {
                base.RegisteredEvents.Add(new Tuple<int, string, string, Action<LocalPluginContext>>(40, "Update", "new_timeregister", new Action<LocalPluginContext>(ExecutePostTimeRegisterUpdate)));
    
            }
    
            protected void ExecutePostTimeRegisterUpdate(LocalPluginContext localContext)
            {
                Entity record = localContext.PluginExecutionContext.InputParameters["Target"] as Entity;
    
                if (!record.Contains("new_total"))
                    return;
    
                int total = record.GetAttributeValue<int>("new_total");
                total = total / 80;
            }
        }


    Dynamics CRM MVP/ Technical Evangelist at SlickData LLC
    My blog

    Thursday, November 28, 2013 5:54 PM
    Moderator
  • Hi Sanjeeb,

    Are you going to subtract all "New_Discountprice" values for all Incidents associated to the Account??

    Do you want to update the new_Total field minus the incident discountprice values?

    Does the Account new_Total need to update everytime an Incident is created or updated?

    Friday, November 29, 2013 11:50 AM
  • Hi Steve, thank you for your reply. To answer your question:

    1. Yes, the discount needs to be subtracted from the main Account.

    2. Yes, The Discount Price needs to be subtracted from The Final Cost.

    3. Yes, customers get a fixed discount immediately if an Incident is Open.

    I have drawn a diagram of what I am trying to achieve:

     

    Would appreciate any advice.

    Friday, November 29, 2013 12:07 PM
  • I was requested to create a video to show how to do mathematics in a plugin and also retrieve information from additional entities. I was sent this post in a link.

    I will record a video and post the link this evening. Plus I will make the source code available.

    What is the division by 80 for??

    So to clarify if any cases are created for this Account the total value of the DiscountPrice fields is subtracted from the Total of the three services A,B,C which are divided by 80?

    Friday, November 29, 2013 4:23 PM
  • Steve that would be amazing! Please can you share you blog or website? Also the 80 was nothing important but a random figure I was playing with, but for this instance it is no relevant. Thanks you Steve!
    Friday, November 29, 2013 5:55 PM
  • Hi Sanjeeb,

    Here is the video as promised: 

    Click here for Video

    The source code is here:

    Souce Code

    Part 2 is coming with the second part of your question.

    Kind regards,

    Steve

    • Marked as answer by Sebd.DD Saturday, November 30, 2013 7:40 AM
    Friday, November 29, 2013 10:18 PM
  • Hi Sanjeeb,

    Here is Part 2 explaining how to query the associated Incidents using Query Expressions and LINQ.

    Part 2

    • Marked as answer by Sebd.DD Saturday, November 30, 2013 7:41 AM
    Saturday, November 30, 2013 12:23 AM
  • Steve, thank you so much for doing that!! I just went through the videos and you explained so well, just got my plugin and the functionality up and running. It helped me so much that I got a few other problems out of the way. Your Youtube Channel is amazing. Please continue to do more videos.

    Thank you so much! I'm subscribing to your channel!

    Saturday, November 30, 2013 7:33 AM