My first suggestion is to slow down a bit and read up on some of the Trigger Best Practices to get an idea of some things you can do differently with the Trigger in the future. With that said, let's get the unit tests fixed first:
As a new developer, expect to be reading a ton of documentation:
Take your time and review all of that in detail. There are 3 main pieces to every unit test in Salesforce.
- Generate test data
- Perform test logic
- Assert logic
So, let's start with that:
@isTest
public Class TestUnitPlacementSalesTriggerClass{
public static testMethod void testAfterInsert(){
// Generate test data
// Perform test logic
// Assert logic
}
}
From here, let's start adding the test data. You already have that done, but we need to change it because you should never hard code IDs in your code. They will change when you deploy and your unit tests will fail. Also, whenever you are testing a Trigger, you need to do so with more than one record in order to test for bulk scenarios.
@isTest
public Class TestUnitPlacementSalesTriggerClass{
public static testMethod void testAfterInsert(){
Unit__c unit = new Unit__c();
insert unit;
List<Unit_Placements_Sales__c> unitPlacementSales = new List<Unit_Placements_Sales__c>();
for(Integer i = 0; i < 21; i++){
unitPlacementSales.add(new Unit_Placements_Sales__c(
Owner__c = UserInfo.getUserId(),
Unit__c = unit.Id,
Service_Month_is_Next_Month__c = true
));
}
// Perform test logic
// Assert logic
}
}
Now that we are ready to perform our insert logic, let's make sure we wrap the actual test code in Test.startTest();
and Test.stopTest();
. This will allow us to properly test our data limits.
@isTest
public Class TestUnitPlacementSalesTriggerClass{
public static testMethod void testAfterInsert(){
Unit__c unit = new Unit__c();
insert unit;
List<Unit_Placements_Sales__c> unitPlacementSales = new List<Unit_Placements_Sales__c>();
for(Integer i = 0; i < 21; i++){
unitPlacementSales.add(new Unit_Placements_Sales__c(
Owner__c = UserInfo.getUserId(),
Unit__c = unit.Id,
Service_Month_is_Next_Month__c = true
));
}
Test.startTest();
insert unitPlacementSales;
Test.stopTest();
// Assert logic
}
}
Finally, we need to assert something to verify our test worked. Each record we inserted should have generated a Services__c
record. We should check as much information as possible to verify our code worked the way we expected, but I will go simplistic here for the sake of example.
@isTest
public Class TestUnitPlacementSalesTriggerClass{
public static testMethod void testAfterInsert(){
Unit__c unit = new Unit__c();
insert unit;
List<Unit_Placements_Sales__c> unitPlacementSales = new List<Unit_Placements_Sales__c>();
for(Integer i = 0; i < 21; i++){
unitPlacementSales.add(new Unit_Placements_Sales__c(
Owner__c = UserInfo.getUserId(),
Unit__c = unit.Id,
Service_Month_is_Next_Month__c = true
));
}
Test.startTest();
insert unitPlacementSales;
Test.stopTest();
List<Services__c> services = [SELECT Id FROM Services__c WHERE Equipment_Onsite__c IN :unitPlacementSales];
System.assert(services.size > 0, 'There should be a Services__c record generated for every UnitPlacementSale');
}
}
There you go. That tests half of your Triggers functionality. You will need to write an additional test for after insert
. You will also want to consider refactoring your Trigger with the best practices I linked to above.
Note: All of the above code was written purely on SFSE. It may or may not compile. I highly suggest taking some time to rework it yourself. Please do not just copy and paste.
EDIT: As noted above, please make sure you include your test class in your deploy. If you don't, your code will never be able to be deployed to production as the unit tests need to be run on production before the code is finally committed.