locked
Plugin that allows you to increase the counter RRS feed

  • Question

  • Hi, I use crm 2016, I have to do this:
    • Create a plugin that allows you to increase the counter “Contacts Registered for the Event” of the Event every time that a contact is associated to an event. In that plugin you have to use the Associate event.

    Tuesday, October 24, 2017 3:47 PM

All replies

  • Hi, 

    you can use a plugin for the post update step where you check the target if the lookup linked to the entity event has been modified. If yes , you update the counter of the associate event. 

    Kr, 

    Moh


    • Proposed as answer by Moh Helper Tuesday, October 24, 2017 10:05 PM
    Tuesday, October 24, 2017 10:05 PM
  • Like this:

    private void Run(LocalPluginContext localContext, Entity targetEntity)
            {
                try
                {
                    // Obtain the execution context from the service provider.
                    IPluginExecutionContext context = localContext.PluginExecutionContext;
                    IOrganizationService service = localContext.OrganizationService;

                    var registerId = targetEntity.GetAttributeValue<EntityReference>("wgarcia_event").Id;
                    Entity registerEvent = service.Retrieve("wgarcia_event", registerId, new ColumnSet(true));

                    int countRegistered = 0;
                    if (registerEvent.Contains("wgarcia_contactsregisteredfortheevent"))
                    {
                        string valueAttr = Convert.ToString(registerEvent["wgarcia_contactsregisteredfortheevent"]);
                        countRegistered = string.IsNullOrEmpty(valueAttr) ? 0 : Convert.ToInt32(valueAttr);
                    }

                    countRegistered++;

                    registerEvent.Attributes["wgarcia_contactsregisteredfortheevent"] = countRegistered;
                    service.Update(registerEvent);
                }
                catch (Exception ex)
                {
                    localContext.Trace(ex.Message);
                    throw ex;
                }
            }

    It works... but the associate event... 

    Wednesday, November 8, 2017 2:45 PM
  • This is not a proper way of writing a plugin.

    Row: Entity registerEvent = service.Retrieve("wgarcia_event", registerId, new ColumnSet(true));

    Why retrieve all columns?

    string valueAttr = Convert.ToString(registerEvent["wgarcia_contactsregisteredfortheevent"]);

    Why convert to String?

    I would take another approach. Count the number of associated records. The above example is not 100% accurate. What if two plugins run at the same time?

    • Proposed as answer by MGCRM Monday, November 20, 2017 10:24 AM
    Saturday, November 18, 2017 9:48 PM
  • Hello,

    Writing plugin may not be necessary here, you can use "Rollup" field to achieve this.


    Thanks and Regards.

    • Proposed as answer by Ravitheja J Tuesday, December 5, 2017 8:31 AM
    Tuesday, December 5, 2017 7:25 AM