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
sri sfdcsri sfdc 

Trigger on lead object

when ever rating is hot on lead object , I want to create Accout and Contact for that lead. As a new bie, I don't know how to get that lead id to create account ,contact .

 kindly help me in this.

Best Answer chosen by sri sfdc
Amit Chaudhary 8Amit Chaudhary 8
1.  led.Rating != OldLead.Rating  can u explain this condition
Amit:- I added this line so that evey time in update case it will not create new Account and Contact. Code will create record only once you will change Rating on Lead object. Code will work even for update just try to change rating.
IF you want to create every time one contact and account remove above check

2. if(listAccountToCreate.size() > 0 ) ,  if(listContactToCreate.size() > 0 )​ why we need to mention these two if conditions
Amit:- IF Event will not change or Rating will not hot in that case above both list will blank and we dnt need to create new record

Same code will work for insert and update as well.
In case of update you need to change Rating from hot to some other and again back to hot, OR you can change code like below if you want to create record every time
 
trigger LeadTrigger on Lead( after update ,After insert )
{

	List<Account> listAccountToCreate = new List<Account>();
	List<Contact> listContactToCreate = new List<Contact>();
	
	for(Lead led : trigger.new )
	{
		if(led.Rating =='Hot' )
		{
			Account acc= new Account();
			if(led.Company != null)
			{
				acc.Name = led.Company;
			}
			else
			{
				acc.Name = 'From Lead';
			}	
			listAccountToCreate.add(acc);
			
			Contact cont = new Contact();
			cont.LastName = led.LastName;
			cont.FirstName = led.FirstName;
			listContactToCreate.add(cont);
		}
			
	}
	if(listAccountToCreate.size() > 0 )
	{
		insert listAccountToCreate;
	}

	if(listContactToCreate.size() > 0 )
	{
		insert listContactToCreate;
	}
	
}

Let us know if this will help you
 

All Answers

Amit Chaudhary 8Amit Chaudhary 8
Please check below post to learn about Trigger
1) http://amitsalesforce.blogspot.in/2015/06/trigger-best-practices-sample-trigger.html
 
trigger LeadTrigger on Lead( after update )
{

	List<Account> listAccountToCreate = new List<Account>();
	List<Contact> listContactToCreate = new List<Contact>();
	
	for(Lead led : trigger.new )
	{
		Lead OldLead = Trigger.oldMap.get(led.id);
		if( led.Rating != OldLead.Rating && led.Rating =='Hot' )
		{
			Account acc= new Account();
			if(led.Company != null)
			{
				acc.Name = led.Company;
			}
			else
			{
				acc.Name = 'From Lead';
			}	
			
			listAccountToCreate.add(acc);
			
			Contact cont = new Contact();
			cont.LastName = led.LastName;
			cont.FirstName = led.FirstName;
			listContactToCreate.add(cont);
		}
			
	}
	if(listAccountToCreate.size() > 0 )
	{
		insert listAccountToCreate;
	}

	if(listContactToCreate.size() > 0 )
	{
		insert listContactToCreate;
	}
	
}

Let us know if this will help you
 
sri sfdcsri sfdc

Hi Amit, 

Thanks for help. Kindly explain few points.

1.  led.Rating != OldLead.Rating  can u explain this condition 
2. if(listAccountToCreate.size() > 0 ) ,  if(listContactToCreate.size() > 0 )​ why we need to mention these two if conditions.

And doing some modifications in above trigger I wrote it for before insert event it works . when I am trying to do for both events before insert and after update its not working. So can u suggest me for both insert and update .

 

Amit Chaudhary 8Amit Chaudhary 8
1.  led.Rating != OldLead.Rating  can u explain this condition
Amit:- I added this line so that evey time in update case it will not create new Account and Contact. Code will create record only once you will change Rating on Lead object. Code will work even for update just try to change rating.
IF you want to create every time one contact and account remove above check

2. if(listAccountToCreate.size() > 0 ) ,  if(listContactToCreate.size() > 0 )​ why we need to mention these two if conditions
Amit:- IF Event will not change or Rating will not hot in that case above both list will blank and we dnt need to create new record

Same code will work for insert and update as well.
In case of update you need to change Rating from hot to some other and again back to hot, OR you can change code like below if you want to create record every time
 
trigger LeadTrigger on Lead( after update ,After insert )
{

	List<Account> listAccountToCreate = new List<Account>();
	List<Contact> listContactToCreate = new List<Contact>();
	
	for(Lead led : trigger.new )
	{
		if(led.Rating =='Hot' )
		{
			Account acc= new Account();
			if(led.Company != null)
			{
				acc.Name = led.Company;
			}
			else
			{
				acc.Name = 'From Lead';
			}	
			listAccountToCreate.add(acc);
			
			Contact cont = new Contact();
			cont.LastName = led.LastName;
			cont.FirstName = led.FirstName;
			listContactToCreate.add(cont);
		}
			
	}
	if(listAccountToCreate.size() > 0 )
	{
		insert listAccountToCreate;
	}

	if(listContactToCreate.size() > 0 )
	{
		insert listContactToCreate;
	}
	
}

Let us know if this will help you
 
This was selected as the best answer
sri sfdcsri sfdc
Thank you so much Amit