Can u explain the senario ie when the lead should be converted to account and contact ?? .. do u want the same situation which happens while converting the lead ??
Using web to lead i will capture details and store it in standard lead object of salesforce. now once i save this , the lead should automatically get converted in to a account record. and it should also check for duplication. i.e if a record with same name exists in account then it shold not create another account.
Could u pls provide me the code to convert a lead to account. pls help
The approach provided by Saurabh is good. However it will fail at times as it is not bulkified and is not using the best design practices of Apex. I ahve reframed the same and following code should work for you.
trigger LeadCreateAccount on Lead (Before Insert) {
Public List<Account> lstAccts = new List<Account>();
Public List<Account> lstNewAccts = new List<Account>();
Public set<String> setLeadNames = new set<String>();
Public set<String> setExistingAcctNames = new set<String>();
public String name = '';
// Create the set for al the names of the leads
for(Lead l: trigger.new)
{
setLeadNames.add(l.firstname + ' ' + l.lastname);
}
// fetch the matching accounts if any exists
lstAccts = [select id,name from account where name IN : setLeadNames];
for(Account a: lstAccts)
{
setExistingAcctNames.add(a.name);
}
// Iterate through all leads to create acocunt if it does nto exists
for(Lead l: trigger.new)
{
name = l.firstname + ' ' + l.lastname;
if (! setExistingAcctNames.contains(name))
{
Account acct = new account();
acct.name = name;
// MAP ALL THE FIELDS U WANT TO MAP HERE
lstNewAccts.add(acct);
// Add this to avoid any duplication in current set fo leads
setExistingAcctNames.add(name);
}
}
if (lstNewAccts.size() > 0)
insert lstNewAccts;
}
Can u explain the senario ie when the lead should be converted to account and contact ?? .. do u want the same situation which happens while converting the lead ??
the scenario is like this.
Using web to lead i will capture details and store it in standard lead object of salesforce. now once i save this , the lead should automatically get converted in to a account record. and it should also check for duplication. i.e if a record with same name exists in account then it shold not create another account.
Could u pls provide me the code to convert a lead to account. pls help
Sure just wait for a couple of minutes ill give u the code.
thanks in advance. pls post it a lil early..as i m a lil b
trigger LeadCreateAccountBI on Lead (Before Insert) {
Public List<Account> aa;
public integer counter=0;
public String name;
for(Lead l: trigger.new)
{
name=l.firstname+' '+l.lastname;
aa=[select id,name from account];
for(Account aaa:aa)
{
if(aaa.name==name)
counter=1;
}
if(counter==0)
{
account acc= new account();
acc.name=name;
// MAP ALL THE FIELDS U WANT TO MAP HERE
insert acc;
}
}
}
Hi Navneeth,
The approach provided by Saurabh is good. However it will fail at times as it is not bulkified and is not using the best design practices of Apex. I ahve reframed the same and following code should work for you.
Regards
HI kranjan,
What happend when trying to add dplicape account name ,
is it show error , or dplicate record not added to list
Please sggest
Thanks,
sarvesh1111.
The intent is to add Account if it is a new account without adding the duplicate. So in case of a duplicate it is not required to do anything.
Regards