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
Hi Sahil,
Did you try the above code?
If not please try it.
Thanks,
Shahbaaz
Hi Sahil,
Did you try the above code?
Did it worked or you faced any issue.
Thanks,
Shahbaaz
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
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.
Hi Sahil,
You can also use Duplicate Detection Rules.
There you can select field of lead and criteria for exact match.
Thank You
Yes, as mentioned above, it is "Existing Account" field.
Thanks for the suggestion.
how i will relate the existing account with newly created lead, Is there any field for that.
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.
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.
Stay up to date on forum activity by subscribing. You can also customize your in-app and email Notification settings across all subscriptions.
André Arnaud de Cal... 291,253 Super User 2024 Season 2
Martin Dráb 230,188 Most Valuable Professional
nmaenpaa 101,156