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

Test Class for Apex Trigger
Hi All,
Can someone please help me finish this test class for the apex trigger below.
//Trigger will execute only when a Technical Query is created or updated trigger UpdateWarranty on Case (before insert, before update) { //Run a loop through each query that was created or updated for(Case c : Trigger.new){ //Only execute the following statements if Technical query is created else do nothing If(c.RecordTypeId == '012D0000000AoAZ' && c.Serial_Number_New__C != null){ //Store the value of Serial Number which was entered by the user in variable cid String cid = c.Serial_Number_New__c; //Run a query on the Warranty__c table and find the associated fields List<Warranty__c> Serials = [SELECT Id, Engine_Number__c, Model_Number__c FROM Warranty__c WHERE Id = :cid] ; //Store the fields c.Engine_s_n__c = Serials.get(0).Engine_Number__c; c.Model_Number__c = Serials.get(0).Model_Number__c; } } }
This is what i have done so far:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 @isTest private class TestUpdateWarranty { public static testMethod void testUpdateWarranty() { Warranty__c Warranty = new Warranty__c(Name = 'Test11',Model_Number__c = 'MF100',Engine_Number__c = 'ENG100' ); insert Warranty; Warranty__c Warranty1 = [select Id, Name, Model_Number__c, Engine_Number__c FROM Warranty__c where Id =:Warranty.Id]; Warranty1.Engine_Number__c = 'ENG200'; update Warranty1; System.assertEquals('Test11',Warranty1.Name); System.assertEquals('MF100',Warranty1.Model_Number__c); System.assertEquals('ENG200',Warranty1.Engine_Number__c); Case Warranty2 = new Case(Serial_Number_New__c = 'Test11'); System.assertEquals('MF100',Warranty2.Model_Number__c); System.assertEquals('ENG200',Warranty2.Engine_s_n__c);
Try this trigger:
//Trigger will execute only when a Technical Query is created or updated
trigger UpdateWarranty on Case (before insert, before update) {
//Run a loop through each query that was created or updated
for(Case c : Trigger.new){
//Only execute the following statements if Technical query is created else do nothing
If(c.RecordTypeId == '012D0000000AoAZ' && c.Serial_Number_New__c != null){
//Run a query on the Warranty__c table and find the associated fields
List<Warranty__c> Serials = [SELECT Id, Engine_Number__c, Model_Number__c FROM Warranty__c WHERE Id = :c.Serial_Number_New__c limit 1] ;
if(Serials.isEmpty() == false) {
Warranty__c temp = Serials.get(0);
}
//Store the fields
c.Engine_s_n__c = temp.Engine_Number__c;
c.Model_Number__c = temp.Model_Number__c;
}
}
}
All Answers
Does your test work?
I dont see any value for Serial_Number_New__c in your test.
No test doesnt work. I get invalid ID.
The Serial_Number_New__c is a lookup field on object Warranty__c. When a user enters a serial number in Case object (field Serial_Number_New__c) and clicks save the Model Number and Engine Number fields get auto populated.
Then this is your problem
You need to assign the Serial_Number_New__c with a reference to a Warranty object. Either to Warranty or Warranty1.
And then insert the Case obviously.
Hi,
Already tried your solution. Still get error message
"System.AssertException: Assertion Failed: Expected: MF100, Actual: null"
Did you insert the Case?
And did you set the new Case to have the correct recordtype
You should not be hard coding the recordtype like this.
You should do a query on recordtype where name = 'type i want' etc and then use the id returned.
Yes i have done that. Hopefully i have don eit correctly.
Insert Warranty2;
Doh!! Well spotted :-) Thank You!
Code coverage is at 59% but still getting error message
"System.AssertException: Assertion Failed: Expected: MF100, Actual: null"
Show me the test code now
Here you go......
Are you sure the recordtypeid from the query matches that in the trigger?
Yes, the recordtypeId does match.
I dont think you can do list.get(0)
If you are refering to the trigger then yes .get(0) does work. Do i need to do something similer in my test class?
If you trigger is working and the recordtypeid in your test calss ic correct then your test should work.
The trigger defo works, i too cant understand why the test wont work!! The recordtypeid is also correct. Strange!!
Go back to hard coding the recordtypeid into the test where you create the case. Does this change anything?
Hard coded the RecodTypeId and still no difference. Same error message as above.
Code Cov = 59%
Try this trigger:
//Trigger will execute only when a Technical Query is created or updated
trigger UpdateWarranty on Case (before insert, before update) {
//Run a loop through each query that was created or updated
for(Case c : Trigger.new){
//Only execute the following statements if Technical query is created else do nothing
If(c.RecordTypeId == '012D0000000AoAZ' && c.Serial_Number_New__c != null){
//Run a query on the Warranty__c table and find the associated fields
List<Warranty__c> Serials = [SELECT Id, Engine_Number__c, Model_Number__c FROM Warranty__c WHERE Id = :c.Serial_Number_New__c limit 1] ;
if(Serials.isEmpty() == false) {
Warranty__c temp = Serials.get(0);
}
//Store the fields
c.Engine_s_n__c = temp.Engine_Number__c;
c.Model_Number__c = temp.Model_Number__c;
}
}
}