Skip to main content

Notifications

Microsoft Dynamics CRM (Archived)

Convert leads to opportunity and contacts for existing accounts

Posted on by Microsoft Employee

Does anyone know the answer to this;

I want to check when new lead is getting created the account is already existed with the same name or website domain in accounts for duplication to avoid.

Thanks

*This post is locked for comments

  • Suggested answer
    Shahbaaz Ansari Profile Picture
    Shahbaaz Ansari 6,203 on at
    RE: Convert leads to opportunity and contacts for existing accounts

    Hi Sahil,

    Did you try the above code?

    If not please try it.

    Thanks,

    Shahbaaz

  • Shahbaaz Ansari Profile Picture
    Shahbaaz Ansari 6,203 on at
    RE: Convert leads to opportunity and contacts for existing accounts

    Hi Sahil,

    Did you try the above code?

    Did it worked or you faced any issue.

    Thanks,

    Shahbaaz

  • Suggested answer
    Shahbaaz Ansari Profile Picture
    Shahbaaz Ansari 6,203 on at
    RE: Convert leads to opportunity and contacts for existing accounts

    Hi Sahil,

    Below is the full code, create a plugin on the post operation of account creation with sync,

    using System;

    using System.Collections.Generic;

    using Microsoft.Xrm.Sdk;

    using Microsoft.Xrm.Sdk.Query;

    using Microsoft.Xrm.Sdk.Messages;

    namespace Lead

    {

       public class CheckAccountExist : IPlugin

       {

           public void Execute(IServiceProvider serviceProvider)

           {

               //Extract the tracing service for use in debugging sandboxed plug-ins.

               ITracingService tracingService =

                   (ITracingService)serviceProvider.GetService(typeof(ITracingService));

               // Obtain the execution context from the service provider.

               IPluginExecutionContext context = (IPluginExecutionContext)

                   serviceProvider.GetService(typeof(IPluginExecutionContext));

               // The InputParameters collection contains all the data passed in the message request.

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

               {

                   // Obtain the target entity from the input parameters.

                   Entity entity = (Entity)context.InputParameters["Target"];

                   if (entity.LogicalName != "lead")

                       return;

                   try

                   {

    //get the lead name in variable

    string leadname = entity.Attribute["name"];

    QueryExpression getAccount = new QueryExpression()

                           {

                               EntityName = "account",                          

                               Criteria =

                                   {

                                       Conditions =

                                       {

                                        new ConditionExpression("name",ConditionOperator.Equal,leadname),

                                       }

                                   }

                           };

                           EntityCollection retrieveAccount = service.RetrieveMultiple(getAccount);

    // if you get the account count > 0 then account exist with the same as lead name, in this case dont create account

                           if (retrieveAccount.Entities.Count > 0)

    {

    var qualifylead = new QualifyLeadRequest

    {

    CreateOpportunity = true,

    CreateContact = true,

    LeadId = new EntityReference(Lead.EntityLogicalName, entity.Id),

    //Status = new OptionSetValue((int)lead_statuscode.Qualified)

    };

    var qualifyIntoAccountContactRes =

    (QualifyLeadResponse)_serviceProxy.Execute(qualifylead);

    }

    entity.Attribute["parentaccount"] =  new EntityReference("account",retrieveAccount.Entities[0].Id );

    service.Update(entity);

                   }

                   catch (Exception ex)

                   {

                       tracingService.Trace("An error occurred in the FollowupPlugin plug-in : " + ex);

                       //throw new InvalidPluginExecutionException("An error occurred in the FollowupPlugin plug-in.", ex);

                   }

               }

           }

       }

    }

    if you face any issue, please let me know.

    Thanks,

    Shahbaaz

  • Suggested answer
    gdas Profile Picture
    gdas 50,085 on at
    RE: Convert leads to opportunity and contacts for existing accounts

    Hi Sahil,

    You need to update your newly created lead with the acccount id.

    Here  is the steps-

    Step 1 - > Create the Lead without account. In Post Image get the Lead ID.

    Step 2- > Check whether Account Exists or you are creating account . Using Service.Update or service.Create both the case you will get the Account ID.

    Step 3 - > Update the newly created lead with Account ID.

    Hope this helps.

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Convert leads to opportunity and contacts for existing accounts

    Hi Sahil,

    You can also use Duplicate Detection Rules.

    There you can select field of lead and criteria for exact match.

    Thank You

  • Suggested answer
    RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: Convert leads to opportunity and contacts for existing accounts

    Yes, as mentioned above, it is "Existing Account" field.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Convert leads to opportunity and contacts for existing accounts

    Thanks for the suggestion.

    how i will relate the existing account with newly created lead, Is there any field for that.

  • Suggested answer
    gdas Profile Picture
    gdas 50,085 on at
    RE: Convert leads to opportunity and contacts for existing accounts

    Hi Sahil,

    There may be scenario like .

    - The account may be  already exists

    - The account not exists in the CRM and you will create the account.

    - you need to validate domain name of the web site and also organization na,e.

    So there is lot of manual check you need to implement , best way for me is plugin on create of lead.

  • Suggested answer
    RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: Convert leads to opportunity and contacts for existing accounts

    Hi,

    You can write a plugin on create of lead. Check if there is any existing account, if yes then add that account as Existing Account in that lead. This should avoid creating a duplicate account.

    Hope this helps.

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!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans