+ Start a Discussion
devloper sfdcdevloper sfdc 

please help to write test class of this trigger

      
trigger Stuck_on_Auto_Escalate_Status on Case (before insert, before update ) {
  
        for(Trigger_Control__c tc:Trigger_Control__c.getall().values()){ //this is a custom setting (checkbox field) 
             
             if(tc.Action_Auto_Escalation__c==true)
             
             {
                for(case cs:trigger.new)
                   {   
        
                            if(cs.Status=='Action - Automated Escalation')
                              {
                   
                                     
                                      cs.Stuck_on_Action_Automated_Escalation__c=true;
                                  
                                     
                               }
                     }
        }
    
        }
     }
 
Best Answer chosen by devloper sfdc
RKSalesforceRKSalesforce
Hello,

As  manish suggested you need to insert custom Setting Record manually. It is not best practice to SeeAllData = true in test classes. Please use below code:
public class testCaseTrigger{

  @isTest
  public static void testMethod1(){
  
	Trigger_Control__c trgControl = New Trigger_Control__c();
	trgControl.Action_Auto_Escalation__c = true;
	trgControl.Name = 'Test Name';
	insert trgControl;
	
    Account acc = new Account(Name='Test Account');
    insert acc;
    Contact con = new Contact(LastName='Test Contact', AccountId=acc.Id);
    insert con;
    
    //Insert custom setting records as well here if using seeAllData=false

    Case caseObj = new Case(
       ContactId = con.Id,
       AccountId = acc.Id,
       Status = 'Working',
       Origin = 'Phone');

     insert caseObj; //this should execute your trigger for before insert
     
     caseObj.Status = 'Action - Automated Escalation';
     update caseObj; //this should execute your trigger for before update
  
     //additionally write assert statement based on you condition

   }
}
Please let me know if you have any question. 
Mark as best answer if helped.


Regards,
Ramakant
 

All Answers

Manish  ChoudhariManish Choudhari
Hi,

You can do it either using (seeAllData=false) or (seeAllData=true).

Use below code if you are using seeAllData=true (not recommended as per best practices)
@isTest(seeAllData=true)
public class testCaseTrigger{

  @isTest
  public static void testMethod1(){
    Account acc = new Account(Name='Test Account');
    insert acc;
    Contact con = new Contact(LastName='Test Contact', AccountId=acc.Id);
    insert con;
    
    //Insert custom setting records as well here if using seeAllData=false

    Case caseObj = new Case(
       ContactId = con.Id,
       AccountId = acc.Id,
       Status = 'Working',
       Origin = 'Phone');

     insert caseObj; //this should execute your trigger for before insert
     
     caseObj.Status = 'New';
     update caseObj; //this should execute your trigger for before update
  
     //additionally write assert statement based on you condition

   }
}


Create custom setting records manually in test method if you are using seeAllData=false

Let me know if you have further questions on this.

Thanks,
Manish
RKSalesforceRKSalesforce
Hello,

As  manish suggested you need to insert custom Setting Record manually. It is not best practice to SeeAllData = true in test classes. Please use below code:
public class testCaseTrigger{

  @isTest
  public static void testMethod1(){
  
	Trigger_Control__c trgControl = New Trigger_Control__c();
	trgControl.Action_Auto_Escalation__c = true;
	trgControl.Name = 'Test Name';
	insert trgControl;
	
    Account acc = new Account(Name='Test Account');
    insert acc;
    Contact con = new Contact(LastName='Test Contact', AccountId=acc.Id);
    insert con;
    
    //Insert custom setting records as well here if using seeAllData=false

    Case caseObj = new Case(
       ContactId = con.Id,
       AccountId = acc.Id,
       Status = 'Working',
       Origin = 'Phone');

     insert caseObj; //this should execute your trigger for before insert
     
     caseObj.Status = 'Action - Automated Escalation';
     update caseObj; //this should execute your trigger for before update
  
     //additionally write assert statement based on you condition

   }
}
Please let me know if you have any question. 
Mark as best answer if helped.


Regards,
Ramakant
 
This was selected as the best answer
devloper sfdcdevloper sfdc
Thanks Ramakant its working 
devloper sfdcdevloper sfdc
Tanks Manish for quick responce .