+ Start a Discussion
fiona gentryfiona gentry 

How To Error Handle Batchable Salesforce apex code such that it shouldn't be failing where the Level1, Level2 or Level3 are not there.


Here is my current Batchable apex code,currently this batch job  copies the data over from the new custom object ERT_Case_Type__c to the old custom object Case_Type__c 

But problem is this job failing where the Level1, Level2 or Level3 are not there.   

This is due to the old picklist restricting based on what values we had in the old picklist on these 3 fields...so I have removed that restriction on the 3 Level fields in Case Type
 How can i make sure that Batchable Salesforce apex code  shouldn't be failing where the Level1, Level2 or Level3 are not there.   
global class ertcopybatch3pm implements Database.Batchable<sObject> {
					global Database.QueryLocator start(Database.BatchableContext BC) {
						// collect the batches of records or objects to be passed to execute
						String query = 'select Case__c, Level_1__c, Level_2__c,Level_3__c  FROM ERT_Case_Type__c';
						System.debug('ERT Case No is =====>' +query);
						return Database.getQueryLocator(query);
					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);
						try {
						  System.debug('ERT Case No is =====>' +listCTD);
							insert listCTD;
						} catch(Exception e) {
					global void finish(Database.BatchableContext BC) {
					  // execute any post-processing operations

Thanks in advance,
Best Answer chosen by fiona gentry
Instead of insert method, you may use Database.insert method. In Database class method, you can specify whether or not to allow for partial record processing if errors are encountered. This will help you in successfully inserting the clean data atleast.