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
Matt Lim 7Matt Lim 7 

Create identical contacts upon account creation

I am trying to create two identical contacts upon account creation. so far i have written below :
trigger IdenticalContacts on Account (after insert) {
    for (Account acc : Trigger.new){
    
        Contact con = new Contact();
        con.LastName = 'Monkey D';
        con.FirstName = 'Luffy';
        con.email = 'goldroger@gmail.com';
        con.AccountId = acc.id;
            
     }                           
}
I also wrote test class below but getting 0% code coverage:
@isTest
public class TestIndenticalContacts {
    
    @isTest static void createAccount(){
        Account acc = new Account();
        acc.Name = 'Salesforce';
        insert acc;
    }
}
Is the problem due to 'After Insert'?
 
Best Answer chosen by Matt Lim 7
ravi soniravi soni
hi Matt,
you forgot to insert contact.
try below code.
trigger IdenticalContacts on Account (after insert) {
list<Contact> lstContact = new    list<Contact> ();
 for (Account acc : Trigger.new){
    
        Contact con = new Contact();
        con.LastName = 'Monkey D';
        con.FirstName = 'Luffy';
        con.email = 'goldroger@gmail.com';
        con.AccountId = acc.id;
            lstContact.add(con);
     }                        
if(lstContact.size() > 0){
insert lstContact ;
}   
}
Test class
@isTest
public class TestIndenticalContacts {
    
    @isTest static void createAccount(){
        Account acc = new Account();
        acc.Name = 'Salesforce';
        insert acc;
    }
}
now try below code. I believe it will help you.
Make sure your trigger is Active.
don't forget to mark it as best answer.
Thank you


 

All Answers

ankit bansalankit bansal
Hi Matt,
How are you checking the code coverage, I would suggest to use developer console to check the coverage.
If still you are not able to cover the code please check that your trigger is active.
ravi soniravi soni
hi Matt,
you forgot to insert contact.
try below code.
trigger IdenticalContacts on Account (after insert) {
list<Contact> lstContact = new    list<Contact> ();
 for (Account acc : Trigger.new){
    
        Contact con = new Contact();
        con.LastName = 'Monkey D';
        con.FirstName = 'Luffy';
        con.email = 'goldroger@gmail.com';
        con.AccountId = acc.id;
            lstContact.add(con);
     }                        
if(lstContact.size() > 0){
insert lstContact ;
}   
}
Test class
@isTest
public class TestIndenticalContacts {
    
    @isTest static void createAccount(){
        Account acc = new Account();
        acc.Name = 'Salesforce';
        insert acc;
    }
}
now try below code. I believe it will help you.
Make sure your trigger is Active.
don't forget to mark it as best answer.
Thank you


 
This was selected as the best answer
ravi soniravi soni
hi Matt,
did you try above code. try it once and let me know and don't forget to mark it as best answer.
Your one best mark give us motivation to working in this direction.
Thank you