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():
01
public
class
LeadConversion {
02
03
private
final
Lead Lead;
04
05
// The extension constructor initializes the private member
06
// variable acct by using the getRecord method from the standard
07
// controller.
08
public
LeadConversion(ApexPages.StandardController stdController) {
09
this
.Lead
=
(Lead)stdController.getRecord();
10
}
11
12
public
void
convertLead(){
13
14
if
(lead.isConverted
=
=
false
)
//to prevent recursion
15
{
16
17
Database
.LeadConvert lc
=
new
Database
.LeadConvert();
18
lc.setLeadId(lead.Id);
19
20
LeadStatus convertStatus
=
[
SELECT
Id, MasterLabel
FROM
LeadStatus
WHERE
IsConverted=
true
LIMIT
1
];
21
lc.setConvertedStatus(convertStatus.MasterLabel);
22
23
24
Database
.LeadConvertResult lcr
=
Database
.convertLead(lc);
25
System
.
assert
(lcr.isSuccess());
26
27
}
28
}
29
30
}
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
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
.ControllerLeadConvirtView
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
CUSTOM REDIRECTION ON LEAD CONVERT OPERATION IN SALESFORCE.COM
Standard=====================================================
========================================================
Override Lead Conversion
01
public
class
leadConvertController {
02
Public lead lObj;
03
Public Id leadId;
04
05
public
leadConvertController (ApexPages.StandardController stdController) {
06
System.debug(
'Called Constructor.'
);
07
leadId = ApexPages.currentPage().getParameters().get(
'id'
);
08
}
09
10
public
PageReference DoConvert() {
11
System.debug(
'In DoConvert Method'
);
12
LeadStatus convertStatus = [Select Id, MasterLabel from LeadStatus where IsConverted=
true
limit
1
];
13
14
lObj = [SELECT ID, OwnerId from lead where id =: leadid];
15
System.debug(
'Found Lead - leadID = '
+leadId+
', UserID = '
+UserInfo.getUserId());
16
17
if
(lObj.OwnerId == UserInfo.getUserId() ){
// perform the lead convert
18
System.debug(
'VALID USER; LEAD ID:'
+lObj.Id);
19
Database.LeadConvert lc =
new
database.LeadConvert();
20
lc.setLeadId(lObj.Id);
21
lc.setConvertedStatus(convertStatus.MasterLabel);
22
Database.LeadConvertResult lcr = Database.convertLead(lc);
23
System.
assert
(lcr.isSuccess());
24
25
Id oppId = lcr.getOpportunityId();
26
System.debug(
'OPPID: '
+oppId);
27
String sServerName = ApexPages.currentPage().getHeaders().get(
'Host'
);
28
sServerName =
'https://'
+ sServerName +
'/'
;
29
30
System.debug(
'REDIRECTING TO:'
+sServerName+oppId);
31
PageReference newPage =
new
pagereference(sServerName+oppId);
32
33
newPage.setRedirect(
true
);
34
return
newPage ;
35
}
36
else
{
// can't do the convert
37
System.debug(
'INVALID USER; DO NOT CONVERT'
);
38
String sServerName = ApexPages.currentPage().getHeaders().get(
'Host'
);
39
PageReference newPage =
new
pagereference(
'https://'
+sServerName+
'/apex/NotAccountOwner'
);
40
System.debug(
'REDIRECTING TO: '
+
'https://'
+sServerName+
'/apex/NotAccountOwner'
);
41
newPage.setRedirect(
true
);
42
return
newPage ;
43
}
44
return
null
;
45
}
46
}
1
<apex:page standardController=
"lead"
2
action=
"{!DoConvert}"
3
extensions=
"leadConvertController"
>
4
</apex:page>
1
../apex/ConvertThisLead?Id={!Lead.Id}