public class LeadClass{
private Lead leadToConvert = null;
public LeadClass(){
//Lead myLead = new Lead(LastName = 'Fry', Company='Fry And Sons');
//insert myLead;
String leadId = System.currentPageReference().getParameters().get('leadId');
leadToConvert = [SELECT Id,Phone, Company
FROM Lead WHERE Id = :leadId];
Database.LeadConvert lc = new database.LeadConvert();
lc.setLeadId(leadToConvert.id);
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());
}
}
==============
<apex:page controller="LeadClass">
</apex:page>
======================
https://developer.salesforce.com/forums/?id=906F0000000MIzZIAW__Trigger and test
https://developer.salesforce.com/forums/?id=906F00000008zJVIAY---trigger and test2
http://stackoverflow.com/questions/30092286/lead-conversion-in-salesforce-using-apex
https://developer.salesforce.com/forums/?id=906F0000000AdlYIAS---good example
https://developer.salesforce.com/forums/?id=906F00000008xTWIAY-----with simple example
http://salesforce.stackexchange.com/questions/4756/convert-lead-through-apex-but-should-not-create-opportunity
====
==https://developer.salesforce.com/forums/?id=906F00000008xTWIAY
apex/LeadConversion?leadId={!Lead.Id}'
https://c.ap2.visual.force.com/apex/ld?leadId='00Q2800000LVmZh'
01publicclassLeadConversion {0203privatefinalLead Lead;0405// The extension constructor initializes the private member06// variable acct by using the getRecord method from the standard07// controller.08publicLeadConversion(ApexPages.StandardController stdController) {09this.Lead=(Lead)stdController.getRecord();10}1112publicvoidconvertLead(){1314if(lead.isConverted==false)//to prevent recursion15{1617Database.LeadConvert lc=newDatabase.LeadConvert();18lc.setLeadId(lead.Id);1920LeadStatus convertStatus=[SELECTId, MasterLabelFROMLeadStatusWHEREIsConverted=trueLIMIT1];21lc.setConvertedStatus(convertStatus.MasterLabel);222324Database.LeadConvertResult lcr=Database.convertLead(lc);25System.assert(lcr.isSuccess());2627}28}2930}Can we control the custom lead conversion class(LeadConversion), not to create account. I just wanted to create only contact when converting the lead. Is it controlling in the code anyway.
Thanks.
Replicating the convert lead button
2 Answers
Leadobject.Set the URL as
/apex/LeadConvertView?id={!Lead.Id}(LeadConvertView is just a blank page with Lead StandardController and controller extension to redirect user into real conversion page).Have below code in
LeadConvertView.ControllerLeadConvirtView++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++CUSTOM REDIRECTION ON LEAD CONVERT OPERATION IN SALESFORCE.COM
Standardpublic class LeadConvertLandingController{ Id newId = null; public LeadConvertLandingController(){ newId = ApexPages.CurrentPage().getParameters().get('newid'); } public PageReference redirect(){ Lead convertedLead = [select ConvertedContactId from Lead where id=:newId]; PageReference pRef = new PageReference('/' + convertedLead.ConvertedContactId); pRef.setRedirect(true); return pRef; } }<apex:page controller="LeadConvertLandingController" action="{!redirect}"> please wait.... </apex:page>/lead/leadconvert.jsp?retURL=/{!Lead.Id}&id={!Lead.Id}&saveURL=/apex/LeadConvertLanding=============================================================================================================Override Lead Conversion
01publicclassleadConvertController {02Public lead lObj;03Public Id leadId;0405publicleadConvertController (ApexPages.StandardController stdController) {06System.debug('Called Constructor.');07leadId = ApexPages.currentPage().getParameters().get('id');08}0910publicPageReference DoConvert() {11System.debug('In DoConvert Method');12LeadStatus convertStatus = [Select Id, MasterLabel from LeadStatus where IsConverted=truelimit1];1314lObj = [SELECT ID, OwnerId from lead where id =: leadid];15System.debug('Found Lead - leadID = '+leadId+', UserID = '+UserInfo.getUserId());1617if(lObj.OwnerId == UserInfo.getUserId() ){// perform the lead convert18System.debug('VALID USER; LEAD ID:'+lObj.Id);19Database.LeadConvert lc =newdatabase.LeadConvert();20lc.setLeadId(lObj.Id);21lc.setConvertedStatus(convertStatus.MasterLabel);22Database.LeadConvertResult lcr = Database.convertLead(lc);23System.assert(lcr.isSuccess());2425Id oppId = lcr.getOpportunityId();26System.debug('OPPID: '+oppId);27String sServerName = ApexPages.currentPage().getHeaders().get('Host');28sServerName ='https://'+ sServerName +'/';2930System.debug('REDIRECTING TO:'+sServerName+oppId);31PageReference newPage =newpagereference(sServerName+oppId);3233newPage.setRedirect(true);34returnnewPage ;35}36else{// can't do the convert37System.debug('INVALID USER; DO NOT CONVERT');38String sServerName = ApexPages.currentPage().getHeaders().get('Host');39PageReference newPage =newpagereference('https://'+sServerName+'/apex/NotAccountOwner');40System.debug('REDIRECTING TO: '+'https://'+sServerName+'/apex/NotAccountOwner');41newPage.setRedirect(true);42returnnewPage ;43}44returnnull;45}46}1<apex:page standardController="lead"2action="{!DoConvert}"3extensions="leadConvertController">4</apex:page>1../apex/ConvertThisLead?Id={!Lead.Id}