locked
How to create a plugin that stops the creation of an entity depending on property of another entity ? RRS feed

  • Question

  • Hello,

    I'm a beginner in CRM development and I need to create a plugin that is triggered on pre-creation of a "Case" entity, that checks on a boolean property "Allow case" in the "Contact" entity, if is checked then the creation of "Case" would be blocked.

    What should be the Target entity in this case ? and how can I get the plugin to check on an entity different than the one that has the event triggering it ?

    I appreciate any help or tip.

    Thank you

    Friday, December 13, 2013 9:18 PM

Answers

  • Hi,
    this plugin is quite easy, you need to retrieve the contact associated to the case and check for the field, if the value is false (means doesn't allow cases) you need to throw an exception and the creation of the case will be stopped (the plugin needs to be synchronous).

    The target is always the case entity, if you can wait I can write the plugin for you in the next hours.

    Edit: you can start from this code

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.Xrm.Sdk;
    using Microsoft.Xrm.Sdk.Query;
    using Microsoft.Xrm.Sdk.Messages;
    
    namespace PluginExample
    {    
        public class CheckContactCase : IPlugin
        {
            public void Execute(IServiceProvider serviceProvider)
            {
                    ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
                    IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
                    IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                    IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
    
                    if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
                    {
                        Entity incident = (Entity)context.InputParameters["Target"];
    
                        if (incident.LogicalName != "incident")
                        {
                           return;
                        }
    
                        if (incident.Contains("customerid") && incident["customerid"] != null)
                        {
                            EntityReference contactReference = (EntityReference)incident["customerid"];
                            // check if the customer is a contact, do nothing if is an account
                            if (contactReference.LogicalName == "contact")
                            {
                                //retrieve contact
                                string allowField = "new_allowcase"; ; // change the field name if different
                                Entity contact = service.Retrieve(contactReference.LogicalName, contactReference.Id, new ColumnSet(allowField))
                                if (contact.Contains(allowField) && contact[allowField] != null)
                                {
                                    bool allowCase = (bool)contact[allowField];
                                    if (allowCase == false)
                                    {
                                         throw new InvalidPluginExecutionException("Create a case for this contact is not permitted");
                                    }
                                }
                            }
                        }
                    }
            }
        }
    }
    Remember that the plugin needs to be synchronous


    My blog: www.crmanswers.net - Rockstar 365 Profile



    • Edited by Guido PreiteMVP Friday, December 13, 2013 10:18 PM
    • Proposed as answer by Guido PreiteMVP Friday, December 13, 2013 10:18 PM
    • Marked as answer by Malhoosh Friday, December 13, 2013 10:45 PM
    Friday, December 13, 2013 9:35 PM

All replies

  • Hi,
    this plugin is quite easy, you need to retrieve the contact associated to the case and check for the field, if the value is false (means doesn't allow cases) you need to throw an exception and the creation of the case will be stopped (the plugin needs to be synchronous).

    The target is always the case entity, if you can wait I can write the plugin for you in the next hours.

    Edit: you can start from this code

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.Xrm.Sdk;
    using Microsoft.Xrm.Sdk.Query;
    using Microsoft.Xrm.Sdk.Messages;
    
    namespace PluginExample
    {    
        public class CheckContactCase : IPlugin
        {
            public void Execute(IServiceProvider serviceProvider)
            {
                    ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
                    IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
                    IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                    IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
    
                    if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
                    {
                        Entity incident = (Entity)context.InputParameters["Target"];
    
                        if (incident.LogicalName != "incident")
                        {
                           return;
                        }
    
                        if (incident.Contains("customerid") && incident["customerid"] != null)
                        {
                            EntityReference contactReference = (EntityReference)incident["customerid"];
                            // check if the customer is a contact, do nothing if is an account
                            if (contactReference.LogicalName == "contact")
                            {
                                //retrieve contact
                                string allowField = "new_allowcase"; ; // change the field name if different
                                Entity contact = service.Retrieve(contactReference.LogicalName, contactReference.Id, new ColumnSet(allowField))
                                if (contact.Contains(allowField) && contact[allowField] != null)
                                {
                                    bool allowCase = (bool)contact[allowField];
                                    if (allowCase == false)
                                    {
                                         throw new InvalidPluginExecutionException("Create a case for this contact is not permitted");
                                    }
                                }
                            }
                        }
                    }
            }
        }
    }
    Remember that the plugin needs to be synchronous


    My blog: www.crmanswers.net - Rockstar 365 Profile



    • Edited by Guido PreiteMVP Friday, December 13, 2013 10:18 PM
    • Proposed as answer by Guido PreiteMVP Friday, December 13, 2013 10:18 PM
    • Marked as answer by Malhoosh Friday, December 13, 2013 10:45 PM
    Friday, December 13, 2013 9:35 PM
  • Thank you very much, this has been very helpful.

    And when registering the plugin it should be on "Pre-operation" ? 
    Friday, December 13, 2013 10:44 PM