+ Start a Discussion
fiona gentryfiona gentry 

Test class for Batchable to insert records, iterate over the results, and persist information about any failures to the database

Hi Gurus,

Need a Test class for Batchable class with database.insert function then saves the results and iterate through each results 
 
global void execute(Database.BatchableContext BC, List<ERT_Case_Type__c> exeList) {
						
						// process each batch of records
						List<Case_Type__c> listCTD = new List<Case_Type__c>();
						System.debug('ERT Case No is =====>' +exeList);
						for(ERT_Case_Type__c exe : exeList)
						{        
							listCTD.add(new Case_Type__c(Case__c=exe.Case__c,Level_1__c=exe.Level_1__c,Level_2__c=exe.Level_2__c,Level_3__c=exe.Level_3__c));
							// System.debug('ERT Case No is =====>' +Case__c);
						}

						System.debug('ERT Case No is =====>' +listCTD);
						insert listCTD;
						
						
						Database.SaveResult[] srList = Database.insert(listCTD, false);
						
						// Iterate through each returned result
						for (Database.SaveResult sr : srList) {
							if (sr.isSuccess()) {
								// Operation was successful, so get the ID of the record that was processed
								System.debug('Successfully inserted Case_Type__c: ' + sr.getId());
							}
							else {
								// Operation failed, so get all errors                
								for(Database.Error err : sr.getErrors()) {
									System.debug('The following error has occurred.');                    
									System.debug(err.getStatusCode() + ': ' + err.getMessage());
									System.debug('Case_Type__c fields that affected this error: ' + err.getFields());
								}
							}
						}            
						
					}   
					
					global void finish(Database.BatchableContext BC) {
						// execute any post-processing operations
					}
				}

 
Best Answer chosen by fiona gentry
RituSharmaRituSharma
Your logic is based on ERT_Case_Type__c object. So to create the test class, you will need to firstly insert ERT_Case_Type__c  records and then call the batch class.

Refer this URL -> https://apexcoder.com/2016/11/08/how-to-write-test-class-for-batch-apex-in-salesforce/
 

All Answers

RituSharmaRituSharma
Your logic is based on ERT_Case_Type__c object. So to create the test class, you will need to firstly insert ERT_Case_Type__c  records and then call the batch class.

Refer this URL -> https://apexcoder.com/2016/11/08/how-to-write-test-class-for-batch-apex-in-salesforce/
 
This was selected as the best answer
fiona gentryfiona gentry
Hi @RituSharma,I i did wrote test class but I see only 64% covered how do I make 100% I updated my test class
 
@isTest
					public class testertnoonbatch3pm{
						static testMethod void testMethod1() 
						{
							List<ERT_Case_Type__c> lstExecutive_RT= new List<ERT_Case_Type__c>();
								Case c = new Case();
							c.Contact_First_Name__c = 'first name';
							c.Contact_Last_Name__c = 'last name';
							c.Address_Line_1__c = 'first name';
							c.Address_Line_2__c = 'first name';
							c.Address_City__c = 'first name';
							c.Address_State__c = 'WA';
							c.Address_Zip__c = '12345';
							c.Home_Phone__c = '123-456-7890';
							c.E_Mail__c = 'abc@abc.com';
							c.hasdevice__c = true;
							c.IMEI__c = 1234567890123.0;
							c.InterestedinTFB__c = true;
							insert c;
							for(Integer i=0 ;i <200;i++)
							{
								ERT_Case_Type__c rt = new ERT_Case_Type__c();
								//5001Q000016wP1BQAU
								rt.Case__c = c.Id;
								rt.Level_1__c ='Customer Service'+i;
								rt.Level_2__c ='Customer Care  -  T-Mobile';
								rt.Level_3__c ='Upholding policy - account services';
								system.debug('inserted value'+rt);
							   lstExecutive_RT.add(rt); 
								
							
							}
							
							insert lstExecutive_RT;
							
							Test.startTest();

								ertnoonbatch3pm objbatch3pm = new ertnoonbatch3pm();
								
								DataBase.executeBatch(objbatch3pm); 
								
							Test.stopTest();
						}

					}

 
fiona gentryfiona gentry
Here are error 
User-added image
RituSharmaRituSharma
it seems that listCTD  is empty. Check the batch query and see if it's fetching some records.