You need to sign in to do that
Don't have an account?
Ellsa James
Can anyone advise how to write a unit test to get coverage for the below code?
Trigger copyunitdetail on Unit__c(before insert,after insert, after update)
{
List<Services__c> sub=new List<Services__c>();
for(Unit__c u : Trigger.new)
{
if(u.Service_Date__c == 'THIS MONTH')
{
Services__c s=new Services__c();
s.Name=u.Name;
s.Covered_By_Manuf_Warranty__c=u.Covered_By_Manuf_Warranty__c;
s.Date_for_Call_About_Servces__c=u.Date_for_Call_About_Services__c;
s.Dell_Tag_Number__c=u.Dell_Tag_Number__c;
s.Dongle_Number__c=u.Dongle_Number__c;
s.Equipment_Type__c=u.Equipment_Type__c;
s.ID__c=u.ID__c;
s.Installed_Date_First__c=u.Installed_Date__c;
s.Item_Onsite__c=u.Item_Onsite__c;
s.Machine_Specific_Notes__c=u.Machine_Specific_Notes__c;
s.Manufacturers_Warranty_End_Date__c=u.Manufacturers_Warranty_End_Date__c;
s.NX_Number__c=u.NX_Number__c;
s.Onsite_with_Customer__c=u.On_Site_With_Customer__c;
s.Product__c=u.Product__c;
s.Purchase_Date__c=u.Purchased_Date__c;
s.Serial_Number__c=u.Serial_Number__c;
s.Service_Date__c=u.Service_Date__c;
s.Service_Date_Month__c=u.Service_Date_Month__c;
s.Service_Engineer__c=u.Service_Engineer__c;
s.Service_Month__c=u.Service_Month__c;
s.Service_Month_Text__c=u.Service_Mnth__c;
s.Shipped_to_a_Customer__c=u.Shipped_to_a_Customer__c;
s.Time_Remaining_on_Manuf_Warranty__c=u.Time_Remaining_on_Manuf_Warranty__c;
s.Vendor__c=u.Vendor__c;
s.XD_Number__c=u.XD_Number__c;
sub.add(s);
}
if(sub.size()>0)
insert sub;
}
}
{
List<Services__c> sub=new List<Services__c>();
for(Unit__c u : Trigger.new)
{
if(u.Service_Date__c == 'THIS MONTH')
{
Services__c s=new Services__c();
s.Name=u.Name;
s.Covered_By_Manuf_Warranty__c=u.Covered_By_Manuf_Warranty__c;
s.Date_for_Call_About_Servces__c=u.Date_for_Call_About_Services__c;
s.Dell_Tag_Number__c=u.Dell_Tag_Number__c;
s.Dongle_Number__c=u.Dongle_Number__c;
s.Equipment_Type__c=u.Equipment_Type__c;
s.ID__c=u.ID__c;
s.Installed_Date_First__c=u.Installed_Date__c;
s.Item_Onsite__c=u.Item_Onsite__c;
s.Machine_Specific_Notes__c=u.Machine_Specific_Notes__c;
s.Manufacturers_Warranty_End_Date__c=u.Manufacturers_Warranty_End_Date__c;
s.NX_Number__c=u.NX_Number__c;
s.Onsite_with_Customer__c=u.On_Site_With_Customer__c;
s.Product__c=u.Product__c;
s.Purchase_Date__c=u.Purchased_Date__c;
s.Serial_Number__c=u.Serial_Number__c;
s.Service_Date__c=u.Service_Date__c;
s.Service_Date_Month__c=u.Service_Date_Month__c;
s.Service_Engineer__c=u.Service_Engineer__c;
s.Service_Month__c=u.Service_Month__c;
s.Service_Month_Text__c=u.Service_Mnth__c;
s.Shipped_to_a_Customer__c=u.Shipped_to_a_Customer__c;
s.Time_Remaining_on_Manuf_Warranty__c=u.Time_Remaining_on_Manuf_Warranty__c;
s.Vendor__c=u.Vendor__c;
s.XD_Number__c=u.XD_Number__c;
sub.add(s);
}
if(sub.size()>0)
insert sub;
}
}
Just write a test class and a test method , And insert a record of Unit__c Object by assigning the appropriate values to all the fields of Unit__c class.
let us know if you have any question .
please don't Forget to Mark this as your best answer if it works fine for you
Regards,
Grazitti Team
Web: www.grazitti.com
You can cover this by insert one Unit__c record with field value Service_Date__c == 'THIS MONTH and give all the required field.
Then this code will get covered automatically.
Eg;
in test method,
Unit__c uu = new Unit__c(Service_Date__c == 'THIS MONTH,name='test',Covered_By_Manuf_Warranty__c='test',Date_for_Call_About_Servces__c='tset'....etc);
Mark this as best answer if its helps.
Thanks.
Praveen Murugesan.
1. Insert a new Unit__c record with Service_Date__c = THIS MONTH'
2. Then assert that that a Service__c was created
More information on Test Methods can be found here:
https://developer.salesforce.com/page/An_Introduction_to_Apex_Code_Test_Methods
Also, please move this line of code OUTSIDE your for loop: