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
Anto HotelbedsAnto Hotelbeds 

Lead conversion problems

Hi,

 

I really need a hand on this, I have been struggling for around 5 hours and I didnt get anything...

 

I had created a custom conversion button on my visualforce page. This button will call a method declared on my leadextension controller. Yesterday it worked without any problem....today I created a few new fields and change the code a bit and didnt work. I made the conversion method as simple as possible because I thought that way I would find my problem. Still getting the same error message...

 

This is the visualforce page where the button is and calls the method:

 

<apex:page standardController="Lead" showHeader="true" extensions="LeadExtension">
    <apex:form >
        <apex:pageBlock title="Account Creation" id="Account_Creation" mode="Read">
            <apex:pageMessages />
            <apex:pageBlockButtons >
                <apex:commandButton value="Create Account" action="{!CustomConversion}"/>
                <apex:commandButton value="Cancel" action="{!cancel}"/>
            </apex:PageBlockButtons>
            <apex:pageBlockSection columns="2">
                    <apex:pageBlockSectionItem >
                        <apex:outputLabel value="Commercial Name"/>
                        <apex:outputLabel value="{!lead.Company}"/>
                    </apex:pageBlockSectionItem>
                    <!--MORE FIELD TO SHOW DECLARE IN HERE-->
              </apex:pageblockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

 And this id the code of my Class that contains the method:

 

public class LeadExtension {
    public class MyException extends Exception{}
    private Lead l1;
    private Lead l2;
    String result='Click on an account to fetch the data';
        
    public LeadExtension(ApexPages.StandardController stdController) {
       this.l1 = (Lead)stdController.getRecord();
       this.l2= [Select Name,Customer_Connectivity__c,Acquisition_Channel_Description__c, OwnerId, Fiscal_Number_2__c, Fiscal_Number_1__c, 			      Relationship__c,Commercial_Group__c,Commission_Group__c,Fiscal_Name__c,Street_fiscal__c,City_fiscal__c,Post_Code_fiscal__c,Country_Fiscal__c,Province_Fiscal__c,
       			FirstName,LastName,Salutation,Contact_Role__c,Contact_Job_Titel__c,Email,Phone,Contact_Language__c,
       			Currency_to_invoice__c,Billing_Language__c,WhiteLabel_Type__c,Web_Currency__c,RecordTypeId,Website,Number_of_Employees__c,
       			Bank_Name__c,Bank_Account_Number__c,Bank_Account_Holder__c,Bank_Account_Country__c,Routing_Number__c,   			
       			Email_Admin_Finance__c,Fax_Admin_Finance__c,Phone_Admin_Finance__c,First_Name_Admin_Finance__c,
       			Last_Name_Admin_Finance__c,Commercial_Contact_equals_Admin_Contact__c,Web_User_1_Last_Name__c,Web_User_2_Last_Name__c,
       			Web_User_3_Last_Name__c,Web_User_4_Last_Name__c,Web_User_1_Salutation__c,Web_User_2_Salutation__c,Web_User_3_Salutation__c,
       			Web_User_4_Salutation__c,Web_User_1_Name__c,Web_User_2_Name__c,Web_User_3_Name__c,Web_User_4_Name__c,Web_User_Email_1__c,
       			Web_User_Email_2__c,Web_User_Email_3__c,Web_User_Email_4__c From Lead Where (Id=:l1.Id)];
    }
     
    
    public pageReference CustomConversion()
    {
    	/*database.Leadconvert lc = new database.Leadconvert();
    	Database.Leadconvertresult lcr;
    	lc.setLeadId(l2.Id);
    	lc.setConvertedStatus('Converted');
    	lc.setDoNotCreateOpportunity(True);
    	Integer NumErrores=0;
    	String ErrorMessage;
    	
    	lcr=Database.convertLead(lc);
        pageReference pageRef= apexpages.currentPage();//new PageReference('/apex/CustomAccount?id='+lcr.getAccountId());
        return pageRef;*/
        
        Database.LeadConvert lc = new database.LeadConvert();
	lc.setLeadId(l1.id);

	lc.setConvertedStatus('Converted');

	Database.Leadconvertresult lcr=Database.convertLead(lc);
	pageReference pageRef=new PageReference('/apex/CustomAccount?id='+lcr.getAccountId());
	return ApexPages.currentPage();
    }

 And the error message I get is:

------------------------------------------------------------------------------------------------------

Visualforce Error
 


System.DmlException: ConvertLead failed. First exception on row 0; first error: DUPLICATE_VALUE, duplicate value found: []

Error is in expression '{!CustomConversion}' in component <apex:page> in page conversionscreen

 

 

Class.LeadExtension.CustomConversion: line 52, column 1

-----------------------------------------------------------------------------------------------------------------

 

The line 52 is the one that says: 

 

Database.Leadconvertresult lcr=Database.convertLead(lc);

 

Really appreciate your help. Regards,

 

Antonio

Best Answer chosen by Admin (Salesforce Developers) 
sornasorna

When you convert the lead, an account and contact will be created. so it seems there is some unique field on either account or contact which is having duplicate values. Check the lead conversion field mapping and unique fields of accounts and contacts...

All Answers

JitendraJitendra

Hi,

Please try below apex class:

 

public class LeadExtension {
    public class MyException extends Exception{}
    private Lead l1;
    String result='Click on an account to fetch the data';
        
    public LeadExtension(ApexPages.StandardController stdController) {
       this.l1 = (Lead)stdController.getRecord();
    }
     
    
    public pageReference CustomConversion()
    {
        
        Database.LeadConvert lc = new Database.LeadConvert();
	    lc.setLeadId(l1.id);

		LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
		lc.setConvertedStatus(convertStatus.MasterLabel);

		Database.Leadconvertresult lcr=Database.convertLead(lc);
		pageReference pageRef=new PageReference('/apex/CustomAccount?id='+lcr.getAccountId());
		return ApexPages.currentPage();
    }
}

 

Anto HotelbedsAnto Hotelbeds

Hi,

 

I got the same error...I created a new class with the code you parse, change the class the visualforce pages calls to, and got exactly the same error....Any other ideas please?

 

thanks

sornasorna

When you convert the lead, an account and contact will be created. so it seems there is some unique field on either account or contact which is having duplicate values. Check the lead conversion field mapping and unique fields of accounts and contacts...

This was selected as the best answer
JitendraJitendra

Do you have any custom fields on your Account or Contact objects that are defined as Unique, and No duplicate values allowed, or any Validation rule on account to avoid duplicate value?

Anto HotelbedsAnto Hotelbeds

Thanks a lot you both for your help.

 

You were right, there was no error in the code but in the value of a field. I had a lead field that was being mapped with an account field. This field was set in the account as unique. In my test I walways gave this field the value TEST, so when converting, because there was already an account with the value TEST so, when converting I was getting the duplicate value error.

 

I spent two hours looking through all my code and eventually the error was other....

 

Thanks again.