Quantcast
Channel: Microsoft Dynamics CRM Forum - Recent Threads
Viewing all articles
Browse latest Browse all 55831

Restrict Contact Creation with No Email, InvalidPluginExecutionException(); is not working.

$
0
0

Objective : To restrict the creation of Contact where email address is not there (null or empty).

Points may help:

tried same code with Pre-Operation and Pre-validation with Sync mode.

Debugger reaching throw new InvalidPluginExecutionException(); or  new InvalidPluginExecutionException(); but still it creates contacts in CRM.

below is the code please refer and suggest.

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

namespace Email_rest_val
{
public class Class1
{

}

public class PlugInEmail_rest_valIPlugin : IPlugin
{
IOrganizationService service = null;
//DataSet ds = new DataSet();
//SQLHelper _helper = new SQLHelper();
public void Execute(IServiceProvider serviceProvider)
{

IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
service = factory.CreateOrganizationService(context.UserId);
Entity entity = null;
try
{

if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{

entity = (Entity)context.InputParameters["Target"];
if (entity.LogicalName != "contact" || context.Depth > 1)
{
Logger.WriteErrorLog("Contact - " + entity.Id + " Not Processing with Depth -" + context.Depth);
return;
}

if (context.MessageName == "Create" || context.MessageName == "Update")
{
if (entity.Attributes.Contains("emailaddress1") == false
||
(entity.Attributes.Contains("emailaddress1") && string.IsNullOrEmpty(Convert.ToString(entity["emailaddress1"]))))
{
new InvalidPluginExecutionException();
}
}

}

}

catch (Exception ex)
{
Logger.WriteErrorLog("Contact - " + entity.Id + " Issue Comes in Method " + System.Reflection.MethodBase.GetCurrentMethod().Name + " as-" + ex.Message);
}
}
}
}


Viewing all articles
Browse latest Browse all 55831

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>