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
NavneethNavneeth 

Apex trigger to convert a lead into account

i need a trigger code which will convert the lead into account automatically. 

Saurabh DuaSaurabh Dua

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 ?? 

NavneethNavneeth

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

Saurabh DuaSaurabh Dua

Sure just wait for a couple of minutes ill give u the code. 

NavneethNavneeth

thanks in advance. pls post it a lil early..as i m a lil b

Saurabh DuaSaurabh Dua

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;
}
}


}

kranjankranjan

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. 

 

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;
}

 

 

Regards

sarvesh001sarvesh001

HI 

kranjankranjan
Hi Sarvesh,

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