You need to sign in to do that
Don't have an account?
Faizan 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
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
Try the below class:
Thanks,
Maharajan.C
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: []
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();
}
}
Thanks,
Maharajan.C
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();
}
}