+ Start a Discussion
Faizan Ali 24Faizan Ali 24 

Creating a test class for a trigger help!

I am new to Salesforce and I am struggling to write a simple test class. I haven't written one before though I have been doing a lot of trailhead on it but can't seem to wrap my head around this test class for a trigger.

I have this trigger that Autofills a lookup field called Staff whenever a value is selected from the Appointment_with__c picklist field. It only auto-fills whenever the event is saved:

trigger Autofill on Event (before insert, before update) {
    { 
        for (Event e : Trigger.new) 
        {
            
            String [] str = e.Appointment_with__c.split(' ');
            
            
            List<Contact> cont = [SELECT Id FROM Contact WHERE FirstName =:str[0] AND LastName =:str[1]];               
            
            
            e.Staff__c = cont[0].Id;
        }
    }
}

Any ideas on how to create a test class for this? 

Thank you in advance for your help
 
Faizan Ali 24Faizan Ali 24
In addition, it should only autofill whenever a value from the picklist is selected. The values in the picklist are text but they are names of certain contacts.
Maharajan CMaharajan C
HI Faizan,

Try the below class:
 
@isTest
public class AutoFillTrigger_Test {
	@isTest Static void testAutoFill(){
		
		Account acc = new Account(name = 'Test Account',phone='9345678791'); // Add if there is any other are required to create Account
		insert acc;
		
		Contact con = new Contact(accountid=acc.id, firstname='John', lastname='Doe', phone='9345678791');  // Add if there is any other are required to create Contact
		insert con;
		
		Event e = new Event();
        e.WhatId=acc.id;
		e.Appointment_with__c = 'John Doe';  // If it's restricted picklist then use the proper value here as like your org pickvsl
        e.StartDateTime=system.today();
        e.EndDateTime=system.today()+5;
		
		Test.StartTest();
			insert e;
		Test.StopTest();
	
	}
}

Thanks,
Maharajan.C
Faizan Ali 24Faizan Ali 24
Thank you so much for this!

I ran into a few errors though...

System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, Autofill: execution of BeforeInsert

caused by: System.ListException: List index out of bounds: 0

Trigger.Autofill: line 12, column 1: []
Faizan Ali 24Faizan Ali 24
@isTest
public class AutoFillTest {
    @isTest Static void testAutoFill(){
        
        Contact con = new Contact(FirstName='John', LastName='Doe', Email='faizanali_786@hotmail.co.uk');  // Add if there is any other are required to create Contact
        insert con;
        
        Event e = new Event();
        e.Appointment_with__c = 'Barbara Carter';  // If it's restricted picklist then use the proper value here as like your org pickvsl
        e.StartDateTime=system.today();
        e.EndDateTime=system.today()+5;
        
        Test.StartTest();
            insert e;
        Test.StopTest();
    
    }
}
Maharajan CMaharajan C
Change the contact first name and last name in test class:
Contact con = new Contact(FirstName='Barbara', LastName='Carter', Email='faizanali_786@hotmail.co.uk');  // Add if there is any other are required to create Contact
 insert con;

Thanks,
Maharajan.C
Faizan Ali 24Faizan Ali 24
Hi Maharajan thank you so much for your help so far!

Unfortunately, I am still getting some errors as stated below:

System.DmlException: Insert failed. First exception on row 0; first error: FIELD_FILTER_VALIDATION_EXCEPTION, Value does not exist or does not match filter criteria.: [Staff__c]


I also added to my test class:

@isTest
public class AutoFillTest {
    @isTest Static void testAutoFill(){
                
        Contact con = new Contact(FirstName='Test', LastName='Trigger', Email='faizanali_786@hotmail.co.uk');
        insert con;
        
        /*
        List<Event> queryPicklist = [SELECT Appointment_with__c from Event];
        for(Event ap : queryPicklist)
        {
            ap.Appointment_with__c = con.Name;
        }
        */

        
        Event e = new Event();
        e.Appointment_with__c = 'Test Trigger'; 
        e.StartDateTime=system.today();
        e.EndDateTime=system.today()+5;
        e.Subject = 'Diary Booking';
        e.Event_Type__c = 'Advisor Appointment';
        e.Event_Sub_Type__c = 'Telephone Appointment';
        
        
        Test.StartTest();
            insert e;
        Test.StopTest();
    
    }
}
Faizan Ali 24Faizan Ali 24
I should also mention to you that, in order to use the value from the picklist field, the contact's first name and last name should be added to the picklist. I don't think I did it correctly, that's why I have commented the section above.