Scenario: XYZ Pvt Ltd, wants to implement business logic of Delete prevention if value of Is Delete Field is No on Opportunity Entity. CRM 2013 will prevent deltetion of record if value of Is Delete Field is set to No and vice versa.
Work Around: We have added a custom field on Opportunity Entity with Display name 'Is Delete' & Schema Name 'IsDelete'.
Solution: Below is the CRM 2013 Prevent Delete Plugin code.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Crm.Sdk.Messages;
namespace PreventAccountDelete
{
public class Class1:IPlugin
{
string _prefix = "new_";
string NEWLINE = "<br/>";
string fieldName_IsDelete = null;
public void Execute(IServiceProvider serviceProvider)
{
try
{
// Preparing Configurations
// Base Data for plugin
fieldName_IsDelete = GetFieldName("isdelete");
string strDebug = "";
string entityName = "account";
// Obtain the execution context from the service provider.
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is EntityReference)
{ }
else return;
EntityReference entityRef = (EntityReference)context.InputParameters["Target"];
// Checking if Entity Name != entityName
if (entityRef.LogicalName != entityName)
return;
// Creating IOrganization Factory
IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
// Creating IOrganization Service
// Retrieving from Factory
IOrganizationService service = factory.CreateOrganizationService(context.UserId);
strDebug = " Plugin Debug started... at " + DateTime.Now + NEWLINE;
/// Quering Account
/// Getting Account Field - isDelete
QueryExpression query = new QueryExpression
{
EntityName = entityName,
ColumnSet = new ColumnSet(fieldName_IsDelete), //col[2], col[5], col[6]), // col[2 = batchcourse, col[5 facultycourse
Criteria =
{
FilterOperator = LogicalOperator.And,
Conditions =
{
new ConditionExpression
{
AttributeName = "accountid",
Operator = ConditionOperator.Equal,
Values = {context.PrimaryEntityId.ToString()}
}
}
}
};
// Retrieving Entity
Entity _Entity = service.Retrieve(entityName, context.PrimaryEntityId, new ColumnSet(fieldName_IsDelete));
SetStateRequest setStatus = new SetStateRequest();
bool isDelete = false;
if (IsContain(_Entity, fieldName_IsDelete))
{
isDelete = (bool)_Entity[fieldName_IsDelete];
if (!isDelete)
{
throw new InvalidPluginExecutionException(" Not Authorize to Delete the Record ");
//return;
}
//else
// throw new InvalidCastException(" Authorize to Delete the Record " + NEWLINE + strDebug);
}
}
catch (Exception ex)
{
throw new InvalidPluginExecutionException(ex.Message);
}
}
private string GetFieldName(string name)
{
return this._prefix + name;
}
protected bool IsContain(Entity ent, string attribute)
{
return ent.Attributes.Contains(attribute);
}
}
}
#AW Customization Consultant Maison Consulting & Solutions