RE: Stop creating new contact and account on converting lead to opportunity
Hi dkrishna,
unfortunately this is an oob behaviour based on company name / topic / firstname / lastname values.
To avoid it you need to implement the following plugin in Pre-Operation.
Please note this plugin require 3 new attributes on lead form: Create Account, Create Contact, Create Opportunity used to drive conversion.
Plugin code:
using System;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
namespace FP.Plugin
{
public class LeadQualifyPlugin : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = factory.CreateOrganizationService(context.UserId);
EntityReference leadid = (EntityReference)context.InputParameters["LeadId"];
ColumnSet columnset = new ColumnSet(new string[] { "new_createaccount", "new_createcontact", "new_createopportunity" });
Entity leadentity = service.Retrieve(leadid.LogicalName, leadid.Id, columnset);
if (!leadentity.GetAttributeValue<bool>("new_createaccount")
&& !leadentity.GetAttributeValue<bool>("new_createcontact")
&& !leadentity.GetAttributeValue<bool>("new_createopportunity"))
{
throw new InvalidPluginExecutionException("LeadQualifyPlugin.Execute error: No lead conversion options have been selected.");
}
try
{
context.InputParameters["CreateAccount"] = leadentity.GetAttributeValue<bool>("new_createaccount");
context.InputParameters["CreateContact"] = leadentity.GetAttributeValue<bool>("new_createcontact");
context.InputParameters["CreateOpportunity"] = leadentity.GetAttributeValue<bool>("new_createopportunity");
context.InputParameters["SuppressDuplicateDetection"] = true;
}
catch (Exception ex)
{
throw new InvalidPluginExecutionException("LeadQualifyPlugin.Execute error: " + ex, ex);
}
}
}
}
Please let me know if you solve.
If you found the answer helpful, please mark as Verified
Join my network on LinkedIn Follow me on Twitter
Thank You & Best Regards
Francesco Picchi
Microsoft Dynamics CRM Consultant, Bologna+Milano, ITALY
Independent Contractor
http://www.francescopicchi.com