Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

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

Posted on by 35

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

  • CRM Reports Profile Picture
    CRM Reports 35 on at
    RE: Plugin code for checking duplication and creation of new contact and activity

    Thanks a lot Kokulan, this really helps me :)

  • Verified answer
    Kokulan Profile Picture
    Kokulan 18,048 on at
    RE: Plugin code for checking duplication and creation of new contact and activity

    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
    CRM Reports 35 on at
    RE: Plugin code for checking duplication and creation of new contact and activity

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

  • Verified answer
    Kokulan Profile Picture
    Kokulan 18,048 on at
    RE: Plugin code for checking duplication and creation of new contact and activity

    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.

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans