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
Andrew Hoban 6Andrew Hoban 6 

Test Class For Trigger

Hi all,

I am having difficulty creating a test class for a trigger I have created. The trigger updates a date/time field every fime a checkbox field is modified. I am unsure how i would create the tesst trigger, any help would be great.

My trigger is:
trigger ControlRoomCheckedTimeTrigger on Match_Day_Check_List__c (before insert, before update) {
    if(Trigger.isInsert){
     for(match_day_check_list__c b: trigger.new){
      b.Steward_Phone_Check_Complete_Last_Mod__c = DateTime.Now();
      b.Radio_Check_Complete_Last_Modified__c = DateTime.Now();
      b.CCTV_Cameras_Checked_Last_Modified__c = DateTime.Now();
    }
  }
  if(Trigger.isUpdate){
        for(match_day_check_list__c b: trigger.new){
            match_day_check_list__c oldb = Trigger.oldMap.get(b.ID);
            
            if(oldb.Steward_Phone_Check_Complete__c != b.Steward_Phone_Check_Complete__c){
                b.Steward_Phone_Check_Complete_Last_Mod__c = DateTime.Now();
            }
            
            if(oldb.Radio_Check_Complete__c != b.Radio_Check_Complete__c){
                b.Radio_Check_Complete_Last_Modified__c = DateTime.Now();
            }
            
            if(oldb.CCTV_Cameras_Checked__c != b.CCTV_Cameras_Checked__c){
                b.CCTV_Cameras_Checked_Last_Modified__c = DateTime.Now();
            }
            
            if(oldb.Turnstile_Checks_Control_Room__c != b.Turnstile_Checks_Control_Room__c){
                b.Turnstile_Checks_Last_Modified_CRoom__c = DateTime.Now();
            }
            
    }
    
}
}

Many thanks.
Best Answer chosen by Andrew Hoban 6
rohitsfdcrohitsfdc
Andrew,
Try the code below. 

I am assuming the name of those objects are fixture__c and match_plan__c. If they are not, please update it in the code at line no 5 and 6
@istest(seealldata=true)
public class ControlRoomTest{
public static testmethod void testmethod1(){
match_day_check_list__c md = new match_day_check_list__c ();
md.Fixture__c = [select id from Fixture__c limit 1].id;
md.Match_Plan__c = [select id from Match_plan__c limit 1].id;
insert md;

md.Steward_Phone_Check_Complete__c=false;
md.Radio_Check_Complete__c = false;
md.CCTV_Cameras_Checked__c = false;
md.Turnstile_Checks_Control_Room__c = false;
update md;
}
}





All Answers

rohitsfdcrohitsfdc
Hello Andrew, 

Try the test class below. let me know in case of any issues.

If that solves your problem, please mark this as the best answer.

@istest

public class rohittest{
public static testmethod void testmethod1(){
match_day_check_list__c md = new match_day_check_list__c ();
insert md;

md.Steward_Phone_Check_Complete__c=false;
md.Radio_Check_Complete__c  = false;
md.CCTV_Cameras_Checked__c  = false;
md.Turnstile_Checks_Control_Room__c  = false;

update md;
}
}




Andrew Hoban 6Andrew Hoban 6
Thanks for your reposne. The test has unfortunately failed. I am getting the message:


System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Fixture__c, Match_Plan__c]: [Fixture__c, Match_Plan__c]
Class.ControlRoomTest.testmethod1: line 5, column

I have now added these into the test class however i am still getting the same error? 

@istest
public class ControlRoomTest{
public static testmethod void testmethod1(){
match_day_check_list__c md = new match_day_check_list__c ();
insert md;
md.Fixture__c = 'Test Fixture';
md.Match_Plan__c = 'Test Match';
md.Steward_Phone_Check_Complete__c=false;
md.Radio_Check_Complete__c = false;
md.CCTV_Cameras_Checked__c = false;
md.Turnstile_Checks_Control_Room__c = false;
update md;
}
}

Thanks
rohitsfdcrohitsfdc
Andrew, you just missed to put them in the right place

@istest
public class ControlRoomTest{
public static testmethod void testmethod1(){
match_day_check_list__c md = new match_day_check_list__c ();
md.Fixture__c = 'Test Fixture';
md.Match_Plan__c = 'Test Match';
insert md;

md.Steward_Phone_Check_Complete__c=false;
md.Radio_Check_Complete__c = false;
md.CCTV_Cameras_Checked__c = false;
md.Turnstile_Checks_Control_Room__c = false;
update md;
}
}

If that solves your problem, please like this post and mark it as the best answer.

Thanks

Andrew Hoban 6Andrew Hoban 6
Thanks for your help. The fixture & match plan are lookup fields, I am getting the error System.StringException: Invalid id: Test Fixture

I think it is something simple I am not doing correctly. Thanks
rohitsfdcrohitsfdc
Andrew,
Try the code below. 

I am assuming the name of those objects are fixture__c and match_plan__c. If they are not, please update it in the code at line no 5 and 6
@istest(seealldata=true)
public class ControlRoomTest{
public static testmethod void testmethod1(){
match_day_check_list__c md = new match_day_check_list__c ();
md.Fixture__c = [select id from Fixture__c limit 1].id;
md.Match_Plan__c = [select id from Match_plan__c limit 1].id;
insert md;

md.Steward_Phone_Check_Complete__c=false;
md.Radio_Check_Complete__c = false;
md.CCTV_Cameras_Checked__c = false;
md.Turnstile_Checks_Control_Room__c = false;
update md;
}
}





This was selected as the best answer
Andrew Hoban 6Andrew Hoban 6
Thanks mate that worked.
rohitsfdcrohitsfdc
Glad i could help.

Please like the post, and mark it as the best answer if that solves your query.

Thanks,
Rohit