Test Data Factory for Field Service Lightning in Salesforce
Here is a simple and concise Test Data Factory for Field Service Lightning. This factory can help you achieve test coverage of over 75%.
@isTest public class TestDataFactory { public static Contact createAccountAndContact(){ Account acc = new Account(); acc.name = 'Test Account'; insert acc; Contact testContact = new Contact( FirstName = 'Test', LastName = 'Contact123', Email = 'testcontact123' + '@example.com', AccountId = acc.Id ); insert testContact; return testContact; } public static WorkType createWorkType(){ WorkType wt = new WorkType(); wt.Name = 'Test WT'; wt.EstimatedDuration = 2.0; wt.DurationType = 'Hours'; wt.ShouldAutoCreateSvcAppt = true; insert wt; return wt; } public static MaintenancePlan createMaintenancePlan(String accoutnId, String contactId, String workTypeId){ MaintenancePlan mp = new MaintenancePlan( MaintenancePlanTitle = 'Test Title', AccountId = accoutnId, StartDate = Date.Today(), NextSuggestedMaintenanceDate = Date.today().addDays(1), GenerationTimeframeType = 'Days', GenerationTimeframe = 10, ContactId = contactId, GenerationHorizon = 7, Reporting_Contact_1__c = contactId, WorkTypeId = workTypeId, WorkOrderGenerationMethod = 'WorkOrderPerAsset' ); insert mp; return mp; } public static MaintenanceWorkRule createMaintenanceWorkRule(String mpId, String worktypeId){ MaintenanceWorkRule mwr = new MaintenanceWorkRule( Title = 'MWR Test Title', RecurrencePattern = 'FREQ=WEEKLY;BYDAY=TH,MO;', NextSuggestedMaintenanceDate = Date.today().addDays(1), SortOrder = 1, WorkTypeId = worktypeId, ParentMaintenancePlanId = mpId ); insert mwr; return mwr; } public static WorkOrder createWorkOrder(String accountId, String worktypeId, String mpId){ WorkOrder wo = new WorkOrder(); wo.subject ='New Work Order Subject' + datetime.now(); wo.description = 'New Work Order Description' + datetime.now(); wo.accountId = accountId; wo.workTypeId = worktypeId; wo.MaintenancePlanId = mpId; wo.SuggestedMaintenanceDate = Date.today().addDays(1); insert wo; return wo; } }

No comments