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
Lee_CampbellLee_Campbell 

Test class for Trigger that creates an associated record

Hi folks,

 

I have a number of Triggers that, upon creation of a custom record, automatically create an associated custom record. The Triggers themselves fire fine, but I can only get about 70% code coverage in my test classes. If anyone could help me with the form one of the test classes should take, I'm sure I'll be able to write the rest for the other Triggers. Here's an example of the kind of Trigger I mean:

 

trigger AutoProject on Procurement__c (before insert) {
    map< id, Milestone1_Project__c > proj = new map< id, Milestone1_Project__c >( );
    
    for( Procurement__c proc : trigger.new ) {
       proj.put( proc.id,
            new Milestone1_Project__c(
                  Name = proc.Name,
                  Submission_Deadline__c = proc.Submission_Deadline__c)
            );
    
    insert proj.values();
    proc.Bid_Planning_Timeline__c = proj.get(proc.id).id;    
    }        
    
}

 Any help very greatly appreciated.

 

Many thanks,

Lee

Best Answer chosen by Admin (Salesforce Developers) 
Laxman RaoLaxman Rao

Try this:

 

@isTest
private with sharing class TestInsert {
static testMethod void procurementBefore() {
list<Procurement__c> procurements = new list<Procurement__c>();

//populate all the required fields for Procurement__c
Procurement__c p1 = new Procurement__c(Name = 'Test1', Submission_Deadline__c = system.today());
procurements.add(p1);

 

//populate some more records also

insert procurements;
}
}

 

Refer this :

http://wiki.developerforce.com/page/An_Introduction_to_Apex_Code_Test_Methods

All Answers

Laxman RaoLaxman Rao

Try this:

 

@isTest
private with sharing class TestInsert {
static testMethod void procurementBefore() {
list<Procurement__c> procurements = new list<Procurement__c>();

//populate all the required fields for Procurement__c
Procurement__c p1 = new Procurement__c(Name = 'Test1', Submission_Deadline__c = system.today());
procurements.add(p1);

 

//populate some more records also

insert procurements;
}
}

 

Refer this :

http://wiki.developerforce.com/page/An_Introduction_to_Apex_Code_Test_Methods

This was selected as the best answer
Lee_CampbellLee_Campbell

Some of my Triggers trigger other Triggers that fire on the same object, so I just need to include code that covers that off as well, but yes. The simple test class you suggested covers the simpler Triggers fine though. Thanks.