Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8843377
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:12:16+00:00 2026-06-14T11:12:16+00:00

I am working on a CRM Dynamics Plugin. There is a field on custom

  • 0

I am working on a CRM Dynamics Plugin. There is a field on custom entity named “email”. I want to make sure that for two entity records email addresses should be unique. For that purpose I have written following code:

public class Class1 : IPlugin
{
    public void Execute(IServiceProvider serviceProvider)
    {
        // Obtain the execution context from the service provider.
        Microsoft.Xrm.Sdk.IPluginExecutionContext context = (Microsoft.Xrm.Sdk.IPluginExecutionContext)
            serviceProvider.GetService(typeof(Microsoft.Xrm.Sdk.IPluginExecutionContext));

        // Get a reference to the organization service.
        IOrganizationServiceFactory factory =
        (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
        IOrganizationService service = factory.CreateOrganizationService(context.UserId);


        // The InputParameters collection contains all the data passed in the message request.
        if (context.InputParameters.Contains("Target") &&
            context.InputParameters["Target"] is Entity)
        {
            Entity entity = (Entity)context.InputParameters["Target"];

            //</snippetAccountNumberPlugin2>

            // Verify that the target entity represents an account.
            // If not, this plug-in was not registered correctly.
            if (context.MessageName.ToUpper() == "CREATE")
            {
                if (entity.LogicalName == "new_assignment1entity")
                {
                    try
                    {
                        QueryExpression query = new QueryExpression("new_assignment1entity");
                        query.ColumnSet.AddColumns("new_email");
                        EntityCollection result1 = service.RetrieveMultiple(query);
                        foreach (var a in result1.Entities)
                        {
                            int size = result1.Entities.Count;
                            if (a.Attributes["new_email"].ToString().Equals(entity["new_email"]))
                                throw new InvalidPluginExecutionException("Duplicate Email found!");
                        }
                    }
                    catch (FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault>)
                    {
                        //You can handle an exception here or pass it back to the calling method.
                        throw new InvalidPluginExecutionException("Some problem occurred while Querying Records!");
                    }
                }
            }
            else if (context.MessageName.ToUpper() == "UPDATE")
            {
                if (entity.LogicalName == "new_assignment1entity")
                {
                    try
                    {
                        QueryExpression query = new QueryExpression("new_assignment1entity");
                        query.ColumnSet.AddColumns("new_email");
                        EntityCollection result1 = service.RetrieveMultiple(query);
                        foreach (var a in result1.Entities)
                        {
                            int size = result1.Entities.Count;
                            if (a.Attributes["new_email"].ToString().Equals(entity["new_email"]))
                                throw new InvalidPluginExecutionException("Duplicate Email found!");
                        }
                    }
                    catch (FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault>)
                    {
                        //You can handle an exception here or pass it back to the calling method.
                        throw new InvalidPluginExecutionException("Some problem occurred while Querying Records!");
                    }
                }
            }
        }
    }
}

When User creates a new entity record with duplicate email address this code works and shows a dialog box printing error message. But when User edit an existing record (update and existing record) and makes the email address duplicate then this code does not work and updated record with duplicated email address saved.
I am guessing that Context message with UPDATE else part is not working.
Please help me out.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T11:12:18+00:00Added an answer on June 14, 2026 at 11:12 am

    I resolved this issue. The problem why only Create execution flow was running and not Update is that I had only registered the plugin for create message step. To overcome this issue, I added a new step in same plugin and registered it with update message as show in following screenshot:

    enter image description here

    And it worked like charm.

    Apart from this, @GregOwens mentioned very helpful points.These should follow as best practices in CRM Development.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a Microsoft Dynamics CRM 2011 plugin attached to SalesOrder entity on
I'm working on a solution that has two applications 1) Wordpress based CRM (frontend)
I'm working with Dynamics CRM 2011 Online and trying to refactor some code that
I am working on CRM 2011. We have a custom entity Orders. One of
I am creating a plugin for Microsoft Dynamics CRM 4 that will change the
I am working with some Date fields in Microsoft Dynamics CRM 2011 and want
I'm working on CRM 2011 (newbie...) and created a plugin that checks if incidents
I am working with a CRM product that uses ASP.net MVC 3.0, Entity Framework
this is a working example of a plugin that I have written for CRM
I am working on a CRM system that allows me to write custom HTML

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.