You need to sign in to do that
Don't have an account?

Test Class help needed please
Hello, thnak you very much for the help in advance if you are able to help with the following test class. I am new to Apex and have created a trigger which I am posting below which is working fine. The test class I created is covering roughly 54 percent and I need it higher of course. I am posting the test class below as well with the lines marked that are not being covered. If you can help me get the code coverage up I would greatly appreciate it. Thank you again!
Trigger Code -
List<String> CaseZipList = new List<String>();
For(Case Cases : Trigger.new) {
IF(string.isBlank(Cases.New_Job_Number__c) && Cases.AVISPL_Service_Maintenance_Contract__c != null && !string.isBlank(Cases.New_Company_Database__c) && !string.isBlank(Cases.Address_Zip_Code__c)) {
String zipCode = cases.Address_Zip_Code__c;
//We need to sanitize the incoming zip code. Specifically, the Zip_Code__c object stores 5-digit zip codes only, so a longer zip code (e.g. 17801-7952) needs to be trimmed to match (e.g. 17801).
zipCode = zipCode.left(5);
trigger AVI_SPL_TM_Zip_To_Warehouse on Case (after insert, before update) {
CaseZipList.add(zipCode);
}
}
//If there is at least one Case which meets our criteria...
if(CaseZipList.size() > 0) {
//...then query the appropriate objects and perform the rest of the trigger's functionality.
ID ExternalZipCodeRT = Schema.sObjectType.Zip_Code__c.getRecordTypeInfosByName().get('External Zip Code').getRecordTypeId();
List<Zip_Code__c> zipList = [SELECT ID, Name, Location__Longitude__s,Location__Latitude__s, Warehouse_Code_Selection__c, Company_Database__c
FROM Zip_Code__c
WHERE Name = : CaseZipList
AND RecordTypeId = :ExternalZipCodeRT];
ID InternalZipCodeRT = Schema.sObjectType.Zip_Code__c.getRecordTypeInfosByName().get('Internal Sales Office').getRecordTypeId();
List<Zip_Code__c> SalesZipList = [SELECT ID, Name, Location__Longitude__s,Location__Latitude__s, Warehouse_Code_Selection__c, Company_Database__c
FROM Zip_Code__c
WHERE RecordTypeId = :InternalZipCodeRT];
List<Location> SalesOfficeLocations = new List<Location>();
Map<Location,String> locationToWarehouseCode = new Map<Location,String>();
Map<String,String> warehouseCodeToCompanyDatabase = new Map<String, String>();
For(Zip_Code__c SZ : SalesZipList) {
NOT COVERED Location Loc = Location.newInstance(SZ.Location__Latitude__s,SZ.Location__Longitude__s);
NOT COVERED SalesOfficeLocations.add(Loc);
NOT COVERED locationToWarehouseCode.put(Loc,SZ.Warehouse_Code_Selection__c);
NOT COVERED warehouseCodeToCompanyDatabase.put(SZ.Warehouse_Code_Selection__c, SZ.Company_Database__c);
}
//Loop through each inserted or updated Case.
for(Case Cases : Trigger.new) {
//For each Case that meets our criteria...
IF(string.isBlank(Cases.New_Job_Number__c) && Cases.AVISPL_Service_Maintenance_Contract__c != null && !string.isBlank(Cases.New_Company_Database__c) && !string.isBlank(Cases.Address_Zip_Code__c)) {
//...then loop through each of the External Zip Codes we queried.
For(Zip_Code__c CL : ZipList) {
NOT COVERED String zipCode = Cases.Address_Zip_Code__c;
//We need to sanitize the incoming zip code. Specifically, the Zip_Code__c object stores 5-digit zip codes only, so a longer zip code (e.g. 17801-7952) needs to be trimmed to match (e.g. 17801).
NOT COVERED zipCode = zipCode.left(5);
//When we find the matching External Zip Code...
NOT COVERED if(CL.name == zipCode) {
//...then grab its location so we can compare against the Sales Office Locations.
NOT COVERED Location Loc = Location.newInstance(CL.Location__Latitude__s,CL.Location__Longitude__s);
NOT COVERED Double minDistance = null;
NOT COVERED String closestWarehouseCode = null;
//Loop through all the Sales Office Locations.
NOT COVERED For(Location SOL : SalesOfficeLocations){
//If the Sales Office Location is for the same Company Database value as the Case we're currently examining...
NOT COVERED if(warehouseCodeToCompanyDatabase.get(locationToWarehouseCode.get(SOL)) == cases.New_Company_Database__c) {
//...then determine the distance between them.
NOT COVERED Double distance = Location.getDistance(Loc,SOL,'mi');
//If this is the closest distance we've calculated so far...
NOT COVERED If(minDistance == null || distance < minDistance){
//...then save that distance as our new closest and assign the Warehouse Code as the closest Warehouse.
NOT COVERED minDistance = distance;
NOT COVERED closestWarehouseCode = locationToWarehouseCode.get(SOL);
}//end of If(minDistance == null || distance < minDistance)
}//end of if(warehouseCodeToCompanyDatabase.get(locationToWarehouseCode.get(SOL)) == cases.New_Company_Database__c)
}//end of For(Location SOL : SalesOfficeLocations)
//Having done all of those comparisons, we can now assign the closest Warehouse Code to the Case.
NOT COVERED Cases.AVI_SPL_Rep_Whse_Office_Code_2011__c = closestWarehouseCode;
}//end of if(CL.name == zipCode)
}//end of For(Zip_Code__c CL : ZipList)
}//end of IF(string.isBlank(Cases.New_Job_Number__c) && Cases.AVISPL_Service_Maintenance_Contract__c != null && !string.isBlank(Cases.New_Company_Database__c) && !string.isBlank(Cases.Address_Zip_Code__c))
}//end of for(Case Cases : Trigger.new)
}//end of if(CaseZipList.size() > 0)
//call of existing trigger "SalesExtract" from Case Object. Need this code to fire after Zip actions.
salespersonextraction.salesextract(Trigger.new);
}
Test Class -
@isTest
public class AVI_SPL_TM_Zip_To_Warehouse_Test{
public static Case testCase;
public static AVISPL_Service_Maintenance_Contract__c testSmc;
public static Account testAcc;
@testSetup
private static void myTestData(){
Account a = new Account();
a.Name = 'Test Warehouse Account';
a.CurrencyIsoCode = 'USD';
a.RecordTypeId = '01240000000DhQMAA0';
a.Named_Account__c = 'No';
a.Exclusive_Account__c = 'No';
a.Enterprise_Account__c = 'No';
insert a;
AVI_SPL_TM_Zip_To_Warehouse_Test.testAcc = a;
System.debug('The new Account ID is ' + a.Id);
Contact con = new Contact();
con.FirstName = 'Test Shawn';
con.LastName = 'Contact';
con.Email = 'djsuite015@gmail.com';
con.CurrencyIsoCode = 'USD';
con.AccountId ='0014000000iLrfy';
con.RecordTypeId ='012330000001HMuAAM';
insert con;
System.debug('The new Contact ID is ' + con.Id);
AVISPL_Service_Level_Term__c sla = new AVISPL_Service_Level_Term__c();
sla.Name = 'Test';
sla.Active__c = True;
insert sla;
system.debug('The new SLA Term ID is ' + sla.Id);
AVISPL_Service_Maintenance_Contract__c s = new AVISPL_Service_Maintenance_Contract__c();
s.Name = 'Warehouse Test SMC';
s.Active_Contract__c = True;
s.CurrencyIsoCode = 'USD';
s.Salesperson__c = con.Id;
s.BKR_Customer__c = '12345';
s.Company_Database__c = 'AVI';
s.Contract_Start_Date__c = Date.newInstance(2016, 11, 1);
s.Contract_End_Date__c = Date.newInstance(2017, 11, 1);
s.SLA_Terms__c = sla.Id;
s.Sale_Type__c = 'Service Only - Net New';
// s.Preventative_Maintenance_Visits__c = 'One';
s.RecordTypeId = '012330000001HMpAAM';
insert s;
AVI_SPL_TM_Zip_To_Warehouse_Test.testSmc = s;
system.debug('The new SMC Id is ' + s.Id);
Case c = new Case();
c.RecordTypeId = '01240000000INaqAAG';
c.Status = 'New / Open';
c.Priority = 'P4';
c.Department__c = 'Help Desk';
c.CurrencyIsoCode = 'USD';
c.AccountId = a.Id;
c.AVISPL_Service_Maintenance_Contract__c = s.Id;
c.Sub_Account_Room_Information__c = 'TBD';
c.Type = 'Contract';
c.Origin = 'Phone';
c.Subject = 'Test';
c.Description = 'Test';
c.Svc_Category_Client_Reported_Issue__c = 'A/V';
c.New_Job_Number__c = '';
c.Address_Zip_Code__c = '17801-7952';
c.New_Company_Database__c = s.Company_Database__c;
insert c;
AVI_SPL_TM_Zip_To_Warehouse_Test.testCase = c;
system.debug('The testCase ID is ' + testCase.Id);
system.debug('The new Case Id is ' + c.Id);
}
private static testMethod void tm1(){
AVI_SPL_TM_Zip_To_Warehouse_Test.myTestData();
AVI_SPL_TM_Zip_To_Warehouse_Test.testCase = [SELECT Id, New_Company_Database__c, Address_Zip_Code__c, New_Job_Number__c,
AVI_SPL_Rep_Whse_Office_Code_2011__c FROM Case
WHERE Id = : AVI_SPL_TM_Zip_To_Warehouse_Test.testCase.Id];
If(AVI_SPL_TM_Zip_To_Warehouse_Test.testCase.AVI_SPL_Rep_Whse_Office_Code_2011__c != null){
system.assert(AVI_SPL_TM_Zip_To_Warehouse_Test.testCase.AVI_SPL_Rep_Whse_Office_Code_2011__c == 'PA_Philadelphia_240S_240600_SPL');
system.assert(AVI_SPL_TM_Zip_To_Warehouse_Test.testCase.Address_Zip_Code__c == '17801');
system.assert(AVI_SPL_TM_Zip_To_Warehouse_Test.testCase.New_Company_Database__c == 'SPL');
system.assert(AVI_SPL_TM_Zip_To_Warehouse_Test.testCase.Address_Zip_Code__c != '17801-7952');
}
}
}
Trigger Code -
List<String> CaseZipList = new List<String>();
For(Case Cases : Trigger.new) {
IF(string.isBlank(Cases.New_Job_Number__c) && Cases.AVISPL_Service_Maintenance_Contract__c != null && !string.isBlank(Cases.New_Company_Database__c) && !string.isBlank(Cases.Address_Zip_Code__c)) {
String zipCode = cases.Address_Zip_Code__c;
//We need to sanitize the incoming zip code. Specifically, the Zip_Code__c object stores 5-digit zip codes only, so a longer zip code (e.g. 17801-7952) needs to be trimmed to match (e.g. 17801).
zipCode = zipCode.left(5);
trigger AVI_SPL_TM_Zip_To_Warehouse on Case (after insert, before update) {
CaseZipList.add(zipCode);
}
}
//If there is at least one Case which meets our criteria...
if(CaseZipList.size() > 0) {
//...then query the appropriate objects and perform the rest of the trigger's functionality.
ID ExternalZipCodeRT = Schema.sObjectType.Zip_Code__c.getRecordTypeInfosByName().get('External Zip Code').getRecordTypeId();
List<Zip_Code__c> zipList = [SELECT ID, Name, Location__Longitude__s,Location__Latitude__s, Warehouse_Code_Selection__c, Company_Database__c
FROM Zip_Code__c
WHERE Name = : CaseZipList
AND RecordTypeId = :ExternalZipCodeRT];
ID InternalZipCodeRT = Schema.sObjectType.Zip_Code__c.getRecordTypeInfosByName().get('Internal Sales Office').getRecordTypeId();
List<Zip_Code__c> SalesZipList = [SELECT ID, Name, Location__Longitude__s,Location__Latitude__s, Warehouse_Code_Selection__c, Company_Database__c
FROM Zip_Code__c
WHERE RecordTypeId = :InternalZipCodeRT];
List<Location> SalesOfficeLocations = new List<Location>();
Map<Location,String> locationToWarehouseCode = new Map<Location,String>();
Map<String,String> warehouseCodeToCompanyDatabase = new Map<String, String>();
For(Zip_Code__c SZ : SalesZipList) {
NOT COVERED Location Loc = Location.newInstance(SZ.Location__Latitude__s,SZ.Location__Longitude__s);
NOT COVERED SalesOfficeLocations.add(Loc);
NOT COVERED locationToWarehouseCode.put(Loc,SZ.Warehouse_Code_Selection__c);
NOT COVERED warehouseCodeToCompanyDatabase.put(SZ.Warehouse_Code_Selection__c, SZ.Company_Database__c);
}
//Loop through each inserted or updated Case.
for(Case Cases : Trigger.new) {
//For each Case that meets our criteria...
IF(string.isBlank(Cases.New_Job_Number__c) && Cases.AVISPL_Service_Maintenance_Contract__c != null && !string.isBlank(Cases.New_Company_Database__c) && !string.isBlank(Cases.Address_Zip_Code__c)) {
//...then loop through each of the External Zip Codes we queried.
For(Zip_Code__c CL : ZipList) {
NOT COVERED String zipCode = Cases.Address_Zip_Code__c;
//We need to sanitize the incoming zip code. Specifically, the Zip_Code__c object stores 5-digit zip codes only, so a longer zip code (e.g. 17801-7952) needs to be trimmed to match (e.g. 17801).
NOT COVERED zipCode = zipCode.left(5);
//When we find the matching External Zip Code...
NOT COVERED if(CL.name == zipCode) {
//...then grab its location so we can compare against the Sales Office Locations.
NOT COVERED Location Loc = Location.newInstance(CL.Location__Latitude__s,CL.Location__Longitude__s);
NOT COVERED Double minDistance = null;
NOT COVERED String closestWarehouseCode = null;
//Loop through all the Sales Office Locations.
NOT COVERED For(Location SOL : SalesOfficeLocations){
//If the Sales Office Location is for the same Company Database value as the Case we're currently examining...
NOT COVERED if(warehouseCodeToCompanyDatabase.get(locationToWarehouseCode.get(SOL)) == cases.New_Company_Database__c) {
//...then determine the distance between them.
NOT COVERED Double distance = Location.getDistance(Loc,SOL,'mi');
//If this is the closest distance we've calculated so far...
NOT COVERED If(minDistance == null || distance < minDistance){
//...then save that distance as our new closest and assign the Warehouse Code as the closest Warehouse.
NOT COVERED minDistance = distance;
NOT COVERED closestWarehouseCode = locationToWarehouseCode.get(SOL);
}//end of If(minDistance == null || distance < minDistance)
}//end of if(warehouseCodeToCompanyDatabase.get(locationToWarehouseCode.get(SOL)) == cases.New_Company_Database__c)
}//end of For(Location SOL : SalesOfficeLocations)
//Having done all of those comparisons, we can now assign the closest Warehouse Code to the Case.
NOT COVERED Cases.AVI_SPL_Rep_Whse_Office_Code_2011__c = closestWarehouseCode;
}//end of if(CL.name == zipCode)
}//end of For(Zip_Code__c CL : ZipList)
}//end of IF(string.isBlank(Cases.New_Job_Number__c) && Cases.AVISPL_Service_Maintenance_Contract__c != null && !string.isBlank(Cases.New_Company_Database__c) && !string.isBlank(Cases.Address_Zip_Code__c))
}//end of for(Case Cases : Trigger.new)
}//end of if(CaseZipList.size() > 0)
//call of existing trigger "SalesExtract" from Case Object. Need this code to fire after Zip actions.
salespersonextraction.salesextract(Trigger.new);
}
Test Class -
@isTest
public class AVI_SPL_TM_Zip_To_Warehouse_Test{
public static Case testCase;
public static AVISPL_Service_Maintenance_Contract__c testSmc;
public static Account testAcc;
@testSetup
private static void myTestData(){
Account a = new Account();
a.Name = 'Test Warehouse Account';
a.CurrencyIsoCode = 'USD';
a.RecordTypeId = '01240000000DhQMAA0';
a.Named_Account__c = 'No';
a.Exclusive_Account__c = 'No';
a.Enterprise_Account__c = 'No';
insert a;
AVI_SPL_TM_Zip_To_Warehouse_Test.testAcc = a;
System.debug('The new Account ID is ' + a.Id);
Contact con = new Contact();
con.FirstName = 'Test Shawn';
con.LastName = 'Contact';
con.Email = 'djsuite015@gmail.com';
con.CurrencyIsoCode = 'USD';
con.AccountId ='0014000000iLrfy';
con.RecordTypeId ='012330000001HMuAAM';
insert con;
System.debug('The new Contact ID is ' + con.Id);
AVISPL_Service_Level_Term__c sla = new AVISPL_Service_Level_Term__c();
sla.Name = 'Test';
sla.Active__c = True;
insert sla;
system.debug('The new SLA Term ID is ' + sla.Id);
AVISPL_Service_Maintenance_Contract__c s = new AVISPL_Service_Maintenance_Contract__c();
s.Name = 'Warehouse Test SMC';
s.Active_Contract__c = True;
s.CurrencyIsoCode = 'USD';
s.Salesperson__c = con.Id;
s.BKR_Customer__c = '12345';
s.Company_Database__c = 'AVI';
s.Contract_Start_Date__c = Date.newInstance(2016, 11, 1);
s.Contract_End_Date__c = Date.newInstance(2017, 11, 1);
s.SLA_Terms__c = sla.Id;
s.Sale_Type__c = 'Service Only - Net New';
// s.Preventative_Maintenance_Visits__c = 'One';
s.RecordTypeId = '012330000001HMpAAM';
insert s;
AVI_SPL_TM_Zip_To_Warehouse_Test.testSmc = s;
system.debug('The new SMC Id is ' + s.Id);
Case c = new Case();
c.RecordTypeId = '01240000000INaqAAG';
c.Status = 'New / Open';
c.Priority = 'P4';
c.Department__c = 'Help Desk';
c.CurrencyIsoCode = 'USD';
c.AccountId = a.Id;
c.AVISPL_Service_Maintenance_Contract__c = s.Id;
c.Sub_Account_Room_Information__c = 'TBD';
c.Type = 'Contract';
c.Origin = 'Phone';
c.Subject = 'Test';
c.Description = 'Test';
c.Svc_Category_Client_Reported_Issue__c = 'A/V';
c.New_Job_Number__c = '';
c.Address_Zip_Code__c = '17801-7952';
c.New_Company_Database__c = s.Company_Database__c;
insert c;
AVI_SPL_TM_Zip_To_Warehouse_Test.testCase = c;
system.debug('The testCase ID is ' + testCase.Id);
system.debug('The new Case Id is ' + c.Id);
}
private static testMethod void tm1(){
AVI_SPL_TM_Zip_To_Warehouse_Test.myTestData();
AVI_SPL_TM_Zip_To_Warehouse_Test.testCase = [SELECT Id, New_Company_Database__c, Address_Zip_Code__c, New_Job_Number__c,
AVI_SPL_Rep_Whse_Office_Code_2011__c FROM Case
WHERE Id = : AVI_SPL_TM_Zip_To_Warehouse_Test.testCase.Id];
If(AVI_SPL_TM_Zip_To_Warehouse_Test.testCase.AVI_SPL_Rep_Whse_Office_Code_2011__c != null){
system.assert(AVI_SPL_TM_Zip_To_Warehouse_Test.testCase.AVI_SPL_Rep_Whse_Office_Code_2011__c == 'PA_Philadelphia_240S_240600_SPL');
system.assert(AVI_SPL_TM_Zip_To_Warehouse_Test.testCase.Address_Zip_Code__c == '17801');
system.assert(AVI_SPL_TM_Zip_To_Warehouse_Test.testCase.New_Company_Database__c == 'SPL');
system.assert(AVI_SPL_TM_Zip_To_Warehouse_Test.testCase.Address_Zip_Code__c != '17801-7952');
}
}
}
Can you repost using the code formatting (click the button with the brackets icon, should say "add code sample" when hovering over it) and insert your code there, it will be eaiser for us to read. It looks like you've successfully made a test case for one of the scenerios, I'd recommend doing the same in a seperate testmethod to cater to the other cases.
Thanks,
Balayesu
try like this,
Thanks,
Balayesu
you have to initialize Name field of Zip_Code__c. to do that you have to Go to Zip_Code__c Object field's list and view Name field's description and find is there any formula involved in it. and initialize those values in myTestData() so that the Name field is not a blank value.
Thanks,
Balayesu