How to Restrict the deleting a record in Dynamics 365 Plugin

Summary:  How to Restrict the deleting a record in Dynamics 365 Plugin


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

namespace Opportunity1
{
    public class OpportunityPreDelete : IPlugin
    {
        public void Execute(IServiceProvider serviceProvider)
        {
            // Obtain the execution context from the service provider.
            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
            // Obtain the organization service reference.
            IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            IOrganizationService service = factory.CreateOrganizationService(context.UserId);
            if (context.InputParameters.Contains("Target") && (context.InputParameters["Target"] is EntityReference))
            {
                if (context.MessageName == "Delete")
                {
                    throw new InvalidPluginExecutionException("opportunity deletion not allowed");
                }
            }

        }

    }
}


If you need any implementation help or Support. get in touch with us.

Contact US:

Email : info@crmonce.com
Phone: +91 9493916112
Skype for Business: umamahesh@crmonce.com


0 comments:

Post a Comment

Popular Posts