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
Vidya H 4Vidya H 4 

how to write batch class for this

trigger NumberOfChild on Case (After Insert,After Update,After Delete) {
   List<Account> accList=new List<Account>();

    Set<Id> setAccIds = new Set<Id>();
    if(Trigger.isInsert){
         if(trigger.isAfter){
        for(Case con : Trigger.new){
            if(con.AccountId != null){
            setAccIds.add(con.AccountId);
                }
            }
        }
    } 
    system.debug('setAccIds ==> '+setAccIds);
    if(Trigger.isUpdate){
         if(trigger.isAfter){
        for(Case con : Trigger.new){ 
            if(con.AccountId!=Trigger.oldMap.get(con.Id).AccountId){
                   setAccIds.add(con.AccountId);
                setAccIds.add(Trigger.oldMap.get(con.Id).AccountId);
                }
          
            }        
        }
    }
    if(Trigger.isDelete){
        if(trigger.isAfter){
        for(Case con : Trigger.old) { 
            if(con.AccountId != null){
            setAccIds.add(con.AccountId);
                }
            }
        }
    }    
    for(Account acc :[Select id,Summary__c ,(Select id,Description from Cases) from Account where Id in : setAccIds]){
      String s ='';
        for(Case Con :acc.Cases){
            s+=Con.Description +',';
        }
        acc.Summary__c =  s.removeEnd(',');
        acclist.add(acc);
    }
    if(acclist.size()>0){
        update accList;     
    }
}
Best Answer chosen by Vidya H 4
Suraj Tripathi 47Suraj Tripathi 47
Hi vidya,
Hope you are doing good
I have written a Test class for your requirement, please follow the below code for your reference.
@isTest
private class TestNumberOfChild {
    @isTest static void TestInsertAccountWithCases() {
        Account acc = new Account();
        acc.Name = 'abc';
        insert acc; 
        Case caseObj = new Case();
        caseObj.AccountId = acc.Id;
        caseObj.Origin ='Email';
        caseObj.Status ='working';
        insert caseObj;
        update caseObj;
        delete caseObj;
        }
}

Hope you find the above solution helpful. If it does, please mark it as Best Answer to help others too
Thanks and regards
Suraj Tripathi

All Answers

Vidya H 4Vidya H 4
Actually I need to write test class for that trigger code
Suraj Tripathi 47Suraj Tripathi 47
Hi vidya,
Hope you are doing good
I have written a Test class for your requirement, please follow the below code for your reference.
@isTest
private class TestNumberOfChild {
    @isTest static void TestInsertAccountWithCases() {
        Account acc = new Account();
        acc.Name = 'abc';
        insert acc; 
        Case caseObj = new Case();
        caseObj.AccountId = acc.Id;
        caseObj.Origin ='Email';
        caseObj.Status ='working';
        insert caseObj;
        update caseObj;
        delete caseObj;
        }
}

Hope you find the above solution helpful. If it does, please mark it as Best Answer to help others too
Thanks and regards
Suraj Tripathi
This was selected as the best answer
Vidya H 4Vidya H 4
thanks