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
LavanyaLavanya 

Checking duplicate records on clicking the custom button“convert”

Hi I am having a custom "convert" button on custom object. Once i click the convert button it will create a account and contact. Once again i click convert button i must tell that the record already exist in account and contact "Duplicate won't be allowed". This "convert" button is similar to our lead convert button. Kindly anyone tell how to resolve this.Kindly tell me code for this.

Here i have given my apex class code:

Code:

 

public class LeadConversion {

    public PageReference RedirecttoLead()
     {
        String currentLead = '/' + leadObj.Id;
        PageReference pageRef = new PageReference(currentLead);
        return pageRef;
     }

     private CustomLead__c leadObj;
    
    // The extension constructor initializes the private member
    // variable acct by using the getRecord method from the standard
    // controller.
    public LeadConversion(ApexPages.StandardController stdController)
     {
     System.debug('******Sai******');
             leadObj = (CustomLead__c)stdController.getRecord();
    }
    
    public void convertLead(){
    
        Account acc = new Account();
        acc.Name = leadObj.Name;
        acc.Companynamelead__c = leadObj.CompanyName__c;
      
       try 
       { 
        insert acc; 
       }
        Catch (Exception ex1)
        {
        
         ex1.getmessage();
        }
        Contact cc = new Contact();
        cc.Lastname = leadObj.Name;
        cc.Lastname_lead__c = leadObj.LastName_c__c;
        cc.AccountId=acc.Id;

      
       try 
       { 
        insert cc; 
       }
        Catch (Exception ex2)
        {
        
         ex2.getmessage();
        }
        
        Opportunity opp = new Opportunity();
        opp.Name = leadObj.Name;
        opp.AccountId=acc.Id;
        opp.opp_namelead__c = leadObj.opp_name__c;
        opp.OPP_DateOfBirth__c = '20.03.1976';
        opp.StageName = 'Qualification';
        opp.closedate = date.today()+3; 

       
        try 
         { 
         insert opp; 
        }
        Catch (Exception ex3)
        {
         
         ex3.getmessage();
        }
        
      
        
    }
    
    }

 

Can anyone tell how resolve this.

 

Thanks, Regards, lavanya.

Best Answer chosen by Admin (Salesforce Developers) 
Prafull G.Prafull G.

You have to add the code in method convertLead().

 

Add it to end

..

.

.     

try 
         { 
         insert opp; 
        }
        Catch (Exception ex3)
        {
         
         ex3.getmessage();
        }
        
      leadObj.Convertedlead__c = TRUE;
      update leadObj;

        
    }

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

 

Once you are done with the above change. You need to add another logic at the begining of the method to check.. if the flag is already Checked then you need to return without inserting account, contact and opportunity.

All Answers

Prafull G.Prafull G.
Just like standard Conversion functionality, You can create a Boolean field to mark at true when the button is clicked once and Lead is converted.
and You can add a logic to check this flag before converting i.e. creating new Contact and Account records.
LavanyaLavanya
how to use this boolean flag in the code
Prafull G.Prafull G.
Create a flag on Lead as
Name:Converted
Data Type: Checkbox

In your code, Once you are doing DML to insert Account, Contact and Opportunity. Update the current Lead record to set this field as TRUE.
Example
leadObj.Converted__c = TRUE;
update leadObj;

Hope it helps.
LavanyaLavanya
Thanks for your reply. I have created a checkbox "Convertedlead". In my apex code where to use this
leadObj.Convertedlead__c = TRUE;
update leadObj;

waiting for your reply.
Prafull G.Prafull G.

You have to add the code in method convertLead().

 

Add it to end

..

.

.     

try 
         { 
         insert opp; 
        }
        Catch (Exception ex3)
        {
         
         ex3.getmessage();
        }
        
      leadObj.Convertedlead__c = TRUE;
      update leadObj;

        
    }

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

 

Once you are done with the above change. You need to add another logic at the begining of the method to check.. if the flag is already Checked then you need to return without inserting account, contact and opportunity.

This was selected as the best answer
Bhawani SharmaBhawani Sharma
Write javascript code on your custom button. Like
if(lead.IsConverted == true) {
alert('Lead has already been converted.');
return false;
}
LavanyaLavanya
Hi thanks a lot its working.
regards,
lavanya.
LavanyaLavanya
Hi i need to display a error message if the account is already exist.For this where i need to write the code. Is their any option to display this error message either in Apex or VF. kindly reply me
thanks,
SurpriseSurprise

I think u can use add error message on sobjects to display error message to the customers.

Prafull G.Prafull G.
Ok. So when you are doing check for already converted lead (using the flag we created earlier) in convertLead() method. At this point you need to do that.

If you found the flag as TRUE, You can add message as
ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.INFO,'Lead is already coverted to Account and Contact OR SOME CUSTOM MESSAGE'));

NOTE: The above is apex solution and you have to use <apex:pageMessages> component on vf page to show the message.

Let me know if this is what you are looking for.
LavanyaLavanya
Hi thanks a lot, its working
Regards,
Lavanya.