+ Start a Discussion
MelAdminMelAdmin 

Created trigger - stuck on creating APEX test class

I created two very simple triggers and tested them in a sandbox. I'm not a developer, but put them together using suggestions from this board.  Trigger 1:  When a new contact is added to the Alumni Outreach object through a lookup to the Contact record, a checkbox (Alumni_Outreach_Project__c) on the Contact record is checked.  Trigger 2:  Unchecks the checkbox when a contact's record is deleted from the Alumni Outreach object.  I tried to deploy them to Production using change sets and learned that I need to create an APEX test class. 

 

Would anyone be able to help me get started with creating the test class?  I'm not sure I'll be able to code this - do I need to create one class for each trigger? 

 

Any help would be appreciated.  Thanks

 

 

Trigger 1:

trigger AlumniOutreach_Check_Checkbox on Alumni_Outreach__c (after insert) {
List<Contact> updatedContacts = new List<Contact>();
Set<Id> ContactId = new Set<Id>();
for (Alumni_Outreach__c w : trigger.new)(
    ContactID.add(w.Name_of_Alum__c));
for (Contact a : [SELECT id, Alumni_Outreach_Project__c FROM Contact WHERE id IN :ContactID]){
    a.Alumni_Outreach_Project__c = true;
    updatedContacts.add(a);
    }
update updatedContacts;
}


Trigger 2:
trigger AlumniOutreach_UNCheck_Checkbox on Alumni_Outreach__c (after delete) {
List<Contact> updatedContacts = new List<Contact>();
Set<Id> ContactId = new Set<Id>();
for (Alumni_Outreach__c w : trigger.old)(
    ContactID.add(w.Name_of_Alum__c));
for (Contact a : [SELECT id, Alumni_Outreach_Project__c FROM Contact WHERE id IN :ContactID]){
    a.Alumni_Outreach_Project__c = false;
    updatedContacts.add(a);
    }
update updatedContacts;

cloudcodercloudcoder

Check out the following resource which should help:

 

http://wiki.developerforce.com/index.php/An_Introduction_to_Apex_Code_Test_Methods

MelAdminMelAdmin

Thank you.  I decided to try to create a test class for the Insert trigger.  It compiles, but Code Coverage = 40%  Can you advise on how I can get to 75%? 

 

@isTest
private class testAlumniOutreachCheckCheckboxTrigger {

        //Create Account record type "Cohort" required for validation on the Contact record
        static testMethod void myUnitTest() {
        List<RecordType> AccountRTypes = [Select id, Name, sObjectType
                                            from RecordType
                                            where Name = 'Cohort' and sObjectType = 'Account'];
                                         
        Map<String,String> AccountRecordTypes = new Map<String,String>{};
        for(RecordType rt: AccountRTypes)
        AccountRecordTypes.put(rt.Name,rt.Id);
        
        //Create Contact record type "Dreamer Alumnus" required for validation on the Contact record
        List<RecordType> ContactRTypes = [Select id, Name, sObjectType
                                            from RecordType
                                            where Name = 'Dreamer Alumnus' and sObjectType = 'Contact'];
                                         
        Map<String,String> ContactRecordTypes = new Map<String,String>{};
        for(RecordType rt: ContactRTypes)
        ContactRecordTypes.put(rt.Name,rt.Id);

       //Create a Cohort Account record to insert
        Account acc = new Account(Name='Test Org');
        acc.RecordTypeid= AccountRecordTypes.get('Cohort');
        insert acc;

        //Create a Contact record to insert
        Contact con = new Contact(Cohort__c = acc.id, Lastname = 'Doe', Firstname = 'Jane');
        con.RecordTypeid= ContactRecordTypes.get('Dreamer Alumnus');        
        insert con;

        //Create an Alumni Outreach Effort Record to insert
        Alumni_Outreach_Efforts__c aeo= new Alumni_Outreach_Efforts__c(Name='Test Effort');
        insert aeo;
        
        //Create an Alumni Outreach Record to insert
        Alumni_Outreach__c ao1= new Alumni_Outreach__c(Name_of_Alum__c= con.id, Outreach_Effort__c = aeo.id);
        //ao1.Outreach_Effort__c = 'Test Effort';
        insert ao1;

        //Test to see that the trigger fired and checked the checkbox
        Contact con2= [SELECT Alumni_Outreach_Campaign__c FROM Contact WHERE Id = :con.id];

        System.assertEquals(true, con2.Alumni_Outreach_Campaign__c);
        }
}

MelAdminMelAdmin

Can I define the Contact Record type another way?

Thanks