+ Start a Discussion
MR_BennettMR_Bennett 

Apex Trigger Test Class Help

Hi everyone,  I am not a SFDC newbie but am new to writing triggers, etc.  Normally I would figure this out myself but I am in a bit of a time crunch :smileyindifferent:  So i created a trigger to count the number of contacts related to an account object, the code works without a problem.  However I now need to implement it and need to test the code to pass coverage requirements.  

 

I  need to create a class that creates a new contact.  Although my code for this class isn't throwing any errors, it is not creating the new contact record.  I realize this is small potatoes compared to some of the other requests here, but I'd appreciate the help.

 

@isTest
public class myClassTest {
static testMethod void myTest() {


Contact testContact = new Contact(recordtypeid='01280000000BcQiAAK',firstname='Bob',lastname='Dole',title='Teacher',AccountId='001V0000005D6cY');


insert testContact

;

}}

Best Answer chosen by Admin (Salesforce Developers) 
Ritesh AswaneyRitesh Aswaney

Create an Account in your test class, then associate a Contact with it, then check count.

 

So 

Account acc = new Account(Name = 'Test Account');

insert acc;

 

Contact con = new Contact (LastName = 'Test Contact', AccountId = acc.Id);

insert con;

 

acc = [Select Id, Name, NoOfContacts__c from Account where Id = :acc.Id];

 

System.assertEquals(1, acc.NoOfContacts__c);

 

Needless to say, replace NoOfContacts__c with your own aggregate field.

All Answers

Ritesh AswaneyRitesh Aswaney

Create an Account in your test class, then associate a Contact with it, then check count.

 

So 

Account acc = new Account(Name = 'Test Account');

insert acc;

 

Contact con = new Contact (LastName = 'Test Contact', AccountId = acc.Id);

insert con;

 

acc = [Select Id, Name, NoOfContacts__c from Account where Id = :acc.Id];

 

System.assertEquals(1, acc.NoOfContacts__c);

 

Needless to say, replace NoOfContacts__c with your own aggregate field.

This was selected as the best answer
Chamil MadusankaChamil Madusanka

You have to aware on best practices of writing test classes.

 Refer following article

 

http://gokubi.com/archives/testing-best-practices

 

If a reply to a post answers your question or resolves your problem, please mark it as the solution to the post so that others may benefit.

Navatar_DbSupNavatar_DbSup

Hi,

 

Try this code

 

Account acc = new Account(Name = 'Test Account1');
insert acc;
Contact con1 = new Contact (LastName = 'Test Contact1', AccountId = acc.Id);
insert con1;
Contact con2= new Contact (LastName = 'Test Contact2', AccountId = acc.Id);
insert con2;


If you need more help on then please post your trigger code here.

MR_BennettMR_Bennett

Thanks.  Worked like a charm, that's what i was thinking i had to do.. Just didn't know all the syntax.  Very much appreciated.  Trigger coverage tests 100%.  Question.  Is there any way to have the trigger fire for all existing records?  right now I have it on before insert and before update.  I thouth that would have done the trick but i guess not.

Ritesh AswaneyRitesh Aswaney

A trigger executes only on an insert / update / ... on a record.

If you intend to process all existing records, you will need to perform a phantom update of all records, which will fire the trigger and perform the necessary recalculation.

MR_BennettMR_Bennett

Thanks.  I had to check the data again anyway so I just did a DL export and updated the records which fired the trigger.