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

Improving Code Coverage
I have been working at this for a couple weeks now and I am so close. Currently I have 16% code coverage which is ovboulsy not enough but its something. After battling with it for a couple days I finally was able to improve the coverage from 0% to 16% yesterday.
Below is the trigger I created it is just a simple trigger that when a value is selected on the case it inserts a line item into the related list:
Below is the trigger I created it is just a simple trigger that when a value is selected on the case it inserts a line item into the related list:
trigger UpdateFieldValues on Case (after insert) { Id accessory360Id = [SELECT Id FROM Accessory__c WHERE Name='197 - 360μm FiberFlex™ Fiber' Limit 1].Id; Id accessoryCleaverId = [SELECT Id FROM Accessory__c WHERE Name='241 - Ceramic Fiber Cleavers (set of 3) 1” x 1”' Limit 1].Id; Id accessoryStripperId = [SELECT Id FROM Accessory__c WHERE Name='134 - Fiber Stripping Tool' Limit 1].Id; List<Accessory_Item__c> Accessories = new List<Accessory_Item__c>(); for (Case c : Trigger.New) { if (c.Quick_Order__c == '360 Fiber') { Accessories.add(new Accessory_Item__c( Case__c = c.Id, Item__c = accessory360Id)); } Else if (c.Quick_Order__c == 'Cleaver') { Accessories.add(new Accessory_Item__c( Case__c = c.Id, Item__c = accessoryCleaverId)); } Else if (c.Quick_Order__c == 'Fiber Stripper'){ Accessories.add(new Accessory_Item__c( Case__c = c.Id, Item__c = accessoryStripperId)); } } insert Accessories; }Below is what I currently have for the test class that gives me 16% coverage. I also have an error when I test it that I am trying to figure out and that is what I need help with.
@isTest public class TestQuickUpdate2 { static testMethod void insertNewRecord() { Accessory__c A1 = new Accessory__c(); a1.Name = '197 - 360μm FiberFlex™ Fiber'; a1.Description__c = '360μm FiberFlex™ Fiber'; a1.Item_Num__c = '197'; a1.Unit_Price__c = 295.00; insert A1; Case recordToCreate1 = new Case(); recordToCreate1.Origin = 'Phone'; recordToCreate1.AccountId = '001W000000I931R'; recordToCreate1.ContactId = '003W000000OAOFK'; recordToCreate1.Site__c = 'a0EW0000000fLBY'; recordToCreate1.Quick_Order__c = '360 Fiber'; recordToCreate1.ContactId = '003W000000OAOFK'; insert recordToCreate1; List<Case> Fiber = [SELECT Id FROM Case WHERE Quick_Order__c = '360 Fiber']; system.assertequals(1, Fiber.size()); Accessory__c A2 = new Accessory__c(); a2.Name = '241 - Ceramic Fiber Cleavers (set of 3) 1” x 1”'; a2.Description__c = 'Ceramic Fiber Cleavers (set of 3) 1” x 1”'; a2.Item_Num__c = '241'; a2.Unit_Price__c = 25.00; insert A2; Case recordToCreate2 = new Case(); recordToCreate2.Origin = 'Phone'; recordToCreate2.AccountId = '001W000000I931R'; recordToCreate2.ContactId = '003W000000OAOFK'; recordToCreate2.Site__c = 'a0EW0000000fLBY'; recordToCreate2.Quick_Order__c = 'Cleaver'; recordToCreate2.ContactId = '003W000000OAOFK'; insert recordToCreate2; List<Case> Cleaver = [SELECT Id FROM Case WHERE Quick_Order__c = 'Cleaver']; system.assertequals(1, Cleaver.size()); Accessory__c A3 = new Accessory__c(); a3.Name = '134 - Fiber Stripping Tool'; a3.Description__c = 'Fiber Stripping Tool'; a3.Item_Num__c = '134'; a3.Unit_Price__c = 110.00; insert A3; Case recordToCreate3 = new Case(); recordToCreate3.Origin = 'Phone'; recordToCreate3.AccountId = '001W000000I931R'; recordToCreate3.ContactId = '003W000000OAOFK'; recordToCreate3.Site__c = 'a0EW0000000fLBY'; recordToCreate3.Quick_Order__c = 'Fiber Stripper'; recordToCreate3.ContactId = '003W000000OAOFK'; insert recordToCreate3; List<Case> Stripper = [SELECT Id FROM Case WHERE Quick_Order__c = 'Stripper']; system.assertequals(1, Stripper.size()); } }Thank you for the help in advance.
You have not initalized the Id's and null you are assigning in the item__c so it is throwing the error...
Please replace your trigger code with this code and it will work ..
Trigger
list<Accessory__c> accessory360Id = new list<Accessory__c>([SELECT Id FROM Accessory__c WHERE Name='197 - 360μm FiberFlex™ Fiber' Limit 1]);
list<Accessory__c> accessoryCleaverId = new list<Accessory__c>([SELECT Id FROM Accessory__c WHERE Name='241 - Ceramic Fiber Cleavers (set of 3) 1” x 1”' Limit 1]);
list<Accessory__c> accessoryStripperId = new list<Accessory__c>([SELECT Id FROM Accessory__c WHERE Name='134 - Fiber Stripping Tool' Limit 1]);
List<Accessory_Item__c> Accessories = new List<Accessory_Item__c>();
for (Case c : Trigger.New) {
if (c.Quick_Order_c__c == '360 Fiber') {
if(accessory360Id.size() > 0) {
Accessories.add(new Accessory_Item__c(
Cases__c = c.Id,
Item__c = accessory360Id[0].Id));}
}
Else if
(c.Quick_Order_c__c == 'Cleaver') {
if(accessoryCleaverId.size() > 0) {
Accessories.add(new Accessory_Item__c(
Cases__c = c.Id,
Item__c = accessoryCleaverId[0].Id));}
} Else if
(c.Quick_Order_c__c == 'Fiber Stripper'){
if(accessoryStripperId.size() > 0) {
Accessories.add(new Accessory_Item__c(
Cases__c = c.Id,
Item__c = accessoryStripperId[0].Id));}
}
}
insert Accessories;
Please repalce the above code and check the coverage and let me know if it helps you..
P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.
Thanks,
Sandeep
Salesforce Certified Developer
All Answers
Please repalce your test class with below code
Accessory__c objAcc1 = new Accessory__c (Name = '197 - 360μm FiberFlex™ Fiber');
Insert objAcc1;
Accessory__c objAcc2 = new Accessory__c (Name = '241 - Ceramic Fiber Cleavers (set of 3) 1” x 1”');
insert objAcc2;
Accessory__c objAcc3 = new Accessory__c (Name = '134 - Fiber Stripping Tool');
insert objAcc3;
Case objCase =new Case(Quick_Order__c = '360 Fiber');
insert objCase;
Case objCase1 =new Case(Quick_Order__c = 'Cleaver');
insert objCase1;
Case objCase2 =new Case(Quick_Order__c = 'Fiber Stripper');
insert objCase2;
provide mandatory field for these above objects and simply insert them ..You will be able to cover the 100% code..
P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.
Thanks,
Sandeep
Salesforce Certified Developer
Do I need to create the Contact, Site, and Account?
The Error Message Is Below:
Apex Test Result Detail
Time Started 4/1/2015 4:16 PM
Class TestQuickUpdate
Method Name insertNewRecord
Pass/Fail Fail
Error Message System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, UpdateFieldValues: execution of AfterInsert
caused by: System.QueryException: List has no rows for assignment to SObject
Trigger.UpdateFieldValues: line 3, column 1: []
Stack Trace Class.TestQuickUpdate.insertNewRecord: line 43, column 1
Whatever code I have given you should only keep that code in your test class..rest all thing you can remove then it will work...we should not hardcode the Id's in our code...
In your above test class you have created 3 case but all have same Quick Order..however if you see my code which I have provided, that will be 3 different Quick orders to cover your trigger..
You simple replace all your test code with my code it will work
Please check and let me know if you fac eany issue..
P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.
Thanks,
Sandeep
Salesforce Certified Developer
I think I need to address the erros that it is giving me but I do not understand what it is asking for.
Here is the error:
Apex Test Result Detail
Time Started 4/2/2015 7:59 AM
Class TestQuickUpdate
Method Name insertNewRecord
Pass/Fail Fail
Error Message System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, UpdateFieldValues: execution of AfterInsert
caused by: System.QueryException: List has no rows for assignment to SObject
Trigger.UpdateFieldValues: line 3, column 1: []
Stack Trace Class.TestQuickUpdate.insertNewRecord: line 40, column 1
Can you check if is there any validation rule on Case object or workflow which is firing ??
Please confirm and let me look into this..
Thanks,
Sandeep
You have not initalized the Id's and null you are assigning in the item__c so it is throwing the error...
Please replace your trigger code with this code and it will work ..
Trigger
list<Accessory__c> accessory360Id = new list<Accessory__c>([SELECT Id FROM Accessory__c WHERE Name='197 - 360μm FiberFlex™ Fiber' Limit 1]);
list<Accessory__c> accessoryCleaverId = new list<Accessory__c>([SELECT Id FROM Accessory__c WHERE Name='241 - Ceramic Fiber Cleavers (set of 3) 1” x 1”' Limit 1]);
list<Accessory__c> accessoryStripperId = new list<Accessory__c>([SELECT Id FROM Accessory__c WHERE Name='134 - Fiber Stripping Tool' Limit 1]);
List<Accessory_Item__c> Accessories = new List<Accessory_Item__c>();
for (Case c : Trigger.New) {
if (c.Quick_Order_c__c == '360 Fiber') {
if(accessory360Id.size() > 0) {
Accessories.add(new Accessory_Item__c(
Cases__c = c.Id,
Item__c = accessory360Id[0].Id));}
}
Else if
(c.Quick_Order_c__c == 'Cleaver') {
if(accessoryCleaverId.size() > 0) {
Accessories.add(new Accessory_Item__c(
Cases__c = c.Id,
Item__c = accessoryCleaverId[0].Id));}
} Else if
(c.Quick_Order_c__c == 'Fiber Stripper'){
if(accessoryStripperId.size() > 0) {
Accessories.add(new Accessory_Item__c(
Cases__c = c.Id,
Item__c = accessoryStripperId[0].Id));}
}
}
insert Accessories;
Please repalce the above code and check the coverage and let me know if it helps you..
P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.
Thanks,
Sandeep
Salesforce Certified Developer
Also in your test class replace the last 3 case record which we are inserting with below code
Case objCase1 = new Case(Origin = 'Phone', Quick_Order_c__c = '360 Fiber', Status = 'Phone'
);
insert objCase1;
Case objCase2 = new Case(Origin = 'Phone',
Quick_Order_c__c = 'Cleaver',Status = 'Phone'
);
insert objCase2;
Case objCase3 = new Case(Origin = 'Phone',
Quick_Order_c__c = 'Fiber Stripper',Status = 'Phone');
insert objCase3;
P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.
Thanks,
Sandeep
Salesforce Certified Developer
http://salesforce.stackexchange.com/questions/25047/test-class-for-a-trigger-failure-cannot-insert-update-activate-entity
I am so close! I have 60% code coverage! And my test mothod passed finally! haha now I am not sure what to do.
If you will repalce case objects from your test class and replace them with what I have given in above comments ..you will be able to cover 90%.
Please check and let me know if you are able to cover more than 60%..
P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.
Thanks,
Sandeep
Salesforce Certified Developer
Wow, it finally works and I have 93%. Thank you very much for your time!
feel free to reach out for all your doubts..
Thanks,
Sandeep