HI Swathi, Try the code mentioned in http://theblogreaders.com/automatically-convert-lead-to-account-contact-and-opportunity-using-apex-trigger/
trigger LeadAutoContactConverter on Lead (after insert) {
LeadStatus convertStatus = [Select MasterLabel from LeadStatus where IsConverted = true limit 1];
List<Database.LeadConvert> leadConverts = new List<Database.LeadConvert>();
for (Lead lead: Trigger.new) {
if (!lead.isConverted && lead.Status == 'Open' ) {
Database.LeadConvert lc = new Database.LeadConvert();
String opportunityName = lead.Name;
lc.setLeadId(lead.Id);
//lc.setAccountId(AccountId); // you can set the AccountId instead of create a new account and contact is created the mention account
lc.setSendNotificationEmail(false);
lc.setOpportunityName(opportunityName);
//lc.setDoNotCreateOpportunity(true); // Optional to create a Opportunity
lc.setConvertedStatus(convertStatus.MasterLabel);
leadConverts.add(lc);
}
}
if (!leadConverts.isEmpty()) {
List<Database.LeadConvertResult> lcr = Database.convertLead(leadConverts);
}
}
Test Class:
@isTest
private class TestLeadTrigger {
static testMethod void TestLeadTrigger() {
Test.startTest();
Lead l = new Lead(FirstName = 'FName', LastName = 'LName', Company = 'Test Account', Status = 'Open', Email='info@theblogreaders.com');
insert l;
system.assertEquals(l.FirstName, 'FName');
test.stopTest();
}
}
Hope this helps you. Please mark this answer as best so that others facing the same issue will find this information useful. Thank you
HI Swathi, Try the code mentioned in http://theblogreaders.com/automatically-convert-lead-to-account-contact-and-opportunity-using-apex-trigger/
trigger LeadAutoContactConverter on Lead (after insert) {
LeadStatus convertStatus = [Select MasterLabel from LeadStatus where IsConverted = true limit 1];
List<Database.LeadConvert> leadConverts = new List<Database.LeadConvert>();
for (Lead lead: Trigger.new) {
if (!lead.isConverted && lead.Status == 'Open' ) {
Database.LeadConvert lc = new Database.LeadConvert();
String opportunityName = lead.Name;
lc.setLeadId(lead.Id);
//lc.setAccountId(AccountId); // you can set the AccountId instead of create a new account and contact is created the mention account
lc.setSendNotificationEmail(false);
lc.setOpportunityName(opportunityName);
//lc.setDoNotCreateOpportunity(true); // Optional to create a Opportunity
lc.setConvertedStatus(convertStatus.MasterLabel);
leadConverts.add(lc);
}
}
if (!leadConverts.isEmpty()) {
List<Database.LeadConvertResult> lcr = Database.convertLead(leadConverts);
}
}
Test Class:
@isTest
private class TestLeadTrigger {
static testMethod void TestLeadTrigger() {
Test.startTest();
Lead l = new Lead(FirstName = 'FName', LastName = 'LName', Company = 'Test Account', Status = 'Open', Email='info@theblogreaders.com');
insert l;
system.assertEquals(l.FirstName, 'FName');
test.stopTest();
}
}
Hope this helps you. Please mark this answer as best so that others facing the same issue will find this information useful. Thank you
Try the code mentioned in http://theblogreaders.com/automatically-convert-lead-to-account-contact-and-opportunity-using-apex-trigger/ Test Class: Hope this helps you. Please mark this answer as best so that others facing the same issue will find this information useful. Thank you
All Answers
Hi Swathi,
You can use below logic in your Lead trigger based on your condition.
Lead myLead;
Database.LeadConvert lc = new database.LeadConvert();
lc.setLeadId(myLead.id);
LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
lc.setConvertedStatus(convertStatus.MasterLabel);
Database.LeadConvertResult lcr = Database.convertLead(lc);
you have to bulkify it.
Try the code mentioned in http://theblogreaders.com/automatically-convert-lead-to-account-contact-and-opportunity-using-apex-trigger/ Test Class: Hope this helps you. Please mark this answer as best so that others facing the same issue will find this information useful. Thank you