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
MelAdminMelAdmin 

Created my first trigger - stuck on creating the APEX class

I created two very simple triggers and tested them in a sandbox. I am 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 on the Contact record, Alumni_Outreach_Project__c, 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 class. 

 

Would anyone be able to help me get started with creating the 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;
}

dotnet developedotnet develope

Hi Friend,

 

For deployment of triggers in to production.

You need to write APEX Test Class and the test coverage should be 75% min..

 

I appreciate you started working on APEX Scripting and suggest you to work on Apex test class

Please find the link below

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

 

Thanks & Regards,

:smileyvery-happy:

 

 

MelAdminMelAdmin

Thank you. I read through the material, but I'm afraid that goes beyond my abilities, so I'll try to figure out another way of doing this.  I really wish they would fully implement cross-object workflow.

new2forcenew2force

@isTest

 

private class testTrigger

{

           static testmethod void testTriiger()

            {

  Alumni_Outreach__c    newAlumni= new Alumni_Outreach__c(// keep all the required values)                        

 

insert newAlumni;

 

    there is a procedure to delete a record search in online

 

delete newAlumni;                          

 

            }

 

}

 

 

 

u r done. test classes for trigger is so easy. u have to create the situation which fires the trigger. in our case trigger fired when Allumni is inserted. all the other lines will be automatically tested by trigger.

MelAdminMelAdmin

Thank you.  I decided to try to create a test class for the Insert trigger.  It compiles, but Code Coverage = 40%  Does anyone know 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);
        }
}