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
mw6mw6 

Test class for the trigger to create a Person Account

I have written a trigger to clone/copy a record from Custom object "Student__c" (when a new student is created or modified)  to Account (Person Account) and it works, I am not sure on how to write a test class to increase the code coverage, please help.  The trigger is as below:

trigger CreatePersonAccountfromStudent on Student__c (after insert,after update)
{
    List<account> acc=new List<account>();
    for(student__c stud:Trigger.New)
    {
        acc.add(new account(
               LastName=Stud.name__c,
               recordtypeid='012O00000009RtPIAU',
               Autocreated__pc=True,
               PersonBirthdate=Stud.DOB__c,
               Current_Acedamic_Year__pc=Stud.Current_Acedamic_Year__c,
               Date_of_Collection__pc=Stud.Date_of_Collection__c,
               Discount_Percentage__pc=Stud.Discount_Percentage__c,
               Discount_Policy__pc=Stud.Discount_Policy__c,
               Discount_Remarks__pc=Stud.Discount_Remarks__c,
               Discount_Type__pc=Stud.Discount_Type__c,
               English__pc=Stud.English__c,
               Exclude_Registration__pc=Stud.Exclude_Registration__c,
               Father_Email__pc=Stud.Father_Email__c,
               Father_Name__pc=Stud.Father_Name__c,
               Father_NRIC__pc=Stud.Father_NRIC__c,
               Father_Office_Phone__pc=Stud.Father_Office_Phone__c,
               Father_Primary_Contact__pc=Stud.Father_Primary_Contact__c,
               Father_Remark__pc=Stud.Father_Remark__c,
               Father_Staff__pc=Stud.Father_Staff__c,
               Gender__pc=Stud.Gender__c,
               GEP__pc=Stud.GEP__c,
               Guardian_Email__pc=Stud.Guardian_Email__c,
               Guardian_Mobile__pc=Stud.Guardian_Mobile__c,
               Guardian_Name__pc=Stud.Guardian_Name__c,
               Guardian_NRIC__pc=Stud.Guardian_NRIC__c,
               Guardian_Office_Phone__pc=Stud.Guardian_Office_Phone__c,
               Guardian_Primary_Contact__pc=Stud.Guardian_Primary_Contact__c,
               Guardian_Relationship__pc=Stud.Guardian_Relationship__c,
               Guardian_Remarks__pc=Stud.Guardian_Remark__c,
               Guardian_Staff__pc=Stud.Guardian_Staff__c,
               PersonHomePhone=Stud.Home_No__c,
               Is_Mind_Stretcher_Staff_Mother__pc=Stud.Is_Mind_Stretcher_Staff_Mother__c,
               Level__pc=Stud.Level__c,
               Level_Promoted_DateTime__pc=Stud.Level_Promoted_DateTime__c,
               PersonMailingStreet=Stud.Unit_Number__c+' '+Stud.Block__c+' '+Stud.Street__c+' '+Stud.Postal_Code__c,
               Maths__pc=Stud.Maths__c,
               Mobile_No_Father__pc=Stud.Mobile_No_Father__c,
               Mother_Email__pc=Stud.Mother_Email__c,
               Mother_Name__pc=Stud.Mother_Name__c,
               Mother_No_Mobile__pc=Stud.Mobile_No_Mother__c,
               Mother_NRIC__pc=Stud.Mother_NRIC__c,
               Mother_Office_Phone__pc=Stud.Mother_Office_Phone__c,
               Mother_Primary_Contact__pc=Stud.Mother_Primary_Contact__c,
               Mother_Remark__pc=Stud.Mother_Remark__c,
               Mother_Staff__pc=Stud.Mother_Staff__c,
               MS_Start_Kit_Bag__pc=Stud.MS_Start_Kit_Bag__c,
               Nationality__pc=Stud.Nationality__c,
               NRIC__pc=Stud.NRIC__c,
               NTUC_10_Discount__pc=Stud.NTUC_10_Discount__c,
               NTUC_Card_Expiry__pc=Stud.NTUC_Card_Expiry__c,
               NTUC_Remark__pc=Stud.NTUC_Remark__c,
               Others__pc=Stud.Others__c,
               Person_Contact__pc=Stud.Person_Contact__c,
               Primary_Contact_Number__pc=Stud.Contact_number__c,
               Primary_Email__pc=Stud.Primary_Email__c,
               Primary_Name__pc=Stud.Primary_Name__c,
               Primary_Remark__pc=Stud.Primary_Remark__c,
               Promotion__pc=Stud.Promotion__c,
               Reading_Oceans_Email__pc=Stud.Reading_Oceans_Email__c,
               Reading_Oceans_End_Date__pc=Stud.Reading_Oceans_End_Date__c,
               Reading_Oceans_Start_Date__pc=Stud.Reading_Oceans_Start_Date__c,
               Reading_Oceans_Subscrip__pc=Stud.Reading_Oceans_Subscrip__c,
               Registraation_Centre__pc=Stud.Registration_Centre__c,
               Related_Centres__pc=Stud.Related_Centres__c,
               Remarks__pc=Stud.Remarks__c,
               RO_Date_Send_User__pc=Stud.RO_Date_Send_User__c,
               RO_Type__pc=Stud.RO_Type__c,
               Safra_Card_Expiry__pc=Stud.Safra_Card_Expiry__c,
               Safra_Card_Holding__pc=Stud.Safra_Card_Holding__c,
               Safra_Remark__pc=Stud.Safra_Remark__c,
               Salesforce_Id_from_Student_Object__pc=Stud.ID,
               School__pc=Stud.School__c,
               Science_Percentage__pc=Stud.Science__c,
               Secondary_Stream__pc=Stud.Secondary_Stream__c,
               Student_Id_from_Student_Prospects__pc=Stud.name,
               Student_Old_Reference_ID__pc=Stud.Student_Old_Reference_ID__c,
               Student_Status__pc=Stud.Student_Status__c,
               Student_Type__pc='Student',
               Tick_to_activate_inter_branch_discount__pc=Stud.Tick_to_activate_inter_branch_discount__c
               ));
    }
    insert acc;
}
Best Answer chosen by mw6
Apoorv Saxena 4Apoorv Saxena 4
Hi,

To increase code coverage for your trigger, you only need to insert a Student__c record in your test class with all the fields that are required when creating a Student__c record and all those fields that you are using in your trigger.

Your testclass should look something like this:
 
@isTest
public class CreatePersonAccountfromstudentTest{
	static testMethod void myTest(){
		Student__c stud = new Student__c();
		stud.name__c = 'Test student';
		stud.DOB__c = system.today();
		stud.Current_Acedamic_Year__c = '2017';
		/* 
		Similarly set all others fields for student record 
		that you are using in your trigger.
		*/
		
		insert stud;
		
	}
}

Hope this helps!

Please mark this question as Solved if it answers your question so that others can view it as a proper solution.

Thanks,
Apoorv

All Answers

sfdcMonkey.comsfdcMonkey.com
hi mw6 
its very easy
you can only create data for Student__c here is an example i use Expense__c instedOf Student__c
trigger CreatePersonAccountfromStudent on Expense__c (after insert,after update) {
    List<account> acc=new List<account>();
    for(Expense__c stud:Trigger.New)
    {
        acc.add(new account(
               Name=Stud.Name,
               AnnualRevenue = Stud.Amount__c
               //..... more field goes here
               ));
    }
    insert acc;
}
@isTest 
public class TriggerTestClass {
    static testMethod void insertNewExpenses() {
       
       Expense__c exp = new Expense__c(); // you use Student__c here
         exp.Name = 'test';             // give test data for all Student__c fields 
         exp.Amount__c  = 500;  
       
          insert exp;
    }
}
this test class cover 100% for this trigger code.
so you can create test class as above test class
if you have any problem with it you can ask here :)
Thanks
let me inform if it helps you




 
Apoorv Saxena 4Apoorv Saxena 4
Hi,

To increase code coverage for your trigger, you only need to insert a Student__c record in your test class with all the fields that are required when creating a Student__c record and all those fields that you are using in your trigger.

Your testclass should look something like this:
 
@isTest
public class CreatePersonAccountfromstudentTest{
	static testMethod void myTest(){
		Student__c stud = new Student__c();
		stud.name__c = 'Test student';
		stud.DOB__c = system.today();
		stud.Current_Acedamic_Year__c = '2017';
		/* 
		Similarly set all others fields for student record 
		that you are using in your trigger.
		*/
		
		insert stud;
		
	}
}

Hope this helps!

Please mark this question as Solved if it answers your question so that others can view it as a proper solution.

Thanks,
Apoorv
This was selected as the best answer
sfdcMonkey.comsfdcMonkey.com
hi mw6
date d1 = date.newInstance(2008,03,02);
Stud.DOB__c = d1;

try this onces :)
mw6mw6
Thank you Piysh, it worked, and now the code coverage is 100%
mw6mw6
I have the below trigger to create a person account, it is running when a new record is created on Student__c objet, now I need some advice on how to modify the trigger to run when a student record is edited.

trigger CreatePersonAccountfromStudent on Student__c (after insert)
{
    List<account> acc=new List<account>();
    Map<Id, String> m = new Map<Id, String>();
    for(student__c stud:Trigger.New)
    {
        acc.add(new account(LastName=Stud.name__c,
                            recordtypeid='012O00000009RtPIAU',
                            Autocreated__pc=True,
                            PersonBirthdate=Stud.DOB__c,
                            Current_Acedamic_Year__pc=Stud.Current_Acedamic_Year__c,
                            Date_of_Collection__pc=Stud.Date_of_Collection__c,
                            Discount_Percentage__pc=Stud.Discount_Percentage__c,
                            Discount_Policy__pc=Stud.Discount_Policy__c,
                            Discount_Remarks__pc=Stud.Discount_Remarks__c,
                            Discount_Type__pc=Stud.Discount_Type__c,
                            English__pc=Stud.English__c,
                            Exclude_Registration__pc=Stud.Exclude_Registration__c,
                            Father_Email__pc=Stud.Father_Email__c,
                            Father_Name__pc=Stud.Father_Name__c,
                            Father_NRIC__pc=Stud.Father_NRIC__c,
                            Father_Office_Phone__pc=Stud.Father_Office_Phone__c,
                            Father_Primary_Contact__pc=Stud.Father_Primary_Contact__c,
                            Father_Remark__pc=Stud.Father_Remark__c,
                            Father_Staff__pc=Stud.Father_Staff__c,
                            Gender__pc=Stud.Gender__c,
                            GEP__pc=Stud.GEP__c,
                            Guardian_Email__pc=Stud.Guardian_Email__c,
                            Guardian_Mobile__pc=Stud.Guardian_Mobile__c,
                            Guardian_Name__pc=Stud.Guardian_Name__c,
                            Guardian_NRIC__pc=Stud.Guardian_NRIC__c,
                            Guardian_Office_Phone__pc=Stud.Guardian_Office_Phone__c,
                            Guardian_Primary_Contact__pc=Stud.Guardian_Primary_Contact__c,
                            Guardian_Relationship__pc=Stud.Guardian_Relationship__c,
                            Guardian_Remarks__pc=Stud.Guardian_Remark__c,
                            Guardian_Staff__pc=Stud.Guardian_Staff__c,
                            PersonHomePhone=Stud.Home_No__c,
                            Is_Mind_Stretcher_Staff_Mother__pc=Stud.Is_Mind_Stretcher_Staff_Mother__c,
                            Level__pc=Stud.Level__c,
                            Level_Promoted_DateTime__pc=Stud.Level_Promoted_DateTime__c,
                            PersonMailingStreet=Stud.Unit_Number__c+' '+Stud.Block__c+' '+Stud.Street__c+' '+Stud.Postal_Code__c,
                            Maths__pc=Stud.Maths__c,
                            Mobile_No_Father__pc=Stud.Mobile_No_Father__c,
                            Mother_Email__pc=Stud.Mother_Email__c,
                            Mother_Name__pc=Stud.Mother_Name__c,
                            Mother_No_Mobile__pc=Stud.Mobile_No_Mother__c,
                            Mother_NRIC__pc=Stud.Mother_NRIC__c,
                            Mother_Office_Phone__pc=Stud.Mother_Office_Phone__c,
                            Mother_Primary_Contact__pc=Stud.Mother_Primary_Contact__c,
                            Mother_Remark__pc=Stud.Mother_Remark__c,
                            Mother_Staff__pc=Stud.Mother_Staff__c,
                            MS_Start_Kit_Bag__pc=Stud.MS_Start_Kit_Bag__c,
                            Nationality__pc=Stud.Nationality__c,
                            NRIC__pc=Stud.NRIC__c,
                            NTUC_10_Discount__pc=Stud.NTUC_10_Discount__c,
                            NTUC_Card_Expiry__pc=Stud.NTUC_Card_Expiry__c,
                            NTUC_Remark__pc=Stud.NTUC_Remark__c,
                            Others__pc=Stud.Others__c,
                            Person_Contact__pc=Stud.Person_Contact__c,
                            Primary_Contact_Number__pc=Stud.Contact_number__c,
                            Primary_Email__pc=Stud.Primary_Email__c,
                            Primary_Name__pc=Stud.Primary_Name__c,
                            Primary_Remark__pc=Stud.Primary_Remark__c,
                            Promotion__pc=Stud.Promotion__c,
                            Reading_Oceans_Email__pc=Stud.Reading_Oceans_Email__c,
                            Reading_Oceans_End_Date__pc=Stud.Reading_Oceans_End_Date__c,
                            Reading_Oceans_Start_Date__pc=Stud.Reading_Oceans_Start_Date__c,
                            Reading_Oceans_Subscrip__pc=Stud.Reading_Oceans_Subscrip__c,
                            Registration_Centre__pc=Stud.Registration_Centre__c,
                            Related_Centres__pc=Stud.Related_Centres__c,
                            Remarks__pc=Stud.Remarks__c,
                            RO_Date_Send_User__pc=Stud.RO_Date_Send_User__c,
                            RO_Type__pc=Stud.RO_Type__c,
                            Safra_Card_Expiry__pc=Stud.Safra_Card_Expiry__c,
                            Safra_Card_Holding__pc=Stud.Safra_Card_Holding__c,
                            Safra_Remark__pc=Stud.Safra_Remark__c,
                            Salesforce_Id_from_Student_Object__pc=Stud.ID,
                            School__pc=Stud.School__c,
                            Science_Percentage__pc=Stud.Science__c,
                            Secondary_Stream__pc=Stud.Secondary_Stream__c,
                            Student_Id_from_Student_Prospects__pc=Stud.name,
                            Student_Old_Reference_ID__pc=Stud.Student_Old_Reference_ID__c,
                            Student_Status__pc=Stud.Student_Status__c,
                            Student_Type__pc='Student',
                            Tick_to_activate_inter_branch_discount__pc=Stud.Tick_to_activate_inter_branch_discount__c
                            )); /*add the name data of student__c table into account table */

    }
    insert acc;
   
}
sfdcMonkey.comsfdcMonkey.com
hi mw6
try this

trigger CreatePersonAccountfromStudent on Student__c (after insert ,after update){
 if(trigger.isAfter && trigger.isInsert){
    List<account> acc=new List<account>();
    Map<Id, String> m = new Map<Id, String>();
    for(student__c stud:Trigger.New)
    {
        acc.add(new account(LastName=Stud.name__c,
                            recordtypeid='012O00000009RtPIAU',
                            Autocreated__pc=True,
                            PersonBirthdate=Stud.DOB__c,
                            Current_Acedamic_Year__pc=Stud.Current_Acedamic_Year__c,
                            Date_of_Collection__pc=Stud.Date_of_Collection__c,
                            Discount_Percentage__pc=Stud.Discount_Percentage__c,
                            Discount_Policy__pc=Stud.Discount_Policy__c,
                            Discount_Remarks__pc=Stud.Discount_Remarks__c,
                            Discount_Type__pc=Stud.Discount_Type__c,
                            English__pc=Stud.English__c,
                            Exclude_Registration__pc=Stud.Exclude_Registration__c,
                            Father_Email__pc=Stud.Father_Email__c,
                            Father_Name__pc=Stud.Father_Name__c,
                            Father_NRIC__pc=Stud.Father_NRIC__c,
                            Father_Office_Phone__pc=Stud.Father_Office_Phone__c,
                            Father_Primary_Contact__pc=Stud.Father_Primary_Contact__c,
                            Father_Remark__pc=Stud.Father_Remark__c,
                            Father_Staff__pc=Stud.Father_Staff__c,
                            Gender__pc=Stud.Gender__c,
                            GEP__pc=Stud.GEP__c,
                            Guardian_Email__pc=Stud.Guardian_Email__c,
                            Guardian_Mobile__pc=Stud.Guardian_Mobile__c,
                            Guardian_Name__pc=Stud.Guardian_Name__c,
                            Guardian_NRIC__pc=Stud.Guardian_NRIC__c,
                            Guardian_Office_Phone__pc=Stud.Guardian_Office_Phone__c,
                            Guardian_Primary_Contact__pc=Stud.Guardian_Primary_Contact__c,
                            Guardian_Relationship__pc=Stud.Guardian_Relationship__c,
                            Guardian_Remarks__pc=Stud.Guardian_Remark__c,
                            Guardian_Staff__pc=Stud.Guardian_Staff__c,
                            PersonHomePhone=Stud.Home_No__c,
                            Is_Mind_Stretcher_Staff_Mother__pc=Stud.Is_Mind_Stretcher_Staff_Mother__c,
                            Level__pc=Stud.Level__c,
                            Level_Promoted_DateTime__pc=Stud.Level_Promoted_DateTime__c,
                            PersonMailingStreet=Stud.Unit_Number__c+' '+Stud.Block__c+' '+Stud.Street__c+' '+Stud.Postal_Code__c,
                            Maths__pc=Stud.Maths__c,
                            Mobile_No_Father__pc=Stud.Mobile_No_Father__c,
                            Mother_Email__pc=Stud.Mother_Email__c,
                            Mother_Name__pc=Stud.Mother_Name__c,
                            Mother_No_Mobile__pc=Stud.Mobile_No_Mother__c,
                            Mother_NRIC__pc=Stud.Mother_NRIC__c,
                            Mother_Office_Phone__pc=Stud.Mother_Office_Phone__c,
                            Mother_Primary_Contact__pc=Stud.Mother_Primary_Contact__c,
                            Mother_Remark__pc=Stud.Mother_Remark__c,
                            Mother_Staff__pc=Stud.Mother_Staff__c,
                            MS_Start_Kit_Bag__pc=Stud.MS_Start_Kit_Bag__c,
                            Nationality__pc=Stud.Nationality__c,
                            NRIC__pc=Stud.NRIC__c,
                            NTUC_10_Discount__pc=Stud.NTUC_10_Discount__c,
                            NTUC_Card_Expiry__pc=Stud.NTUC_Card_Expiry__c,
                            NTUC_Remark__pc=Stud.NTUC_Remark__c,
                            Others__pc=Stud.Others__c,
                            Person_Contact__pc=Stud.Person_Contact__c,
                            Primary_Contact_Number__pc=Stud.Contact_number__c,
                            Primary_Email__pc=Stud.Primary_Email__c,
                            Primary_Name__pc=Stud.Primary_Name__c,
                            Primary_Remark__pc=Stud.Primary_Remark__c,
                            Promotion__pc=Stud.Promotion__c,
                            Reading_Oceans_Email__pc=Stud.Reading_Oceans_Email__c,
                            Reading_Oceans_End_Date__pc=Stud.Reading_Oceans_End_Date__c,
                            Reading_Oceans_Start_Date__pc=Stud.Reading_Oceans_Start_Date__c,
                            Reading_Oceans_Subscrip__pc=Stud.Reading_Oceans_Subscrip__c,
                            Registration_Centre__pc=Stud.Registration_Centre__c,
                            Related_Centres__pc=Stud.Related_Centres__c,
                            Remarks__pc=Stud.Remarks__c,
                            RO_Date_Send_User__pc=Stud.RO_Date_Send_User__c,
                            RO_Type__pc=Stud.RO_Type__c,
                            Safra_Card_Expiry__pc=Stud.Safra_Card_Expiry__c,
                            Safra_Card_Holding__pc=Stud.Safra_Card_Holding__c,
                            Safra_Remark__pc=Stud.Safra_Remark__c,
                            Salesforce_Id_from_Student_Object__pc=Stud.ID,
                            School__pc=Stud.School__c,
                            Science_Percentage__pc=Stud.Science__c,
                            Secondary_Stream__pc=Stud.Secondary_Stream__c,
                            Student_Id_from_Student_Prospects__pc=Stud.name,
                            Student_Old_Reference_ID__pc=Stud.Student_Old_Reference_ID__c,
                            Student_Status__pc=Stud.Student_Status__c,
                            Student_Type__pc='Student',
                            Tick_to_activate_inter_branch_discount__pc=Stud.Tick_to_activate_inter_branch_discount__c
                            )); /*add the name data of student__c table into account table */

    }
    insert acc;
    }
    
  if(trigger.isAfter && trigger.isUpdate){
 
      // code for after edit student record goes here
     
   }  
 
   
}

Thanks
i hop it helps you
let me inform if it helps you and mark it best answer so it make proper solution for others :)