function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SArorasSAroras 

Need Test class for simple after insert trigger

Hi ,

Can anyone please help me with test class for below trigger:


trigger CaseTrigger on Case (after insert , after update){
 if(Trigger.isAfter){
   if(Trigger.isInsert || Trigger.isUpdate){
    Set<Id> setIds = new Set<Id>();
    for(Case c :trigger.new){
    if(c.Delete_Case__c == true){
   setIds.add(c.id);

     }

    }
   if(setIds.size()>0){
   List<Case> listCaseToDelete = [Select Id from case where id in:setIds];
      delete listCaseToDelete;
      }
    }
  }
​}
Khan AnasKhan Anas (Salesforce Developers) 
Hi,

Greetings to you!

Please try below code:
@isTest
public class Test_CaseTrigger {
    
    static testMethod void testMethod1() {
        // Please add all required field
        Case c = new Case();
        c.Subject ='TestSub';
        c.Delete_Case__c = true;
        INSERT c;        
    }
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
Deepali KulshresthaDeepali Kulshrestha
Hi SAroras,

I have gone through your question. To make the test Class of Trigger you have to do the two DML operation in your Test Class i.e. insert and Update.

Test Class of your Trigger with 100% code coverage is given below:-


@isTest
public class CaseTriggerTest {
    public testmethod static void caseTestMethod(){
        Case c = new Case();
        c.Origin = 'Phone';
        Test.startTest();
        insert c;
        c.Delete_Case__c = true;
        update c;
        Test.stopTest();
    }
}
 
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com
Ajay K DubediAjay K Dubedi
Hi SAroras,

Use bellow code it may helpful for you
 
@IsTest
public class CaseTrigger_Test {
    @isTest 
    static void CaseTrigger_TestMethod() {
        Account acc = new Account(name='Test Account');
          insert acc;
        List<Case> caseList = new List<Case>();
        for(i=0; i< 2 ; i++){
            Case ca = new Case();
            ca.AccountId = acc.Id;
            // put value here in required fields
            ca.Delete_Case__c == true;
            caseList.add(ca);
        }
        insert caseList;
    }
}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com