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
Nishit MistryyyNishit Mistryyy 

Test class for updating fields

I have a batch class in which fields are updated from CWL fields to standard fields. I want to write the test class for it with 100% code coverage.

The code for the class is :
*@Class Name - BatchUpdateCTSTfieldsFromCWLCTSTfields
*@Created on: 07/Sept/2020
*@Modified on: 
*@Modified Reason: 
*@description - copying CTST Data from CWL fields to Non CWL fields 
global class BatchUpdateCTSTfieldsFromCWLCTSTfields implements Database.Batchable<sObject>{
    global Database.QueryLocator start(Database.BatchableContext BC){
        string recordTypeName = 'Case without Lan';
        string caseId = '500p0000007m8ItAAI';
        //string query = 'SELECT Id, CWL_Case_Category__c, CWL_Category__c, CWL_Customer_Name__c, CWL_Loan_Number__c, CWL_Sub_Category__c, CWL_Sub_Sub_Category__c, Case_Category__c, Category__c, Sub_Category__c, Sub_Sub_Category__c, FROM Case where =:recordTypeName and Id =: caseId ';      
        string caseCategory = '';
        string query = 'SELECT Id,CaseNumber,Product_Type__c, CWL_Customer_Name__c, CWL_Loan_Number__c, CWL_Case_Category__c, Case_Category__c, CWL_Category__c, Category__c,   CWL_Sub_Category__c, Sub_Category__c, CWL_Sub_Sub_Category__c, Sub_Sub_Category__c, FROM Case where =:recordTypeName and Case_category__c =:caseCategory ';      
            system.debug('#####query@@@'+ query);
        return Database.getQueryLocator(query);
    global void execute(Database.BatchableContext BC, List<Case> CWLCaseList){
        system.debug('CWLCaseList:' + CWLCaseList.Size());
            List<Case> caseList = new List<Case>();
            for (Case caseRec : CWLCaseList){
                if( == 'Case without Lan'){
                    if(caseRec.Product_Type__c == '' ||  caseRec.Product_Type__c == NUll){
                        caseRec.Product_Type__c = 'Others';
                       caseRec.Case_Category__c = caseRec.CWL_Case_Category__c;
                       caseRec.Category__c = caseRec.CWL_Category__c;
                       caseRec.Sub_Category__c = caseRec.CWL_Sub_Category__c;
                       caseRec.Sub_Sub_Category__c = caseRec.CWL_Sub_Sub_Category__c;
            CaseTriggerHelper.isCaseUpdated = true;
            CaseTriggerHelper.isCaseInserted = true;
            system.debug('@@@@@@@@@@@@caseList:@@@@@@@@@@@@' + caseList);
            Database.SaveResult[] updatedCase= Database.update(caseList,false);
            system.debug('@@@@@@@@@@@@updatedCase:@@@@@@@@@@@@' + updatedCase);
            for (Database.SaveResult sr : updatedCase) {
                for(Database.Error err : sr.getErrors()) {
                    ErrorLogCls.SaveError(err.getStatusCode() + ' : ' + err.getMessage() +' : Case fields that affected this error: ' + err.getFields(),'error:' + err, 42,'execute', 'BatchUpdateCTSTfieldsFromCWLCTSTfields','code');
        catch(Exception ex){
            ErrorLogCls.SaveError(ex.getMessage(),ex.getStackTraceString(), ex.getLineNumber(),'execute', 'BatchUpdateCTSTfieldsFromCWLCTSTfields','catch block');
    global void finish(Database.BatchableContext BC){ }

Sai PraveenSai Praveen (Salesforce Developers) 

Did you try creating a test data for the batch class by creating a case with record type as "Case without Lan" and Case_category__c as null.

Nishit MistryyyNishit Mistryyy
No i dont know how to write the test class for this, can you please help me?
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Nishit,

Can you try the test class as below,
public class BatchUpdateCTSTfieldsCWLCTSTfieldsTest {

    static testmethod void triggerTest(){
        Id recordtypeid=Schema.SObjectType.Case.getRecordTypeInfosByName().get('Case without Lan').getRecordTypeId();
        Case caseObj = new Case(
    Status = 'Working',
    Origin = 'Phone');

insert caseObj;
        BatchUpdateCTSTfieldsFromCWLCTSTfields bs= new BatchUpdateCTSTfieldsFromCWLCTSTfields();

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.