You need to sign in to do that
Don't have an account?

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.
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
and You can add a logic to check this flag before converting i.e. creating new Contact and Account records.
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.
leadObj.Convertedlead__c = TRUE;
update leadObj;
waiting for your reply.
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.
if(lead.IsConverted == true) {
alert('Lead has already been converted.');
return false;
}
regards,
lavanya.
thanks,
I think u can use add error message on sobjects to display error message to the customers.
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.
Regards,
Lavanya.