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
David SmithDavid Smith 

Writing test class for Trigger

Hi all,

 

With the help of users from this board I was able to write and implement triggers in my sandbox environment, the code for which is below. However, when I tried to move it to live it fell over, telling me I needed 75% code covereage. 

 

Could someone give me some insight into writing this test class? As far as I know it's about testing your interactions with records, but I'm not sure how to go about doing that.

 

 

}

trigger Contact_Update_SCIWorkflow on Event (before update, before insert)
{   
List<Contact> conForUpdate = new List<Contact>();       
for(Event e : Trigger.New)
    {

        if(e.whoid != null)
        {
            if(e.Consultative_Selling_Sci_Work_Flows__c == TRUE)
            {
                If(Trigger.IsInsert || (Trigger.IsUpdate && (Trigger.oldMap.get(e.Id).Consultative_Selling_Sci_Work_Flows__c !=Trigger.newMap.get(e.Id).Consultative_Selling_Sci_Work_Flows__c)
                                        )
                   )
                                {           
                                conForUpdate.add
                                    (
                                    new Contact
                                        (
                                        Id = e.WhoId,


                                        Consultative_Selling_Sci__c = TRUE,
                                        Consultative_Selling_SCI_Last_Pres_Date__c = e.Startdatetime


                                        )
                                    );       
                                }   
}       
 
 
 
 
if(conForUpdate != NULL && conForUpdate.size() > 0)
{update conForUpdate;}    

        }
    }

Best Answer chosen by Admin (Salesforce Developers) 
Starz26Starz26

@isTest

private class myTestClass{

 

   Static myTestMethod void myTest(){

   

     Event nEvent = New Event();

     Contact nContact = New Contact();

 

     nContact.FirstName = 'Test';

     nContact.LastName = 'Test';

     nContact.Email = 'test@test.net';

     //Put other required fields for contact here

     Insert nContact;

 

     nEvent.whoID = nContact.id;

     nEvent.Consultative_Selling_Sci_Work_Flo​ws__c = True;

     nEvent.Startdatetime = date.Today();

     //Add any other required Event fields here

 

     test.startTest();

     insert nEvent;

     test.stopTest();

 

     //If you want, query for the record just added and do system.assertEquals on the fields you want to check to ensure the right values are there.

 

  }

 

 

}

All Answers

Starz26Starz26

@isTest

private class myTestClass{

 

   Static myTestMethod void myTest(){

   

     Event nEvent = New Event();

     Contact nContact = New Contact();

 

     nContact.FirstName = 'Test';

     nContact.LastName = 'Test';

     nContact.Email = 'test@test.net';

     //Put other required fields for contact here

     Insert nContact;

 

     nEvent.whoID = nContact.id;

     nEvent.Consultative_Selling_Sci_Work_Flo​ws__c = True;

     nEvent.Startdatetime = date.Today();

     //Add any other required Event fields here

 

     test.startTest();

     insert nEvent;

     test.stopTest();

 

     //If you want, query for the record just added and do system.assertEquals on the fields you want to check to ensure the right values are there.

 

  }

 

 

}

This was selected as the best answer
David SmithDavid Smith

Thanks so much for that, it pretty much did what I wanted it to. I had to make a few changes to get it to work, so I've highlighted them below in case anyone else is using this code. Once again, thanks for your help, you're a lifesaver.

 

}

@isTest

private class myTestClass{

 

   Static myTestMethod void myTest(){

   

     Event nEvent = New Event();

     Contact nContact = New Contact();

 

     nContact.FirstName = 'Test';

     nContact.LastName = 'Test';

     nContact.Email = 'test@test.net';

     //Put other required fields for contact here

     Insert nContact;

 

     nEvent.whoID = nContact.id;

     nEvent.Consultative_Selling_Sci_Work_Flo​ws__c = True;

     nEvent.Startdatetime = system.Today();

     //Add any other required Event fields here

 

     test.startTest();

     insert nEvent;

     test.stopTest();

 

     //If you want, query for the record just added and do system.assertEquals on the fields you want to check to ensure the right values are there.

 

  }

 }

 

Starz26Starz26

Lol, sorry about the syntax issues, cross language stuff........Glad it worked for you