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
Gateway Bikes Group4Gateway Bikes Group4 

/* Write a trigger that automatically creates an Account whenever a Lead is created. The Account be named after the last name of the Lead*/

/* Write a trigger that automatically creates an Account whenever a Lead is created. The Account be named after the last name of the Lead*/


trigger Accountcreate on Lead(after insert){
 
   for(Lead ld:Trigger.new){

         Account acc= new Account();
 
         acc.Name=ld.Lastname;
     
           insert acc;

}

}


@isTest
private class AccountcreateTest {
    

    @isTest static void Accountcreate() {
        // Implement test code
    
           Lead ld = new Lead();
             
      ld.LastName ='Creed';
                
      ld.FirstName='Apollo';
            
      ld.Company  ='Rocky';
           
         Insert ld;

}
}
Best Answer chosen by Gateway Bikes Group4
Raj VakatiRaj Vakati
You code is correct .. just bulkify as shown above 

All Answers

Raj VakatiRaj Vakati
Code 
 
trigger Accountcreate on Lead(after insert){
 List<Account> accs= new List<Account>();
   for(Lead ld:Trigger.new){
         Account acc= new Account();
         acc.Name=ld.Lastname;
		 accs.add(acc) ; 
   }
   if(accs.size()>0){
	   
	   insert accs;
   }

}

Test CLass
 
@isTest
private class AccountcreateTest {
    

    @isTest static void Accountcreate() {
		List<Lead> les = new List<Lead>();
		for(Integer i = 0 ;i<10;i++){
           Lead ld = new Lead();
             
      ld.LastName ='Creed';
                
      ld.FirstName='Apollo';
         ld.Status = 'Open'   
      ld.Company  ='Rocky';
	  les.add(ld);
		}    
         Insert les;

}
}

 
Raj VakatiRaj Vakati
You code is correct .. just bulkify as shown above 
This was selected as the best answer
Eshwar Ajay Sai SiddaniEshwar Ajay Sai Siddani

Yeah It works fine. 

But why it also works fine when we use in Before Insert.

trigger CreateAccounts on Lead (before insert) {
For(lead l:Trigger.new){
Account a;
a=new Account(Name=l.LastName);
insert a;
}
}

It is working when I execute the above code.