web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Plugin code for checking duplication and creation of new contact and activity

(0) ShareShare
ReportReport
Posted on by 37

Hi,

I have a requirement where there should be duplicate check done on case entity for First name and last name and based on the value, if its a duplicate then a new activity needs to be created and if its not a duplicate, then a new contact and new activity needs to be created. Can someone help me with the code for this?

Thanks in Advance,

JB

*This post is locked for comments

I have the same question (0)
  • Verified answer
    Kokulan Profile Picture
    18,054 on at

    You will have to create a plugin that triggers OnCreate (Post Op) of the case that does the following

    01. In the plugin, query the Case Entity with the current case's FN & LN as filter and check the number of records returned.

    02. If it returns more than one, that confirms you have duplicates. if there is only 1, means there are no duplicates.

    03. You can now do whatever you want to do based on number of records returned. If the returned records count  =1, create new contact and activity. And if the count > 1, create new activity.

    Let me know if you need any code examples.

  • CRM Reports Profile Picture
    37 on at

    Yes Kokulan, it would be great, if you could help me with some sample codes :)

  • Verified answer
    Kokulan Profile Picture
    18,054 on at

    Hi

    Please see an example of how you could check dups and create contact/activity.

    ScreenClip-_5B00_726_5D00_.png

    ScreenClip-_5B00_726_5D00_.png


    using System;
    using System.ServiceModel;
    using Microsoft.Xrm.Sdk;
    using Microsoft.Xrm.Sdk.Query;

    namespace CrmVSSolution.Plugins
    {

    /// <summary>
    /// PostOperationincidentCreate Plugin.
    /// </summary>
    public class PostOperationincidentCreate: PluginBase
    {
    public PostOperationincidentCreate(string unsecure, string secure): base(typeof(PostOperationincidentCreate)){}

    protected override void ExecuteCrmPlugin(LocalPluginContext localContext)
    {
    if (localContext == null)
    {
    throw new InvalidPluginExecutionException("localContext");
    }

    Entity entCase = (Entity)localContext.PluginExecutionContext.InputParameters["Target"];

    // if FN or LN is not set on the case, dont continue
    if (!entCase.Attributes.Contains("new_firstname") || !entCase.Attributes.Contains("new_lastname"))
    return;

    if (!DuplicatesExist(entCase, localContext.OrganizationService))
    {
    // No Duplicate found

    // Create contact
    Entity contact = new Entity();
    contact.LogicalName = "contact";
    contact["firstname"] = entCase["new_firstname"];
    contact["lastname"] = entCase["new_lastname"];
    localContext.OrganizationService.Create(contact);

    // Create activity
    Entity task = new Entity("task");
    task["subject"] = "Test Task";
    task["description"] = "Testing";
    task["regardingobjectid"] =entCase.ToEntityReference();
    localContext.OrganizationService.Create(task);

    }
    else
    {
    // Duplicates Found

    // Create activity
    Entity task = new Entity("task");
    task["subject"] = "Test Task";
    task["description"] = "Testing";
    task["regardingobjectid"] = entCase.ToEntityReference();
    localContext.OrganizationService.Create(task);
    }

    }

    private bool DuplicatesExist(Entity entCase, IOrganizationService CrmService)
    {
    var QEincident = new QueryExpression("incident");
    QEincident.ColumnSet.AddColumns("title", "ticketnumber", "createdon", "incidentid");
    QEincident.Criteria.AddCondition("new_firstname", ConditionOperator.Equal, entCase["new_firstname"]);
    QEincident.Criteria.AddCondition("new_lastname", ConditionOperator.Equal, entCase["new_lastname"]);
    return CrmService.RetrieveMultiple(QEincident).Entities.Count > 1;

    }
    }
    }

  • CRM Reports Profile Picture
    37 on at

    Thanks a lot Kokulan, this really helps me :)

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
JS-09031509-0 Profile Picture

JS-09031509-0 3

#2
AS-17030037-0 Profile Picture

AS-17030037-0 2

#2
Mark Eckert Profile Picture

Mark Eckert 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans