We created a custom convert button on custom object, which is to convert a lead into account, opportunity and contact. Can anyone tell how to do this convertion either by code or anyother way
Thanks for the code,I have saved this code in apex class then what i have to do am new to apex coding plz tell me immediately its very urgent. waiting for ur reply.Reply me as soon as possible.
Hi Vinit, 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.
I will need to create my own "convert lead" button because I need to ensure that certain fields are updated when the button is clicked.
However, I was wondering if there is a quick way of doing this, because usually when converting a lead, we have the option of creating an opportunity, contact and account. Can I maybe create a button that, when clicked, populates the fields that I need to populate and then reroutes to the standard lead conversion process?
There is a Database.leadConvert method available documented in LeadConvert Class.
So yes, you could create e.g. a JavaScript button that calls your own WebService method that does whatever population you need and then calls Database.leadConvert.
Of course you can. But I doubt if this is a quick way as you are looking for. This is what I have.
Create a custom button of Content source type URL under Lead object.
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 .
And have the below code in the ControllerLeadConvirtView
public class ControllerLeadConvertView {
public Id leadId;
public ControllerLeadConvertView(ApexPages.StandardController stdController){
leadId = ApexPages.CurrentPage().getParameters().get('id');
//You can retrieve the Lead object and do whatever the populating here
//populateFields(); if you need
}
public void populateFields(){
//You can do your stuffs here may be by taking as an action of UI events
}
//This will convert the Lead and redirect the user into newly created Account's detail page
public PageReference convertLead(){
LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
Database.LeadConvert lc = new Database.LeadConvert();
lc.setLeadId(leadId);
lc.setConvertedStatus(convertStatus.MasterLabel);
lc.setDoNotCreateOpportunity(true);
try{
Database.LeadConvertResult lcr = Database.convertLead(lc);
convertedAccountId = lcr.getAccountId();
}
catch(Exception e){
System.Debug('Error - ControllerLeadConvertView.convertLead - Exception [' + e.getMessage() + ']');
return null;
}
String sServerName = ApexPages.currentPage().getHeaders().get('Host');
sServerName = 'https://'+sServerName+'/';
PageReference retPage = new PageReference(sServerName+convertedAccountId);
retPage.setRedirect(true);
return retPage;
}
}
CUSTOM REDIRECTION ON LEAD CONVERT OPERATION IN SALESFORCE.COM
Standard
When you convert a Lead, you will be taken to newly created Account’s detail screen. Thats the standard salesforce.com behaviour.
But what if, you want to show newly created Contact’s detail screen (instead of Account screen), upon Lead conversion? Unfortunately, there is no such setting available in salesforce (yet).
Many people might have achieved it, by some or the other workaround. And here is my trick.
Create following Apex Controller class, to use it along with the VF page.
public 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;
}
}
Now edit the Lead layout and remove the standard Convert button and add the custom Convert button.
Thats it!
Hope thats helpful!
Note: This solution works only when, lead conversion is done with new Account/Contact during lead conversion process. It may not work, if existing Account/Contact is chosen, during lead conversion process/screens.
One of the most annoying things to me about Salesforce is that, unless you have a private sharing model for lead processes, anyone can convert anyone else's leads. It causes great distress at many organizations, but mostly people tend to live with it.
Here's a simple little solution - Make a custom button that overrides the Salesforce "Convert" button.
First, start off with a page controller that calls Database.ConvertLead():
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}