function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
udayar_jayamudayar_jayam 

custom lead convert button created on custom object

Hi All,

      we created a custom convert button on custom object,which is to convert  

Vinit_KumarVinit_Kumar

So whats the issue in that.

udayar_jayamudayar_jayam

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

Vinit_KumarVinit_Kumar

Udyarar,

 

Try below :-

 

public class LeadConversion {

    private final Lead Lead;
    
    // The extension constructor initializes the private member
    // variable acct by using the getRecord method from the standard
    // controller.
    public LeadConversion(ApexPages.StandardController stdController) {
        this.Lead = (Lead)stdController.getRecord();
    }
	
	public void convertLead(){
	
	if (lead.isConverted == false) //to prevent recursion
      {
      
        Database.LeadConvert lc = new Database.LeadConvert();
        lc.setLeadId(lead.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());
	
		}
	}
	
	}

 

udayar_jayamudayar_jayam

Hi vinit kumar,

         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.

 

Regards

udaya

Vinit_KumarVinit_Kumar

You need to create a VF page and use this class as an extension to it.Then,you can create a Custom button and attach the VF page to it.

udayar_jayamudayar_jayam

Here this VF page code is correct or not,if wrong means tell me the correct one

<apex:page Controller="LeadConversion" 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>

 

thanks,

regards,

Udaya

Vinit_KumarVinit_Kumar

Change this line from

 

<apex:page Controller="LeadConversion" cache="true" action="{!autorun}" extensions="leadController" >

 

to

 

<apex:page StandardController="Lead" cache="true" action="{!convertLead}" extensions="LeadConversion" >

 

 

udayar_jayamudayar_jayam

geting this error

 

unknown Constructor'Lead/Conversion.LeadConversion() Create a apex method 'LeadConversion.LeadConversion()'

 

Please check this once.

thanks,

regards.

udaya.

SF DEVSF DEV
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.