Restrict Delete Of Entity Record Based On Parent Field Val


Permit has lookup of Permit Type.

Permit Type has field "Type" with options apple, orange.

Now, when Permit record is deleted you should check the value of Type in Permit Type and if it is orange then do not only allow deletion of a record. Otherwise, a record shouldn't be deleted using plugin.

Plugin Code

using Microsoft.Xrm.Sdk;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Microsoft.Crm.Sdk.Samples
    public class RestrictPermitDelete : IPlugin
        public void Execute(IServiceProvider serviceProvider)

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

            // Obtain the execution context from the service provider.
            IPluginExecutionContext context = (IPluginExecutionContext)
            // Obtain the organization service reference.
            IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

            // The InputParameters collection contains all the data passed in the message request.
            if (context.InputParameters.Contains("Target") &&
                context.InputParameters["Target"] is EntityReference)
                // Obtain the target entity from the input parameters.
                EntityReference entity = (EntityReference)context.InputParameters["Target"];

                // Verify that the target entity represents an contoso_permit.
                // If not, this plug-in was not registered correctly.
                if (entity.LogicalName != "contoso_permit")

                // Get Lookup field value
                Entity permitEntity = service.Retrieve("contoso_permit", entity.Id, new Xrm.Sdk.Query.ColumnSet("contoso_permittype"));
                if (permitEntity != null)
                    if (permitEntity.Attributes.Contains("contoso_permittype") && permitEntity.Attributes["contoso_permittype"] != null)
                        EntityReference permitType = (EntityReference)permitEntity.Attributes["contoso_permittype"];
                        if (permitType != null && permitType.Id != Guid.Empty)
                            Entity permitTypeEntity = service.Retrieve("contoso_permittype", permitType.Id, new Xrm.Sdk.Query.ColumnSet("contoso_type"));
                            if (permitTypeEntity != null)
                                if (permitTypeEntity.Attributes.Contains("contoso_type") && permitTypeEntity.Attributes["contoso_type"] != null)
                                    OptionSetValue type = (OptionSetValue)permitTypeEntity.Attributes["contoso_type"];
                                    if (type != null && type.Value == 100000001)//100000001 - Orange Value
                                        throw new InvalidPluginExecutionException("Permit Can not be Deleted as Permit Type is Orange");

Register Plugin Step as shown below,

Restrict Delete of Entity Record based on Parent Field val

Now when user tries to delete Permit Record whose Permit Type is Orange, system will throw an exception as shown below,

Restrict Delete of Entity Record based on Parent Field val

We can achieve the same requirement using Real Time Workflow but I wanted to show how it can be achieved using Plugin.

Hope this helps!