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
asims2006asims2006 

Trigger Create test class

I've created the trigger below and now i'm having issues creating a test class. Can someone please help? trigger Parent_Name_Trigger on Account (after insert) { List sr = new List(); for (Account newAccount: Trigger.New) if (newAccount.Student_Name__c <> ''){ sr.add (new Student__c( Account__c = newAccount.ID, Name = newAccount.Student_Name__c)) ; } insert sr; }
Best Answer chosen by Admin (Salesforce Developers) 
magicforce9magicforce9
 @isTest
 public class TestAccountTrigger{

 	static testMethod void checkForStudentName()
 	{

 		Account acc = new Account (Name = 'Default Test Account', Student_Name__c = 'Test Student'); //Here you might need to add few more fields if they are required on account object
 		insert acc;
 		List<Student__c> students = [Select id, Name from Student__c where Account__c = :acc.id];
 		System.assert(students.size() = 1);
 		System.assertEquals(acc.Student_Name__c, students[0].Name);
 	}

 }

 

All Answers

magicforce9magicforce9
 @isTest
 public class TestAccountTrigger{

 	static testMethod void checkForStudentName()
 	{

 		Account acc = new Account (Name = 'Default Test Account', Student_Name__c = 'Test Student'); //Here you might need to add few more fields if they are required on account object
 		insert acc;
 		List<Student__c> students = [Select id, Name from Student__c where Account__c = :acc.id];
 		System.assert(students.size() = 1);
 		System.assertEquals(acc.Student_Name__c, students[0].Name);
 	}

 }

 

This was selected as the best answer
Sam MohyeeSam Mohyee

First, I suggest you edit your post and copy/paste the code text into the "insert code" popup you can access for the text editing toolbar. Makes it easier for us to read!

 

This is the code text you posted:

 

trigger Parent_Name_Trigger on Account (after insert) { 

List sr = new List(); 

for (Account newAccount: Trigger.New) 
     if (newAccount.Student_Name__c <> ''){ 
sr.add (new Student__c( Account__c = newAccount.ID, Name = newAccount.Student_Name__c)) ;
}

insert sr;
}

  

I notice your for  clause is missing its { }'s.

 

Question: is Student_Name__c a lookup field pointing to a contact record? Or is it just a text field?

 

What exactly would you like help with on your test class? Broad strokes would be to create a test class that creates an account with student_name__c populated, then make a system assertion that the newly inserted student record has that account as it's parent. 

asims2006asims2006

This worked Perfect!  I appreciate your help.  I'm new to working with Apex I'll be descriptive in my next posts. 

nitinkhunal.ax1786nitinkhunal.ax1786
@isTest
Public class ParentNameTriggerTest
{
Public static testMethod void AccountTest()
{
Account acc=new Account();
acc.Name='Example';
acc.Student_Name__c='Asim';
insert acc;

Student__c stu=new Student__c();
stu.Account__c=acc.Id;
stu.Student_Name__c=acc.Student_Name__c;
insert stu;
}
}