When ever new Account record is created with Account Type as ‘prospect ‘ then create a new contact for this account with following data
Contact lastname as Account Name
Contact Phone as Account Phone
Trigger
trigger createContact on Account( after insert ){
List<Contact> contacts=new List<Contact>();
for(Account a: Trigger.New){
if(a.type=='Prospect'){
Contact c=new Contact();
c.LastName=a.Name;
c.Phone=a.Phone;
c.AccountId=a.id;
contacts.add(c);
}
}
insert contacts;
}
Test Class
@isTest
private class AccountTest{
@isTest
static void testme(){
Account a=new Account();
a.name='Test';
a.Type='Prospect';
try{
insert a;
}catch(Exception e){
}
Integer count=[select count() from Contact];
if(a.Type=='Prospect'){
System.assertEquals(count,1);
}else{
System.assertEquals(count,0);
}
}
}