+ Start a Discussion
brozinickrbrozinickr 

Convert Lead to Only an Opportunity

Hello, I was wondering if someone would be able to assist me with something.

 

We are going to start leveraging leads in our organization.  All of the standard salesforce lead conversion functionality is working great, but the only problem is that we only want to create opportunities, not contacts and accounts when the lead is converted.  I have found some code online and started messing around with it, but it's still creating the account and the contact upon lead conversion.  Is there a way that I can suppress the creation of the contact and the account and just convert the opportunity?  (apex class is first part of code, visualforce is in the bottom)

 

public class leadController
{
    Public lead lObj;
    Public Id leadId;
    public leadController(ApexPages.StandardController stdController)
    {
        leadId = ApexPages.currentPage().getParameters().get('id');
        lObj = (leadid == null) ? new Lead():[SELECT Name from lead where id =: leadid];
    }
    public PageReference autoRun()
    {
        Database.LeadConvert lc = new database.LeadConvert();
        lc.setLeadId(leadId);
        LeadStatus convertStatus = [Select Id, MasterLabel from LeadStatus where IsConverted=true limit 1];
        lc.setConvertedStatus(convertStatus.MasterLabel);
        Database.LeadConvertResult lcr = Database.convertLead(lc);
        System.assert(lcr.isSuccess());
        Id oppId = lcr.getOpportunityId();
        PageReference Page = new PageReference('https://ap1.salesforce.com/'+oppId);
        Page.setRedirect(true);
        return Page;
    }
    Public PageReference RedirecttoLead(){
        PageReference Page = new PageReference('https://ap1.salesforce.com/'+leadId);
        Page.setRedirect(true);
        return Page;
    }
}

 

 

 

<apex:page standardController="lead" cache="true" action="{!autorun}" extensions="leadController" >
<br/><br/>
<apex:messages style="color:red; font-weight:bold; text-align:center;"/>
<apex:form >
<center>
<apex:commandLink value="Redirect to Lead" style="color:blue; font-weight:bold;" action="{!RedirecttoLead}"/>
</center>
</apex:form>
</apex:page>
Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox

That code will still create/update accounts and contacts. Instead, if you want to delete the account and contact after the fact, after you have the opportunity Id, query the opportunity, set the account to null and update the opportunity, then delete the account, and finally complete the rest of the logic. Something like this:

 

Id oppId = lcr.getOpportunityId();
Opportunity o = [select id,accountid from opportunity where id = :oppId];
account a = new account(id=o.accountid);
o.accountid = null;
update o;
delete a;

 

All Answers

sfdcfoxsfdcfox

That code will still create/update accounts and contacts. Instead, if you want to delete the account and contact after the fact, after you have the opportunity Id, query the opportunity, set the account to null and update the opportunity, then delete the account, and finally complete the rest of the logic. Something like this:

 

Id oppId = lcr.getOpportunityId();
Opportunity o = [select id,accountid from opportunity where id = :oppId];
account a = new account(id=o.accountid);
o.accountid = null;
update o;
delete a;

 

This was selected as the best answer
brozinickrbrozinickr

Sweet, that worked.  My only other question is it possible to have the opportunity be related to the account that lead was originially created for?

 

So for example, Lead 123 was created for Account XX.  I convert the Lead 123 into an opportunity and I want the opportunity to be related to Account XX.  Is this possible?

sfdcfoxsfdcfox
How do you know what account the lead was attached to? If you want to just attach the lead to an existing account, just use LeadConvert.setAccountId. This will assign it to the existing account instead of a new account (you can skip the deletion step). Ditto for the contact ID, you can use LeadConvert.setContactId. Or you could convert the lead, then delete the contact using LeadConvert.getContactId(), similar to deleting the account.
brozinickrbrozinickr

In our org, we build our Accounts first instead of going from lead to opportunity to an Account.  The plan is for our reps to go into the Account, go down to the related list for that Account and click the new lead button there to create the leads so they are related to that particular account.  So when reps decide, oh hey, I want to convert this lead to an opportunity, then it will be related to the Account the lead was originally created for.  

sfdcfoxsfdcfox
Well, there you go. Just pull the account into lObj on the initial query, and set the account Id like I mentioned earlier, and there you go.
Casey VerniaCasey Vernia
Where would I enter that code?