+ Start a Discussion
OssieOssie 

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);

 

Best Answer chosen by Ossie
carlocarlo

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

carlocarlo

Does your test work?

 

I dont see any value for Serial_Number_New__c in your test.

OssieOssie

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.

carlocarlo

Then this is your problem

 

Case Warranty2 = new Case(Serial_Number_New__c = 'Test11');

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.

 

Case Warranty2 = new Case(Serial_Number_New__c = Warranty.Id);
OssieOssie

Hi,

 

Already tried your solution.  Still get error message

"System.AssertException: Assertion Failed: Expected: MF100, Actual: null"

carlocarlo

Did you insert the Case?

carlocarlo

And did you set the new Case to have the correct recordtype

 

c.RecordTypeId == '012D0000000AoAZ'
carlocarlo

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.

OssieOssie

Yes i have done that.  Hopefully i have don eit correctly.

 

@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);
        
        Id RrdTypeId = [SELECT Id from RecordType WHERE sObjectType='Case' AND isActive=true AND Name='Technical'limit 1].Id;
        
        Case Warranty2 = new Case(Serial_Number_New__c = Warranty.Id, RecordTypeId = RrdTypeId);
        
        System.assertEquals('MF100',Warranty2.Model_Number__c);
        System.assertEquals('ENG200',Warranty2.Engine_s_n__c);
        }
}

 

carlocarlo

Insert Warranty2;

 

OssieOssie

Doh!! Well spotted :-) Thank You!

 

Code coverage is at 59% but still getting error message

"System.AssertException: Assertion Failed: Expected: MF100, Actual: null"

carlocarlo

Show me the test code now

OssieOssie

Here you go......

 

@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);
        
        Id RrdTypeId = [SELECT Id from RecordType WHERE sObjectType='Case' AND isActive=true AND Name='Technical'limit 1].Id;
        
        Case Warranty2 = new Case(Serial_Number_New__c = Warranty.Id, RecordTypeId = RrdTypeId);
        Insert Warranty2;
        
        System.assertEquals('MF100',Warranty2.Model_Number__c);
        System.assertEquals('ENG200',Warranty2.Engine_s_n__c);
        }
}

 

carlocarlo

Are you sure the recordtypeid from the query matches that in the trigger?

OssieOssie

Yes, the recordtypeId does match.

carlocarlo

I dont think you can do list.get(0)

OssieOssie

If you are refering to the trigger then yes .get(0) does work.  Do i need to do something similer in my test class?

carlocarlo

If you trigger is working and the recordtypeid in your test calss ic correct then your test should work.

OssieOssie

The trigger defo works, i too cant understand why the test wont work!!  The recordtypeid is also correct.  Strange!!

carlocarlo

Go back to hard coding the recordtypeid into the test where you create the case.  Does this change anything?

OssieOssie

Hard coded the RecodTypeId and still no difference.  Same error message as above.

Code Cov = 59%

carlocarlo

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;

           }

           

     }

}

This was selected as the best answer