trigger convertLead on Lead (before update) {
for(Lead myLead:Trigger.new){
if(myLead.status == ‘qualified’){
list<Account> acc = [select id,name from account where name =: myLead.Company limit 1];
if(acc.size() == 0){
Id accId = create.createNewAccount(myLead.Company);
create.createContact(accId,myLead.LastName);
create.createOpportunity(accId,myLead.Company);
}
else{
create.createContact(acc[0].Id,myLead.LastName);
create.createOpportunity(acc[0].Id,myLead.Company);
}
}
}
}
Helper class-
public class create {
public static id createNewAccount(String accName){
Account acc = new Account(name = accName);
insert acc;
return acc.Id;
}
public static void createContact(id accID,String lName){
Contact con = new Contact(lastname = lName,accountId=accID);
insert con;
}
public static void createOpportunity(id accID,String company)
{
Opportunity opp = new Opportunity(name = company+’-‘,accountId=accID,StageName=’prospecting’,closedate=system.today());
insert opp;
}
}